@@ -7,7 +7,7 @@ import { JSONExt, JSONObject, JSONValue } from '@lumino/coreutils';
7
7
import { ISignal , Signal } from '@lumino/signaling' ;
8
8
import { Awareness } from 'y-protocols/awareness' ;
9
9
import * as Y from 'yjs' ;
10
- import type { DocumentChange , ISharedDocument , StateChange } from './api.js ' ;
10
+ import type { DocumentChange , IDocumentProvider , ISharedDocument , StateChange } from './api' ;
11
11
12
12
/**
13
13
* Generic shareable document.
@@ -28,13 +28,37 @@ export abstract class YDocument<T extends DocumentChange>
28
28
this . _awareness = new Awareness ( this . _ydoc ) ;
29
29
30
30
this . _ystate . observe ( this . onStateChanged ) ;
31
+
32
+ this . _providers = { } ;
31
33
}
32
34
33
35
/**
34
36
* Document version
35
37
*/
36
38
abstract readonly version : string ;
37
39
40
+ addFork ( forkId : string ) {
41
+ this . ystate . set ( `fork_${ forkId } ` , 'new' ) ;
42
+ }
43
+
44
+ getProvider ( providerId : string , sharedModel ?: ISharedDocument ) : IDocumentProvider {
45
+ if ( ! ( providerId in this . _providers ) ) {
46
+ if ( providerId === 'root' ) {
47
+ throw new Error ( 'Cannot get a new provider for root document' ) ;
48
+ }
49
+ if ( sharedModel === undefined ) {
50
+ throw new Error ( 'New provider needs a shared document' ) ;
51
+ }
52
+ const root_provider = this . _providers [ 'root' ] ;
53
+ this . _providers [ providerId ] = root_provider . connectFork ( providerId , sharedModel ! ) ;
54
+ }
55
+ return this . _providers [ providerId ] ;
56
+ }
57
+
58
+ setProvider ( providerId : string , provider : IDocumentProvider ) {
59
+ this . _providers [ providerId ] = provider ;
60
+ }
61
+
38
62
/**
39
63
* YJS document.
40
64
*/
@@ -200,6 +224,7 @@ export abstract class YDocument<T extends DocumentChange>
200
224
private _awareness : Awareness ;
201
225
private _isDisposed = false ;
202
226
private _disposed = new Signal < this, void > ( this ) ;
227
+ private _providers : { [ key : string ] : IDocumentProvider } ;
203
228
}
204
229
205
230
/**
0 commit comments