@lightningjs/threadx
Version:
A web browser-based JavaScript library that helps manage the communcation of data between one or more web worker threads.
109 lines (108 loc) • 3.72 kB
TypeScript
export type StructPropType = 'string' | 'number' | 'boolean' | 'int32';
export type BufferStructConstructor<WritableProps = object, T extends BufferStruct = BufferStruct> = {
new (): T & WritableProps;
propDefs: PropDef[];
};
export interface StructPropOptions {
propToBuffer?(value: unknown): unknown;
bufferToProp?(value: unknown): unknown;
/**
* Allow the value of this property to be undefined.
*
* @remarks
* If true, the property will be undefined by default. Be sure to type
* the property as `type | undefined` in the BufferStruct interface,
* getter and setter.
*/
allowUndefined?: boolean;
}
export declare function structProp(type: StructPropType, options?: StructPropOptions): (target: BufferStruct, key: string, descriptor: PropertyDescriptor) => void;
interface PropDef {
propNum: number;
name: string;
type: StructPropType;
byteOffset: number;
offset: number;
byteSize: number;
allowUndefined: boolean;
}
/**
* BufferStruct Header Structure:
* Int32[0]
* Type ID: Type of object (32-bit identifier)
* Int32[1]
* Notify / Last Mutator Worker ID
* Int32[2]
* Lock
* Int32[3]
* RESERVED (64-bit align)
* Int32[4 - 5] / Float64[ID_FLOAT64_INDEX = 2]
* Shared Unique ID of the object
* Int32[DIRTY_INT32_INDEX = 6]
* Dirty Bit Mask 1 (Property Indices 0-31)
* Int32[DIRTY_INT32_INDEX + 1 = 7]
* Dirty Bit Mask 2 (Property Indices 32-63)
* Int32[UNDEFINED_INT32_INDEX = 8]
* Undefined Bit Mask 1 (Property Indices 0-31)
* Int32[UNDEFINED_INT32_INDEX + 1 = 9]
* Undefined Bit Mask 2 (Property Indices 32-63)
*
* HEADER SIZE MUST BE A MULTIPLE OF 8 BYTES (64-BIT ALIGNMENT)
*/
export declare abstract class BufferStruct {
buffer: SharedArrayBuffer;
protected lockId: number;
protected uint16array: Uint16Array;
protected int32array: Int32Array;
protected float64array: Float64Array;
static staticInitialized: boolean;
static typeId: number;
static typeIdStr: string;
static size: number;
static propDefs: PropDef[];
constructor(buffer?: SharedArrayBuffer);
/**
* Safely extract the TypeID from any SharedArrayBuffer (as if it is a BufferStruct)
*
* @remarks
* Does not check if the TypeID is valid however it does a basic sanity check to
* ensure the buffer is large enough to contain the TypeID at Int32[TYPEID_INT32_INDEX].
*
* If the buffer is found to be invalid, 0 is returned.
*
* @param buffer
* @returns
*/
static extractTypeId(buffer: SharedArrayBuffer): number;
/**
* Checks if typeId is valid and sets up static properties when the first
* structProp() decorator is set-up on the class.
*
* @remarks
* WARNING: This should not ever be called directly.
*
* @internal
*/
static initStatic(): void;
protected setDirty(propIndex: number): void;
resetDirty(): void;
isDirty(propIndex?: number): boolean;
protected setUndefined(propIndex: number, value: boolean): void;
protected isUndefined(propIndex: number): boolean;
get typeId(): number;
get id(): number;
/**
* Returns the current notify value
*/
get notifyValue(): number;
/**
* Returns true if the BufferStruct is currently locked
*/
get isLocked(): boolean;
lock<T>(callback: () => T): T;
lockAsync<T>(callback: (...args: any[]) => Promise<T>): Promise<T>;
notify(value?: number): number;
wait(expectedValue: number, timeout?: number): "ok" | "not-equal" | "timed-out";
waitAsync(expectedValue: number, timeout?: number): Promise<'not-equal' | 'timed-out' | 'ok'>;
}
export {};