@thingts/execution - v1.0.0
    Preparing search index...

    Function serialize

    • Create a serializer to cause repeated async functions calls to be queued for execution one at a time.

      By default, each serialized function is serialized in its own queue. An optional group parameter allows multiple functions to share the same serialization queue, keyed by an arbitrary value.

      When used as a method decorator, the queues (both individual and group) are scoped to the object instance, so that calls to methods on the same instance are serialized, but calls on different object instances can run concurrently.

      Parameters

      • opts: SerializeOptions = {}
        {
        group?: string | number | boolean | symbol | bigint | object
        }
        • group: Identifier (a.k.a. key) for a shared serialization queue to use. If omitted, each function or method that serialize() is applied to is serialized in its own queue. Any non-nullish value or object may be used as a group key.

      Returns Serializer

      import { serialize } from '@thingts/execution'

      // Basic functional form:
      const oneAtATime = serialize()(async function fetchData(...) { ... })

      // With grouping:
      const save = serialize({ group: 'db' })(saveToDb)
      const load = serialize({ group: 'db' })(loadFromDb)

      // With serializer object:
      const serializer: Serializer = serialize({ group: 'tasks' })
      const task1 = serializer(async function task1(...) { ... })
      const task2 = serializer(async function task2(...) { ... })


      // Decorator form (ECMAScript / TypeScript 5.2+)
      class Player {
      @serialize()
      async load(url: string) { ... }

      @serialize({ group: 'controls' })
      async play() { ... }

      @serialize({ group: 'controls' })
      async pause() { ... }
      }