Skip to content

Commit

Permalink
Use postMessage instead of a comlink produced MessageChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
martenrichter committed Dec 15, 2024
1 parent 9a9c722 commit 19bd184
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/pyodide-kernel/src/comlink.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class PyodideDriveFS extends DriveFS {
}

export class PyodideComlinkKernel extends PyodideRemoteKernel {
constructor() {
super();
this._sendWorkerMessage = (msg: any) => {
// use postMessage, but in a format, that comlink would not process.
postMessage({ jMsg: msg });
};
}

/**
* Setup custom Emscripten FileSystem
*/
Expand Down
10 changes: 8 additions & 2 deletions packages/pyodide-kernel/src/kernel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import coincident from 'coincident';

import { Remote, proxy, wrap } from 'comlink';
import { Remote, wrap } from 'comlink';

import { PromiseDelegate } from '@lumino/coreutils';

Expand Down Expand Up @@ -86,7 +86,13 @@ export class PyodideKernel extends BaseKernel implements IKernel {
};
} else {
remote = wrap(this._worker) as IPyodideWorkerKernel;
remote.registerCallback(proxy(this._processWorkerMessage.bind(this)));
// we use the normal postMessage mechanism
this._worker.addEventListener('message', (ev) => {
if (typeof ev?.data?.jMsg !== 'undefined') {
// only process non comlink messages
this._processWorkerMessage(ev.data.jMsg);
}
});
}
const remoteOptions = this.initRemoteOptions(options);
remote.initialize(remoteOptions).then(this._ready.resolve.bind(this._ready));
Expand Down

0 comments on commit 19bd184

Please sign in to comment.