UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

95 lines (94 loc) 3.07 kB
export declare type ThreadTaskData = ArrayBuffer | Uint8Array | Uint16Array | Uint32Array | Float32Array | Float64Array; export interface ThreadTaskDataObject { [index: string]: ThreadTaskData | ThreadTaskData[] | ThreadTaskDataObject; } export interface ThreadTask { task: "chunk-generation" | "init-chunk-generator" | "chunk-geometry" | "init-geometry-generator"; data: ThreadTaskData | ThreadTaskData[] | ThreadTaskDataObject; cb?: (data?: any) => void; } export default class Thread { private worker; private inUse; private currentTask; private queue; private maxQueueSize; private id; private cleanRate; /** * Creates a {@link Thread} instance, sets up it's worker and starts it's cleaning loop. * * @param id A string to be used as an identifier for this thread. */ constructor(id: string); /** * Sets up the thread's worker and it's associated events. */ private setupWorker; /** * Handles a message from the thread's worker * * @param msg The message received from the worker */ private handleMessage; /** * Attempts to run a task on the thread or add it to the queue. * * @param task The task to run or queue. * @param skipQueue If true then the max queue size is ignored and the task is prepended to the front of the queue * @returns True when the task is ran or queued, false otherwise */ addTask(task: ThreadTask, skipQueue?: boolean): boolean; /** * Sends a task to the thread's worker, saves a reference to the sent task and marks the thread as in use. * * No checks are made to see if the thread is in use before sending the task. * * @param task The task to send to the thread's worker. */ private sendTask; /** * Sends the next task in the thread's queue to it's worker. * * If the queue is empty then the thread marks itself as not in use. */ private nextTask; /** * Attempts to clean out the thread's queue whenever the thread has hung. * * A hung thread is one in which the thread is not in use but it has task(s) in it's queue. */ private clean; /** * Returns wether the thread is currently in use. * * This is true when the thread's worker is currently processing a task. * * @returns If the thread is currently in use. */ getInUse(): boolean; /** * A thread is free when it is not in use or the queue size is below the thread's maximum queue size * * @returns true/false depending on if the thread is free */ isFree(): boolean; /** * Gets the number of task in the thread's queue. * * @returns The length of the thread's queue. */ getNumInQueue(): number; /** * Gets the thread's id. * * @returns The thread's id */ getId(): string; /** * Prepends a console.log call with the thread's id. * * @param params Params for `console.log` */ private log; }