comlinkWorker.ts 1013 B

12345678910111213141516171819202122232425262728293031
  1. import { Remote, wrap } from 'comlink';
  2. // import { WorkerElectronCacheStorageClient } from 'services/workerElectronCache/client';
  3. import { addLocalLog } from '@ente/shared/logging';
  4. export class ComlinkWorker<T extends new () => InstanceType<T>> {
  5. public remote: Promise<Remote<InstanceType<T>>>;
  6. private worker: Worker;
  7. private name: string;
  8. constructor(name: string, worker: Worker) {
  9. this.name = name;
  10. this.worker = worker;
  11. this.worker.onerror = (errorEvent) => {
  12. console.error('Got error event from worker', errorEvent);
  13. };
  14. addLocalLog(() => `Initiated ${this.name}`);
  15. const comlink = wrap<T>(this.worker);
  16. this.remote = new comlink() as Promise<Remote<InstanceType<T>>>;
  17. // expose(WorkerElectronCacheStorageClient, this.worker);
  18. }
  19. public getName() {
  20. return this.name;
  21. }
  22. public terminate() {
  23. this.worker.terminate();
  24. addLocalLog(() => `Terminated ${this.name}`);
  25. }
  26. }