Skip to content

Commit

Permalink
feat(zkappWorkerClient.ts): initialize worker client and wrap with co…
Browse files Browse the repository at this point in the history
…mlink in constructor
  • Loading branch information
ymekuria committed Sep 19, 2024
1 parent 05948b1 commit 84b3dcc
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export default class ZkappWorkerClient {
// Proxy to interact with the worker's methods as if they were local
remoteApi: Comlink.Remote<typeof import('./zkappWorker').api>;

constructor() {
// Initialize the worker from the zkappWorker module
const worker = new Worker(new URL('./zkappWorker.ts', import.meta.url), { type: 'module' });
// Wrap the worker with Comlink to enable direct method invocation
this.remoteApi = Comlink.wrap(worker);
}

setActiveInstanceToDevnet() {
return this._call('setActiveInstanceToDevnet', {});
}
Expand Down Expand Up @@ -70,16 +77,6 @@ export default class ZkappWorkerClient {

nextId: number;

constructor() {
this.worker = new Worker(new URL('./zkappWorker.ts', import.meta.url));
this.promises = {};
this.nextId = 0;

this.worker.onmessage = (event: MessageEvent<ZkappWorkerReponse>) => {
this.promises[event.data.id].resolve(event.data.data);
delete this.promises[event.data.id];
};
}

_call(fn: WorkerFunctions, args: any) {
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 84b3dcc

Please sign in to comment.