Skip to content

Commit c9db1e7

Browse files
authored
Merge pull request #142 from trungleduc/ydoc-override
Update `YDocument` constructor
2 parents bfc1437 + 132d9d0 commit c9db1e7

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

javascript/src/ydocument.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ import type { DocumentChange, ISharedDocument, StateChange } from './api.js';
1313
* Generic shareable document.
1414
*/
1515
export class YDocument<T extends DocumentChange> implements ISharedDocument {
16-
constructor() {
17-
this.ystate.observe(this.onStateChanged);
16+
constructor(options?: YDocument.IOptions) {
17+
this._ydoc = options?.ydoc ?? new Y.Doc();
18+
19+
this._ystate = this._ydoc.getMap('state');
20+
21+
this._undoManager = new Y.UndoManager([], {
22+
trackedOrigins: new Set([this]),
23+
doc: this._ydoc
24+
});
25+
26+
this._awareness = new Awareness(this._ydoc);
27+
28+
this._ystate.observe(this.onStateChanged);
1829
}
1930

2031
/**
@@ -25,27 +36,32 @@ export class YDocument<T extends DocumentChange> implements ISharedDocument {
2536
}
2637

2738
/**
28-
* YJS document
39+
* YJS document.
2940
*/
30-
readonly ydoc = new Y.Doc();
41+
get ydoc(): Y.Doc {
42+
return this._ydoc;
43+
}
3144

3245
/**
3346
* Shared state
3447
*/
35-
readonly ystate: Y.Map<any> = this.ydoc.getMap('state');
48+
get ystate(): Y.Map<any> {
49+
return this._ystate;
50+
}
3651

3752
/**
3853
* YJS document undo manager
3954
*/
40-
readonly undoManager = new Y.UndoManager([], {
41-
trackedOrigins: new Set([this]),
42-
doc: this.ydoc
43-
});
55+
get undoManager(): Y.UndoManager {
56+
return this._undoManager;
57+
}
4458

4559
/**
4660
* Shared awareness
4761
*/
48-
readonly awareness = new Awareness(this.ydoc);
62+
get awareness(): Awareness {
63+
return this._awareness;
64+
}
4965

5066
/**
5167
* The changed signal.
@@ -178,6 +194,19 @@ export class YDocument<T extends DocumentChange> implements ISharedDocument {
178194
};
179195

180196
protected _changed = new Signal<this, T>(this);
197+
private _ydoc: Y.Doc;
198+
private _ystate: Y.Map<any>;
199+
private _undoManager: Y.UndoManager;
200+
private _awareness: Awareness;
181201
private _isDisposed = false;
182202
private _disposed = new Signal<this, void>(this);
183203
}
204+
205+
namespace YDocument {
206+
export interface IOptions {
207+
/**
208+
* The optional YJS document for YDocument.
209+
*/
210+
ydoc?: Y.Doc;
211+
}
212+
}

0 commit comments

Comments
 (0)