Skip to content

Library: Threader

ᴏᴠ ━ ᴀɴɪꜱᴀ edited this page Dec 12, 2024 · 18 revisions

» Overview

The primary goal of this module is to provide robust asynchronous operation handling, particularly in scenarios involving:

  • Concurrent execution of multiple heavy data processing tasks.
  • Preventing potential infinite loop scenarios.
  • Supporting advanced Async/Await functionality.

» Key Features

  • Enhanced performance for complex, data-intensive operations.
  • Improved concurrency management.
  • Streamlined asynchronous programming patterns. By implementing this approach, developers can efficiently manage and execute multiple tasks without blocking the main thread, ensuring responsive and scalable application performance.

» Importing

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

loadstring(exports.assetify_library:import("threader"))()

» APIs

  • assetify.thread:getType() - shared

    Retrieve's instance's type.

    local string: type = self:getType()
  • assetify.thread:getThread() - shared

    Retrieve's current running thread.

    local thread: cThread = assetify.thread:getThread()
  • assetify.thread:create() - shared

    Creates a new thread.
    💡 Altform: async()

    local thread: cThread = assetify.thread:create(
       function: exec
    )
  • assetify.thread:createHeartbeat() - shared

    Creates a new heartbeat.
    💡 Altform: heartbeat()

    local thread: cThread = assetify.thread:createHeartbeat(
       function: conditionExec,
       function: exec,
       int = rate
    )
  • assetify.thread:createPromise() - shared

    Creates a new promise.
    💡 Altform: promise()

    local promise: cPromise = assetify.thread:createPromise(
       function: callback(resolve, reject), --Optional: 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

    Destroys an existing thread.

    local bool: result = self:destroy()
  • assetify.thread:pause() - shared

    Pauses the current thread.

    local bool: result = assetify.thread:pause()
  • assetify.thread:status() - shared

    Retrieves the status of a specified thread.

    local string: status = self:status()
  • assetify.thread:resume() - shared

    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 per resume
            int: frames = 100 --Number of frames at which the the resume interval would run
         }
      )
  • assetify.thread:sleep() - shared

    Pauses the current thread for specified duration.
    💡 Altform: sleep()

    local bool: result = self:sleep(
       int: duration --Duration in milliseconds
    )
  • assetify.thread:await() - shared

    Pauses the current thread & awaits the exec to be resolved.
    💡 Altform: await()

    local bool: result = self:await(
       function: exec
    )
  • assetify.thread:try() - shared

    Safely executes provided handle while catching its exceptions.
    💡 Altform: 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