Skip to content
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

Add support for async model creation #22

Merged
merged 2 commits into from
Feb 7, 2025
Merged
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
32 changes: 29 additions & 3 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapChange } from '@jupyter/ydoc';
import { JSONExt, JSONObject } from '@lumino/coreutils';
import { JSONExt, JSONObject, PromiseDelegate } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import * as Y from 'yjs';

Expand All @@ -8,8 +8,9 @@ import { IJupyterYDoc, IJupyterYModel } from './types';
export class JupyterYModel implements IJupyterYModel {
constructor(commMetadata: { [key: string]: any }) {
this._yModelName = commMetadata.ymodel_name;
const ydoc = this.ydocFactory(commMetadata);
this._sharedModel = new JupyterYDoc(commMetadata, ydoc);
this.initialize(commMetadata).then(() => {
this._ready.resolve();
});
}

get yModelName(): string {
Expand All @@ -20,6 +21,18 @@ export class JupyterYModel implements IJupyterYModel {
return this._sharedModel;
}

get ydoc(): Y.Doc {
return this._ydoc;
}

protected set sharedModel(value: IJupyterYDoc) {
this._sharedModel = value;
}

protected set ydoc(value: Y.Doc) {
this._ydoc = value;
}

get sharedAttrsChanged(): ISignal<IJupyterYDoc, MapChange> {
return this.sharedModel.attrsChanged;
}
Expand All @@ -32,6 +45,15 @@ export class JupyterYModel implements IJupyterYModel {
return this._isDisposed;
}

get ready(): Promise<void> {
return this._ready.promise;
}

protected async initialize(commMetadata: { [key: string]: any }) {
this.ydoc = this.ydocFactory(commMetadata);
this.sharedModel = new JupyterYDoc(commMetadata, this._ydoc);
}

ydocFactory(commMetadata: { [key: string]: any }): Y.Doc {
return new Y.Doc();
}
Expand All @@ -54,11 +76,15 @@ export class JupyterYModel implements IJupyterYModel {
this.sharedModel.removeAttr(key);
}

private _ydoc: Y.Doc;

private _yModelName: string;
private _sharedModel: IJupyterYDoc;

private _isDisposed = false;

private _ready: PromiseDelegate<void> = new PromiseDelegate<void>();

private _disposed = new Signal<this, void>(this);
}

Expand Down
4 changes: 4 additions & 0 deletions src/notebookrenderer/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ export class JupyterYWidget extends Widget implements IRenderMime.IRenderer {
this._yModel?.dispose();
super.dispose();
}

async renderModel(mimeModel: IRenderMime.IMimeModel): Promise<void> {
const modelId = mimeModel.data[this._mimeType]!['model_id'];

this._yModel = this._modelFactory.getYModel(modelId);
if (!this._yModel) {
return;
}

await this._yModel.ready;

this._ywidget = this._modelFactory.createYWidget(modelId, this.node);
}

Expand Down
2 changes: 2 additions & 0 deletions src/notebookrenderer/widgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class WidgetModelRegistry implements IJupyterYWidgetModelRegistry {
);
const yModel: IJupyterYModel = new yModelFactory(msg.metadata);

await yModel.ready;

new YCommProvider({
comm,
ydoc: yModel.sharedModel.ydoc
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ export interface IJupyterYModel extends IDisposable {
sharedAttrsChanged: ISignal<IJupyterYDoc, MapChange>;

disposed: ISignal<any, void>;

ready: Promise<void>;
}