11import { MapChange } from '@jupyter/ydoc' ;
2- import { JSONExt , JSONObject } from '@lumino/coreutils' ;
2+ import { JSONExt , JSONObject , PromiseDelegate } from '@lumino/coreutils' ;
33import { ISignal , Signal } from '@lumino/signaling' ;
44import * as Y from 'yjs' ;
55
@@ -8,8 +8,9 @@ import { IJupyterYDoc, IJupyterYModel } from './types';
88export 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
0 commit comments