Skip to content

Commit

Permalink
Switch from using comlink to coincident
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Jun 7, 2024
1 parent 47323c4 commit 96fec0d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/pyodide-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@jupyterlab/coreutils": "^6.1.1",
"@jupyterlite/contents": "^0.4.0-alpha.3",
"@jupyterlite/kernel": "^0.4.0-alpha.3",
"comlink": "^4.4.1"
"coincident": "^1.2.3"
},
"devDependencies": {
"@babel/core": "^7.22.17",
Expand Down
25 changes: 25 additions & 0 deletions packages/pyodide-kernel/src/coincident.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

/**
* A WebWorker entrypoint that uses coincident to handle postMessage details
*/
import coincident from 'coincident';

import { PyodideRemoteKernel } from './worker';
import { IPyodideWorkerKernel } from './tokens';

const worker = new PyodideRemoteKernel();

const workerAPI: IPyodideWorkerKernel = coincident(self) as IPyodideWorkerKernel;

workerAPI.initialize = worker.initialize.bind(worker);
workerAPI.execute = worker.execute.bind(worker);
workerAPI.complete = worker.complete.bind(worker);
workerAPI.inspect = worker.inspect.bind(worker);
workerAPI.isComplete = worker.isComplete.bind(worker);
workerAPI.commInfo = worker.commInfo.bind(worker);
workerAPI.commOpen = worker.commOpen.bind(worker);
workerAPI.commMsg = worker.commMsg.bind(worker);
workerAPI.commClose = worker.commClose.bind(worker);
workerAPI.inputReply = worker.inputReply.bind(worker);
10 changes: 3 additions & 7 deletions packages/pyodide-kernel/src/comlink.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
// Distributed under the terms of the Modified BSD License.

/**
* A WebWorker entrypoint that uses comlink to handle postMessage details
* A WebWorker entrypoint that uses coincident to handle postMessage details
*/
import { expose } from 'comlink';

import { PyodideRemoteKernel } from './worker';

const worker = new PyodideRemoteKernel();

expose(worker);
// This file is only here for backward compat
export * from './coincident.worker';
6 changes: 3 additions & 3 deletions packages/pyodide-kernel/src/kernel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import coincident from 'coincident';

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

import { PageConfig } from '@jupyterlab/coreutils';
import { KernelMessage } from '@jupyterlab/services';

import { BaseKernel, IKernel } from '@jupyterlite/kernel';

import { wrap } from 'comlink';

import { IPyodideWorkerKernel, IRemotePyodideWorkerKernel } from './tokens';

import { allJSONUrl, pipliteWheelUrl } from './_pypi';
Expand All @@ -24,7 +24,7 @@ export class PyodideKernel extends BaseKernel implements IKernel {
super(options);
this._worker = this.initWorker(options);
this._worker.onmessage = (e) => this._processWorkerMessage(e.data);
this._remoteKernel = wrap(this._worker);
this._remoteKernel = coincident(this._worker) as IPyodideWorkerKernel;
this.initRemote(options);
}

Expand Down
6 changes: 2 additions & 4 deletions packages/pyodide-kernel/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Definitions for the Pyodide kernel.
*/

import type { Remote } from 'comlink';

import { IWorkerKernel } from '@jupyterlite/kernel';

/**
Expand All @@ -25,9 +23,9 @@ export interface IPyodideWorkerKernel extends IWorkerKernel {
}

/**
* An convenience interface for Pyodide workers wrapped by a comlink Remote.
* Deprecated.
*/
export type IRemotePyodideWorkerKernel = Remote<IPyodideWorkerKernel>;
export type IRemotePyodideWorkerKernel = IPyodideWorkerKernel;

/**
* An namespace for Pyodide workers.
Expand Down
2 changes: 1 addition & 1 deletion packages/pyodide-kernel/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export class PyodideRemoteKernel {
*
* @param content The incoming message with the comm target name.
*/
async commInfo(content: any, parent: any) {
async commInfo(content: any, parent: any): Promise<{ comms: any; status: 'ok' }> {
await this.setup(parent);

const res = this._kernel.comm_info(content.target_name);
Expand Down
56 changes: 54 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2983,7 +2983,7 @@ __metadata:
"@jupyterlite/contents": ^0.4.0-alpha.3
"@jupyterlite/kernel": ^0.4.0-alpha.3
"@types/jest": ^29.5.4
comlink: ^4.4.1
coincident: ^1.2.3
esbuild: ^0.19.2
jest: ^29.7.0
pyodide: 0.26.0
Expand Down Expand Up @@ -4463,6 +4463,13 @@ __metadata:
languageName: node
linkType: hard

"@ungap/with-resolvers@npm:^0.1.0":
version: 0.1.0
resolution: "@ungap/with-resolvers@npm:0.1.0"
checksum: ac0c6234414b87ec26e9603c5e57f6ae48477ccc901841e725e820bc026367987cc82dbdb8c702e058179dadbd16578c0fcc607d2122e72d9fe91373bade126c
languageName: node
linkType: hard

"@webassemblyjs/ast@npm:1.11.6, @webassemblyjs/ast@npm:^1.11.5":
version: 1.11.6
resolution: "@webassemblyjs/ast@npm:1.11.6"
Expand Down Expand Up @@ -5705,6 +5712,22 @@ __metadata:
languageName: node
linkType: hard

"coincident@npm:^1.2.3":
version: 1.2.3
resolution: "coincident@npm:1.2.3"
dependencies:
"@ungap/structured-clone": ^1.2.0
"@ungap/with-resolvers": ^0.1.0
gc-hook: ^0.3.1
proxy-target: ^3.0.2
ws: ^8.16.0
dependenciesMeta:
ws:
optional: true
checksum: 21c44d9d74be393b12d6f91c885c8b6c640dce570b01b5d5156327488390c858de8e40c0af08763e3cd7041bcf32bee70c058a1b24aa25b145376a92b1c520a3
languageName: node
linkType: hard

"collect-v8-coverage@npm:^1.0.0":
version: 1.0.2
resolution: "collect-v8-coverage@npm:1.0.2"
Expand Down Expand Up @@ -5779,7 +5802,7 @@ __metadata:
languageName: node
linkType: hard

"comlink@npm:^4.3.1, comlink@npm:^4.4.1":
"comlink@npm:^4.3.1":
version: 4.4.1
resolution: "comlink@npm:4.4.1"
checksum: 16d58a8f590087fc45432e31d6c138308dfd4b75b89aec0b7f7bb97ad33d810381bd2b1e608a1fb2cf05979af9cbfcdcaf1715996d5fcf77aeb013b6da3260af
Expand Down Expand Up @@ -7200,6 +7223,13 @@ __metadata:
languageName: node
linkType: hard

"gc-hook@npm:^0.3.1":
version: 0.3.1
resolution: "gc-hook@npm:0.3.1"
checksum: bda26ab6493faf8593eb8e1ff636c91ba4b38f3d87867ede757b24c4677e4f55ec696977a2b884503e3e56243d5e38abf8cec52257b1fd0685622ec5a3d9c84b
languageName: node
linkType: hard

"gensync@npm:^1.0.0-beta.2":
version: 1.0.0-beta.2
resolution: "gensync@npm:1.0.0-beta.2"
Expand Down Expand Up @@ -10826,6 +10856,13 @@ __metadata:
languageName: node
linkType: hard

"proxy-target@npm:^3.0.2":
version: 3.0.2
resolution: "proxy-target@npm:3.0.2"
checksum: 9d8aa207ce8d3d50273e6926a61bb0205848f2a9de7b6781feeb115072242d9803255351d4da2f2980c573fd2164609257d8c8feaa39b80147867805f2c5d101
languageName: node
linkType: hard

"psl@npm:^1.1.33":
version: 1.9.0
resolution: "psl@npm:1.9.0"
Expand Down Expand Up @@ -13001,6 +13038,21 @@ __metadata:
languageName: node
linkType: hard

"ws@npm:^8.16.0":
version: 8.17.0
resolution: "ws@npm:8.17.0"
peerDependencies:
bufferutil: ^4.0.1
utf-8-validate: ">=5.0.2"
peerDependenciesMeta:
bufferutil:
optional: true
utf-8-validate:
optional: true
checksum: 147ef9eab0251364e1d2c55338ad0efb15e6913923ccbfdf20f7a8a6cb8f88432bcd7f4d8f66977135bfad35575644f9983201c1a361019594a4e53977bf6d4e
languageName: node
linkType: hard

"xml-name-validator@npm:^4.0.0":
version: 4.0.0
resolution: "xml-name-validator@npm:4.0.0"
Expand Down

0 comments on commit 96fec0d

Please sign in to comment.