Pixi’VN
indexNamespacesGameVariables

Variable: worker

> const worker: object

Defined in: src/index.ts:506

Optional integration point for offloading Pixi'VN's own heavy, pure computations (state diffing/restoring/cloning for stepHistory.back()/add()/export()) to a Worker the game project provides. Pixi'VN never creates a Worker itself, decides on its own which eligible work actually goes through it, and falls back to computing the same thing synchronously on the main thread whenever none is registered - so projects that can't or don't want to use Workers (SSR, certain embeds, ...) are unaffected either way.

The game project stays in control of the actual new Worker(...) call (bundler-specific URL/type: "module" handling, CSP, lifecycle) and only needs to point that worker's message handling at handleGameWorkerMessage from @drincs/pixi-vn/worker:

// main thread
import { Game } from "@drincs/pixi-vn";
const worker = new Worker(new URL("./game-worker.ts", import.meta.url), { type: "module" });
Game.worker.register(worker);

// game-worker.ts
import { handleGameWorkerMessage } from "@drincs/pixi-vn/worker";
self.onmessage = async (event) => {
    const response = await handleGameWorkerMessage(event.data);
    if (response) self.postMessage(response);
};

A third-party library built on top of Pixi'VN (one with @drincs/pixi-vn as a peer dependency) can share this same worker for its own heavy, pure computations instead of needing one of its own - see registerGameWorkerHandler (worker side) and GameWorkerManager.custom (main-thread side), both from @drincs/pixi-vn/worker.

Type Declaration

register

> register: (worker) => void

Parameters

worker

Worker

Returns

void

unregister

> unregister: () => void

Returns

void

isAvailable

Get Signature

> get isAvailable(): boolean

Returns

boolean

On this page