Skip to content

Commit 814b213

Browse files
authored
Add support for async model creation (#22)
* Add support for async model creation * Backward compat
1 parent 70e5a1c commit 814b213

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/model.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MapChange } from '@jupyter/ydoc';
2-
import { JSONExt, JSONObject } from '@lumino/coreutils';
2+
import { JSONExt, JSONObject, PromiseDelegate } from '@lumino/coreutils';
33
import { ISignal, Signal } from '@lumino/signaling';
44
import * as Y from 'yjs';
55

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

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

24+
get ydoc(): Y.Doc {
25+
return this._ydoc;
26+
}
27+
28+
protected set sharedModel(value: IJupyterYDoc) {
29+
this._sharedModel = value;
30+
}
31+
32+
protected set ydoc(value: Y.Doc) {
33+
this._ydoc = value;
34+
}
35+
2336
get sharedAttrsChanged(): ISignal<IJupyterYDoc, MapChange> {
2437
return this.sharedModel.attrsChanged;
2538
}
@@ -32,6 +45,15 @@ export class JupyterYModel implements IJupyterYModel {
3245
return this._isDisposed;
3346
}
3447

48+
get ready(): Promise<void> {
49+
return this._ready.promise;
50+
}
51+
52+
protected async initialize(commMetadata: { [key: string]: any }) {
53+
this.ydoc = this.ydocFactory(commMetadata);
54+
this.sharedModel = new JupyterYDoc(commMetadata, this._ydoc);
55+
}
56+
3557
ydocFactory(commMetadata: { [key: string]: any }): Y.Doc {
3658
return new Y.Doc();
3759
}
@@ -54,11 +76,15 @@ export class JupyterYModel implements IJupyterYModel {
5476
this.sharedModel.removeAttr(key);
5577
}
5678

79+
private _ydoc: Y.Doc;
80+
5781
private _yModelName: string;
5882
private _sharedModel: IJupyterYDoc;
5983

6084
private _isDisposed = false;
6185

86+
private _ready: PromiseDelegate<void> = new PromiseDelegate<void>();
87+
6288
private _disposed = new Signal<this, void>(this);
6389
}
6490

src/notebookrenderer/view.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ export class JupyterYWidget extends Widget implements IRenderMime.IRenderer {
3030
this._yModel?.dispose();
3131
super.dispose();
3232
}
33+
3334
async renderModel(mimeModel: IRenderMime.IMimeModel): Promise<void> {
3435
const modelId = mimeModel.data[this._mimeType]!['model_id'];
3536

3637
this._yModel = this._modelFactory.getYModel(modelId);
3738
if (!this._yModel) {
3839
return;
3940
}
41+
42+
await this._yModel.ready;
43+
4044
this._ywidget = this._modelFactory.createYWidget(modelId, this.node);
4145
}
4246

src/notebookrenderer/widgetManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export class WidgetModelRegistry implements IJupyterYWidgetModelRegistry {
6969
);
7070
const yModel: IJupyterYModel = new yModelFactory(msg.metadata);
7171

72+
await yModel.ready;
73+
7274
new YCommProvider({
7375
comm,
7476
ydoc: yModel.sharedModel.ydoc

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ export interface IJupyterYModel extends IDisposable {
3030
sharedAttrsChanged: ISignal<IJupyterYDoc, MapChange>;
3131

3232
disposed: ISignal<any, void>;
33+
34+
ready: Promise<void>;
3335
}

0 commit comments

Comments
 (0)