Small World Engine API Reference - v0.46.1
    Preparing search index...

    Class ThreadPool

    A generic Web Worker pool for offloading heavy CPU tasks. Uses Blob URLs to dynamically spawn workers without requiring external script files.

    const pool = new ThreadPool();
    const result = await pool.execute((data) => {
    // Heavy calculation here. Runs on a background thread!
    // NOTE: Cannot access variables outside this function's scope.
    return data.a + data.b;
    }, { a: 10, b: 20 });
    Index

    Constructors

    Methods

    Constructors

    Methods

    • Executes a function on a background worker thread.

      Type Parameters

      • TData
      • TResult

      Parameters

      • fn: (data: TData) => TResult | Promise<TResult>

        The function to execute. WARNING: This function is serialized to a string. It cannot access variables, imports, or context outside its own scope. Everything it needs must be passed in via the data parameter.

      • data: TData

        The input data passed to the function. Must be JSON serializable.

      • transferables: Transferable[] = []

        Optional array of Transferable objects to pass by reference (zero-copy).

      Returns Promise<TResult>

      A Promise resolving with the function's return value.