Skip to content

Feature: Threader

Tron edited this page Jan 13, 2023 · 1 revision

━ What's the Objective?

Most importantly for Async operation when you've plenty of executions to be performed on some heavy data which would cause infinite loop termination normally. Essentially its also needed by Async/Await functionality provided by the same module.

━ How to Import?

Add the below code once in either of the shared .lua script of the resource you want to use within:

--Declare it globally only once
loadstring(exports.assetify_library:import("threader"))()

━ APIs

━ assetify.thread:getType() (Shared)

@Objective: Retrieve's instance's type.
local string: type = self:getType()

━ assetify.thread:getThread() (Shared)

@Objective: Retrieve's current running thread.
local thread: cThread = assetify.thread:getThread()

━ assetify.thread:create() (Shared)

@Objective: Creates a new thread.
ℹ️ Acronym: async()
local thread: cThread = assetify.thread:create(
  function: exec
)

━ assetify.thread:createHeartbeat() (Shared)

@Objective: Creates a new heartbeat.
ℹ️ Acronym: heartbeat()
local thread: cThread = assetify.thread:createHeartbeat(
  function: conditionExec,
  function: exec,
  int = rate
)

━ assetify.thread:createPromise() (Shared)

@Objective: Creates a new promise.
ℹ️ Acronym: promise()
local promise: cPromise = assetify.thread:createPromise(
  function: callback(resolve, reject), --(Optional) Note: Thread instance is passed as param if Async is enabled; i.e, callback(cThread, resolve, reject)
  table: {
    isAsync = false, --(Optional): Bool flag indicating whether the handle should be asynchronous
    timeout = 6000 --(Optional): Timeout duration in milliseconds
  }
)

cPromise: {
  resolve = resolve(...),
  reject = reject(...)
}

━ assetify.thread:destroy() (Shared)

@Objective: Destroys an existing thread.
local bool: result = self:destroy()

━ assetify.thread:pause() (Shared)

@Objective: Pauses the current thread.
local bool: result = assetify.thread:pause()

━ assetify.thread:status() (Shared)

@Objective: Retrieves the status of a specified thread.
local string: status = self:status()

━ assetify.thread:resume() (Shared)

@Objective: Resumes a paused thread.
--Syntax #1:
local bool: result = self:resume()

--Syntax #2:
--Note: Use this when you need to repeatedly resume a thread
local bool: result = self:resume(
  table: {
    int: executions = 1 --Number of executions within a resume
    int: frames = 100 --Number of frames to run the resume interval at
  }
)

━ assetify.thread:sleep() (Shared)

@Objective: Pauses the current thread for specified duration.
ℹ️ Acronym: sleep()
--Duration must in milliseconds
local bool: result = self:sleep(
  int: duration
)

━ assetify.thread:await() (Shared)

@Objective: Pauses the current thread & awaits the exec to be resolved.
ℹ️ Acronym: await()
local bool: result = self:await(
  function: exec
)

━ assetify.thread:try() (Shared)

@Objective: Safely executes provided handle while catching its exceptions.
ℹ️ Acronym: try()
local ~: ...results = self:try(
  table: {
    exec = function(self)
       --Your codeblocks here
       return a, b, c
    end,
    catch = function(...)
        iprint(table.pack(...))
        return false
    end
  }
)
Clone this wiki locally