UNPKG

@awesome-ecs/workers

Version:

The Workers package for Awesome ECS. Provides basic implementation for the Worker communication using ECS.

1 lines 4.93 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/helpers/worker-handler-registry.ts","../../src/helpers/worker-message-queue.ts"],"sourcesContent":["import { IWorkerHandlerRegistry } from '../abstract/worker-handler-registry';\nimport { WorkerInstance } from '../abstract/worker-instance';\n\nexport class WorkerHandlerRegistry implements IWorkerHandlerRegistry {\n private readonly handlers = new Map<string, WorkerInstance<any, any>>();\n\n register(requestType: string, handler: WorkerInstance<any, any>): void {\n this.handlers.set(requestType, handler);\n }\n\n resolve(requestType: string): WorkerInstance<any, any> | undefined {\n return this.handlers.get(requestType);\n }\n}\n","import { WorkerRequestMessage } from '../abstract/worker-message';\nimport { IWorkerMessageQueue } from '../abstract/worker-queue';\n\nconst COMPACTION_THRESHOLD = 1024;\n\n// @injectable\nexport class WorkerMessageQueue implements IWorkerMessageQueue {\n private _queue: WorkerRequestMessage<any>[];\n private _head: number;\n private _enqueueTimes: number[];\n private _stalledEntries: boolean[];\n private _recentStalledWaitTimesMs: number[];\n\n constructor() {\n this._queue = [];\n this._head = 0;\n this._enqueueTimes = [];\n this._stalledEntries = [];\n this._recentStalledWaitTimesMs = [];\n }\n\n get size(): number {\n return this._queue.length - this._head;\n }\n\n get queue(): WorkerRequestMessage<any>[] {\n return this._queue.slice(this._head);\n }\n\n enqueue(message: WorkerRequestMessage<any>): void {\n this._queue.push(message);\n this._enqueueTimes.push(performance.now());\n this._stalledEntries.push(false);\n }\n\n dequeue(): WorkerRequestMessage<any> {\n if (this._head >= this._queue.length) {\n return undefined;\n }\n\n const item = this._queue[this._head];\n const enqueuedAt = this._enqueueTimes[this._head];\n const wasStalled = this._stalledEntries[this._head] === true;\n this._head++;\n\n if (enqueuedAt !== undefined && wasStalled) {\n this._recentStalledWaitTimesMs.push(performance.now() - enqueuedAt);\n }\n\n // Compact the backing array once the consumed prefix is large enough\n // to avoid unbounded memory growth.\n if (this._head >= COMPACTION_THRESHOLD || this._head * 2 >= this._queue.length) {\n this._queue = this._queue.slice(this._head);\n this._enqueueTimes = this._enqueueTimes.slice(this._head);\n this._stalledEntries = this._stalledEntries.slice(this._head);\n this._head = 0;\n }\n\n return item;\n }\n\n /** Marks all currently queued messages as stalled until they are eventually dispatched. */\n markAllAsStalled(): void {\n for (let i = this._head; i < this._stalledEntries.length; i++) {\n this._stalledEntries[i] = true;\n }\n }\n\n /** Returns and clears wait times for messages that previously stalled before dispatch. */\n flushWaitTimes(): number[] {\n if (!this._recentStalledWaitTimesMs.length) return [];\n const result = this._recentStalledWaitTimesMs;\n this._recentStalledWaitTimesMs = [];\n return result;\n }\n\n peek(): WorkerRequestMessage<any> {\n if (this._head < this._queue.length) {\n return this._queue[this._head];\n }\n\n return undefined;\n }\n\n clear(): void {\n this._queue = [];\n this._head = 0;\n this._enqueueTimes = [];\n this._stalledEntries = [];\n this._recentStalledWaitTimesMs = [];\n }\n}\n"],"mappings":";AAGA,IAAa,wBAAb,MAAqE;CACnE,2BAA4B,IAAI,IAAsC;CAEtE,SAAS,aAAqB,SAAyC;EACrE,KAAK,SAAS,IAAI,aAAa,OAAO;CACxC;CAEA,QAAQ,aAA2D;EACjE,OAAO,KAAK,SAAS,IAAI,WAAW;CACtC;AACF;;;ACVA,MAAM,uBAAuB;AAG7B,IAAa,qBAAb,MAA+D;CAC7D;CACA;CACA;CACA;CACA;CAEA,cAAc;EACZ,KAAK,SAAS,CAAC;EACf,KAAK,QAAQ;EACb,KAAK,gBAAgB,CAAC;EACtB,KAAK,kBAAkB,CAAC;EACxB,KAAK,4BAA4B,CAAC;CACpC;CAEA,IAAI,OAAe;EACjB,OAAO,KAAK,OAAO,SAAS,KAAK;CACnC;CAEA,IAAI,QAAqC;EACvC,OAAO,KAAK,OAAO,MAAM,KAAK,KAAK;CACrC;CAEA,QAAQ,SAA0C;EAChD,KAAK,OAAO,KAAK,OAAO;EACxB,KAAK,cAAc,KAAK,YAAY,IAAI,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