Skip to content

Allow a JupyterYModel to send and receive binary messages #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ requires = [
name = "yjs-widgets"
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.7"
requires-python = ">=3.9"
dependencies = []
classifiers = [
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Framework :: Jupyter",
"Framework :: Jupyter :: JupyterLab",
"Framework :: Jupyter :: JupyterLab :: 3",
Expand Down
10 changes: 10 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JSONExt, JSONObject, PromiseDelegate } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import * as Y from 'yjs';

import { YCommProvider } from './notebookrenderer/yCommProvider';
import { IJupyterYDoc, IJupyterYModel } from './types';

export class JupyterYModel implements IJupyterYModel {
Expand All @@ -13,6 +14,14 @@ export class JupyterYModel implements IJupyterYModel {
});
}

onReceive(callback: (message: Uint8Array) => void) {
this.yCommProvider.onReceive(callback);
}

send(message: Uint8Array) {
this.yCommProvider.send(message);
}

get yModelName(): string {
return this._yModelName;
}
Expand Down Expand Up @@ -76,6 +85,7 @@ export class JupyterYModel implements IJupyterYModel {
this.sharedModel.removeAttr(key);
}

yCommProvider: YCommProvider;
private _ydoc: Y.Doc;

private _yModelName: string;
Expand Down
3 changes: 2 additions & 1 deletion src/notebookrenderer/widgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export class WidgetModelRegistry implements IJupyterYWidgetModelRegistry {

await yModel.ready;

new YCommProvider({
const yCommProvider = new YCommProvider({
comm,
ydoc: yModel.sharedModel.ydoc
});
this._yModels.set(comm.commId, yModel);
yModel.yCommProvider = yCommProvider;
};

private _yModels: Map<string, IJupyterYModel> = new Map();
Expand Down
23 changes: 20 additions & 3 deletions src/notebookrenderer/yCommProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export class YCommProvider implements IDisposable {
this._connect();
}

onReceive(callback: (message: Uint8Array) => void) {
this._onReceive = callback;
}

send(message: Uint8Array) {
const messageType = new Uint8Array([2]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exported constant for this magic value might be cleaner

const msg = new Uint8Array([...messageType, ...message]);
this._sendOverComm(msg);
}

get doc(): Y.Doc {
return this._ydoc;
}
Expand Down Expand Up @@ -47,9 +57,15 @@ export class YCommProvider implements IDisposable {
const buffer_uint8 = new Uint8Array(
ArrayBuffer.isView(buffer) ? buffer.buffer : buffer
);
const encoder = Private.readMessage(this, buffer_uint8, true);
if (encoding.length(encoder) > 1) {
this._sendOverComm(encoding.toUint8Array(encoder));
if (buffer_uint8.slice(0, 1)[0] === 2) {
if (this._onReceive !== null) {
this._onReceive(buffer_uint8.slice(1));
}
} else {
const encoder = Private.readMessage(this, buffer_uint8, true);
if (encoding.length(encoder) > 1) {
this._sendOverComm(encoding.toUint8Array(encoder));
}
}
}
};
Expand Down Expand Up @@ -81,6 +97,7 @@ export class YCommProvider implements IDisposable {
private _ydoc: Y.Doc;
private _synced: boolean;
private _isDisposed = false;
private _onReceive: ((message: Uint8Array) => void) | null = null;
}

namespace Private {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Y from 'yjs';
import { ISignal } from '@lumino/signaling';
import { JSONObject } from '@lumino/coreutils';
import { IDisposable } from '@lumino/disposable';
import { YCommProvider } from './notebookrenderer/yCommProvider';

export interface IJupyterYDocChange {
attrsChange?: MapChange;
Expand All @@ -26,10 +27,14 @@ export interface IJupyterYModel extends IDisposable {
yModelName: string;
isDisposed: boolean;
sharedModel: IJupyterYDoc;
yCommProvider: YCommProvider;

sharedAttrsChanged: ISignal<IJupyterYDoc, MapChange>;

disposed: ISignal<any, void>;

ready: Promise<void>;

send(message: Uint8Array);
onReceive(callback: (message: Uint8Array) => void);
}