workerPool.d.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type { IDisposable } from "../scene";
  2. /** @ignore */
  3. interface WorkerInfo {
  4. workerPromise: Promise<Worker>;
  5. idle: boolean;
  6. timeoutId?: ReturnType<typeof setTimeout>;
  7. }
  8. /**
  9. * Helper class to push actions to a pool of workers.
  10. */
  11. export declare class WorkerPool implements IDisposable {
  12. protected _workerInfos: Array<WorkerInfo>;
  13. protected _pendingActions: ((worker: Worker, onComplete: () => void) => void)[];
  14. /**
  15. * Constructor
  16. * @param workers Array of workers to use for actions
  17. */
  18. constructor(workers: Array<Worker>);
  19. /**
  20. * Terminates all workers and clears any pending actions.
  21. */
  22. dispose(): void;
  23. /**
  24. * Pushes an action to the worker pool. If all the workers are active, the action will be
  25. * pended until a worker has completed its action.
  26. * @param action The action to perform. Call onComplete when the action is complete.
  27. */
  28. push(action: (worker: Worker, onComplete: () => void) => void): void;
  29. protected _executeOnIdleWorker(action: (worker: Worker, onComplete: () => void) => void): boolean;
  30. protected _execute(workerInfo: WorkerInfo, action: (worker: Worker, onComplete: () => void) => void): void;
  31. }
  32. /**
  33. * Options for AutoReleaseWorkerPool
  34. */
  35. export interface AutoReleaseWorkerPoolOptions {
  36. /**
  37. * Idle time elapsed before workers are terminated.
  38. */
  39. idleTimeElapsedBeforeRelease: number;
  40. }
  41. /**
  42. * Similar to the WorkerPool class except it creates and destroys workers automatically with a maximum of `maxWorkers` workers.
  43. * Workers are terminated when it is idle for at least `idleTimeElapsedBeforeRelease` milliseconds.
  44. */
  45. export declare class AutoReleaseWorkerPool extends WorkerPool {
  46. /**
  47. * Default options for the constructor.
  48. * Override to change the defaults.
  49. */
  50. static DefaultOptions: AutoReleaseWorkerPoolOptions;
  51. private readonly _maxWorkers;
  52. private readonly _createWorkerAsync;
  53. private readonly _options;
  54. constructor(maxWorkers: number, createWorkerAsync: () => Promise<Worker>, options?: AutoReleaseWorkerPoolOptions);
  55. push(action: (worker: Worker, onComplete: () => void) => void): void;
  56. protected _execute(workerInfo: WorkerInfo, action: (worker: Worker, onComplete: () => void) => void): void;
  57. }
  58. export {};