Skip to content

Commit

Permalink
Merge pull request #16 from jupyterlite/comlink_callback
Browse files Browse the repository at this point in the history
Replace postMessage from webworker with comlink callback
  • Loading branch information
ianthomas23 committed Jul 16, 2024
2 parents f5c7dad + 9d5cfe1 commit 1841226
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
24 changes: 9 additions & 15 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

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

import { proxy, wrap } from 'comlink';

import {
Server as WebSocketServer,
Client as WebSocketClient
} from 'mock-socket';

import { wrap } from 'comlink';

import { ITerminal, IRemoteWorkerTerminal } from './tokens';

export class Terminal implements ITerminal {
Expand All @@ -28,17 +28,13 @@ export class Terminal implements ITerminal {
this._remote = wrap(this._worker);
const { baseUrl } = this.options;
await this._remote.initialize({ baseUrl });
this._remote.registerCallbacks(proxy(this._outputCallback.bind(this)));
}

/**
* Process a message coming from the JavaScript web worker.
*
* @param msg The worker message to process.
*/
private _processWorkerMessage(msg: any, socket: WebSocketClient): void {
if (msg.type === 'output') {
const ret = JSON.stringify(['stdout', msg.text]);
socket.send(ret);
private async _outputCallback(text: string): Promise<void> {
if (this._socket) {
const ret = JSON.stringify(['stdout', text]);
this._socket.send(ret);
}
}

Expand All @@ -56,10 +52,7 @@ export class Terminal implements ITerminal {

server.on('connection', async (socket: WebSocketClient) => {
console.log('==> server connection', this, socket);

this._worker!.onmessage = e => {
this._processWorkerMessage(e.data, socket);
};
this._socket = socket;

socket.on('message', async (message: any) => {
const data = JSON.parse(message) as JSONPrimitive[];
Expand Down Expand Up @@ -95,4 +88,5 @@ export class Terminal implements ITerminal {

private _worker?: Worker;
private _remote?: IRemoteWorkerTerminal;
private _socket?: WebSocketClient;
}
10 changes: 9 additions & 1 deletion src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import { TerminalAPI } from '@jupyterlab/services';

import { IOutputCallback } from '@jupyterlite/cockle';

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

import { Remote } from 'comlink';
import { ProxyMarked, Remote } from 'comlink';

/**
* The token for the Terminals service.
Expand Down Expand Up @@ -71,11 +73,17 @@ export namespace IWorkerTerminal {
}
}

export namespace IRemote {
export type OutputCallback = IOutputCallback & ProxyMarked;
}

export interface IRemote extends IWorkerTerminal {
/**
* Handle any lazy initialization activities.
*/
initialize(options: IWorkerTerminal.IOptions): Promise<void>;

registerCallbacks(outputCallback: IRemote.OutputCallback): void;
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DriveFS } from '@jupyterlite/contents';

import { expose } from 'comlink';

import { IWorkerTerminal } from './tokens';
import { IRemote, IWorkerTerminal } from './tokens';

class WorkerTerminal implements IWorkerTerminal {
async initialize(options: IWorkerTerminal.IOptions): Promise<void> {
Expand All @@ -15,12 +15,16 @@ class WorkerTerminal implements IWorkerTerminal {
await this._shell!.input(text);
}

registerCallbacks(outputCallback: IRemote.OutputCallback) {
this._outputCallback = outputCallback;
}

async setSize(rows: number, columns: number): Promise<void> {
await this._shell!.setSize(rows, columns);
}

async start(): Promise<void> {
this._shell = new Shell(this.output, this._mountpoint);
this._shell = new Shell(this.output.bind(this), this._mountpoint);
const { FS, PATH, ERRNO_CODES } = await this._shell.initFilesystem();

if (this._wantDriveFS) {
Expand All @@ -45,17 +49,18 @@ class WorkerTerminal implements IWorkerTerminal {
}

private async output(text: string): Promise<void> {
postMessage({
type: 'output',
text: text
});
if (this._outputCallback) {
await this._outputCallback(text);
}
}

private _options: IWorkerTerminal.IOptions | null = null;
private _shell?: Shell;
private _mountpoint: string = '/drive';
private _wantDriveFS: boolean = true;
private _driveFS?: DriveFS;

private _outputCallback?: IRemote.OutputCallback;
}

const obj = new WorkerTerminal();
Expand Down

0 comments on commit 1841226

Please sign in to comment.