diff --git a/src/model.ts b/src/model.ts index 8cdf613..52455da 100644 --- a/src/model.ts +++ b/src/model.ts @@ -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'; @@ -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 { @@ -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 { return this.sharedModel.attrsChanged; } @@ -32,6 +45,15 @@ export class JupyterYModel implements IJupyterYModel { return this._isDisposed; } + get ready(): Promise { + 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(); } @@ -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 = new PromiseDelegate(); + private _disposed = new Signal(this); } diff --git a/src/notebookrenderer/view.ts b/src/notebookrenderer/view.ts index a053413..b7d915f 100644 --- a/src/notebookrenderer/view.ts +++ b/src/notebookrenderer/view.ts @@ -30,6 +30,7 @@ export class JupyterYWidget extends Widget implements IRenderMime.IRenderer { this._yModel?.dispose(); super.dispose(); } + async renderModel(mimeModel: IRenderMime.IMimeModel): Promise { const modelId = mimeModel.data[this._mimeType]!['model_id']; @@ -37,6 +38,9 @@ export class JupyterYWidget extends Widget implements IRenderMime.IRenderer { if (!this._yModel) { return; } + + await this._yModel.ready; + this._ywidget = this._modelFactory.createYWidget(modelId, this.node); } diff --git a/src/notebookrenderer/widgetManager.ts b/src/notebookrenderer/widgetManager.ts index ea7cb13..ad2d219 100644 --- a/src/notebookrenderer/widgetManager.ts +++ b/src/notebookrenderer/widgetManager.ts @@ -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 diff --git a/src/types.ts b/src/types.ts index 35190db..1fbd7c4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,4 +30,6 @@ export interface IJupyterYModel extends IDisposable { sharedAttrsChanged: ISignal; disposed: ISignal; + + ready: Promise; }