Skip to content

Commit

Permalink
Merge pull request #142 from trungleduc/ydoc-override
Browse files Browse the repository at this point in the history
Update `YDocument` constructor
  • Loading branch information
hbcarlos authored Feb 17, 2023
2 parents bfc1437 + 132d9d0 commit c9db1e7
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions javascript/src/ydocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ import type { DocumentChange, ISharedDocument, StateChange } from './api.js';
* Generic shareable document.
*/
export class YDocument<T extends DocumentChange> implements ISharedDocument {
constructor() {
this.ystate.observe(this.onStateChanged);
constructor(options?: YDocument.IOptions) {
this._ydoc = options?.ydoc ?? new Y.Doc();

this._ystate = this._ydoc.getMap('state');

this._undoManager = new Y.UndoManager([], {
trackedOrigins: new Set([this]),
doc: this._ydoc
});

this._awareness = new Awareness(this._ydoc);

this._ystate.observe(this.onStateChanged);
}

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

/**
* YJS document
* YJS document.
*/
readonly ydoc = new Y.Doc();
get ydoc(): Y.Doc {
return this._ydoc;
}

/**
* Shared state
*/
readonly ystate: Y.Map<any> = this.ydoc.getMap('state');
get ystate(): Y.Map<any> {
return this._ystate;
}

/**
* YJS document undo manager
*/
readonly undoManager = new Y.UndoManager([], {
trackedOrigins: new Set([this]),
doc: this.ydoc
});
get undoManager(): Y.UndoManager {
return this._undoManager;
}

/**
* Shared awareness
*/
readonly awareness = new Awareness(this.ydoc);
get awareness(): Awareness {
return this._awareness;
}

/**
* The changed signal.
Expand Down Expand Up @@ -178,6 +194,19 @@ export class YDocument<T extends DocumentChange> implements ISharedDocument {
};

protected _changed = new Signal<this, T>(this);
private _ydoc: Y.Doc;
private _ystate: Y.Map<any>;
private _undoManager: Y.UndoManager;
private _awareness: Awareness;
private _isDisposed = false;
private _disposed = new Signal<this, void>(this);
}

namespace YDocument {
export interface IOptions {
/**
* The optional YJS document for YDocument.
*/
ydoc?: Y.Doc;
}
}

0 comments on commit c9db1e7

Please sign in to comment.