Skip to content

Commit

Permalink
fix: enable connecting to SharedWorker instances
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 23, 2024
1 parent 363e7f7 commit 7f03b82
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/experiments/src/data-worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ import type Store from '@ember-data/store';

import type { AbortEventData, RequestEventData, ThreadInitEventData, WorkerThreadEvent } from './types';

const WorkerScope = (globalThis as unknown as { SharedWorkerGlobalScope: FunctionConstructor }).SharedWorkerGlobalScope;

export class DataWorker {
declare store: Store;
declare threads: Map<string, MessagePort>;
declare pending: Map<string, Map<number, Future<unknown>>>;
declare isSharedWorker: boolean;

constructor(UserStore: typeof Store) {
(globalThis as unknown as { name: string }).name = 'WarpDrive DataWorker';
this.store = new UserStore();
this.threads = new Map();
this.pending = new Map();
this.initialize();
this.isSharedWorker = globalThis instanceof WorkerScope;
}

initialize() {
globalThis.onmessage = (event: MessageEvent<ThreadInitEventData>) => {
const fn = (event: MessageEvent<ThreadInitEventData>) => {
const { type } = event.data;

switch (type) {
Expand All @@ -25,6 +30,12 @@ export class DataWorker {
break;
}
};

if (this.isSharedWorker) {
(globalThis as unknown as { onconnect: typeof globalThis.onmessage }).onconnect = fn;
} else {
globalThis.onmessage = fn;
}
}

setupThread(thread: string, port: MessagePort) {
Expand Down

0 comments on commit 7f03b82

Please sign in to comment.