@lightningjs/threadx
Version:
A web browser-based JavaScript library that helps manage the communcation of data between one or more web worker threads.
83 lines (82 loc) • 3.01 kB
TypeScript
import type { IEventEmitter } from './IEventEmitter.js';
import type { BufferStruct } from './BufferStruct.js';
export declare class SharedObject implements IEventEmitter {
/**
* The ThreadX instance that this SharedObject should interact with
*
* @remarks
* It's unsafe to use `ThreadX.instance` in different, especially asyncronous,
* locations directly because it may change during the lifetime of a
* SharedObject. At least it can during tests. So this one should always
* be referenced when needed.
*/
private threadx;
private sharedObjectStruct;
protected mutations: {
[s in string]?: true;
};
private waitPromise;
private mutationsQueued;
static staticInitialized: boolean;
private _id;
private _typeId;
private initialized;
private destroying;
z$__type__Props: object;
protected curProps: this['z$__type__Props'];
/**
* Extract the buffer from a SharedObject
*
* @remarks
* For internal use by ThreadX only
*
* @param sharedObject
* @returns
*/
static extractBuffer(sharedObject: SharedObject): SharedArrayBuffer;
constructor(sharedObjectStruct: BufferStruct, curProps: Record<string, unknown>);
get typeId(): number;
get id(): number;
/**
* Assumes lock is acquired
*/
protected processDirtyProperties(): void;
onPropertyChange<Key extends keyof this['z$__type__Props']>(propName: Key, newValue: this['z$__type__Props'][Key], oldValue: this['z$__type__Props'][Key] | undefined): void;
queueMutations(): void;
private mutationMicrotask;
flush(): void;
/**
* Called when the SharedObject is being destroyed.
*
* @remarks
* This is an opportunity to clean up anything just prior to the SharedObject
* being completely destroyed. Shared mutations are allowed in this method.
*
* IMPORTANT:
* `super.onDestroy()` must be called at the END of any subclass override to
* ensure proper cleanup.
*/
protected onDestroy(): void;
/**
* Destroy the SharedObject on this worker only.
*
* @remarks
* This stops any internal mutation processing, releases the reference
* to the underlying BufferStruct/SharedArrayBuffer, and removes all
* event listeners so that the SharedObject can be garbage collected.
*
* This does not destroy the SharedObject on other worker. To do that,
* call `SharedObject.destroy()` on the other worker.
*/
destroy(): void;
private finishDestroy;
get isDestroyed(): boolean;
private _executeMutations;
private eventListeners;
on(event: string, listener: (target: any, data: any) => void): void;
off(event: string, listener: (target: any, data: any) => void): void;
once(event: string, listener: (target: any, data: any) => void): void;
emit(event: string, data: Record<string, unknown>, options?: {
localOnly?: boolean;
}): void;
}