Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #246 on branch 2.x (Add optional origin to transaction, filter out 'silent-change' origin) #291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion javascript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ export interface ISharedBase extends IObservableDisposable {
*
* @param f Transaction to execute
* @param undoable Whether to track the change in the action history or not (default `true`)
* @param origin Transaction origin
*/
transact(f: () => void, undoable?: boolean): void;
transact(f: () => void, undoable?: boolean, origin?: any): void;
}

/**
Expand Down
30 changes: 21 additions & 9 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,14 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
*
* @param f Transaction to execute
* @param undoable Whether to track the change in the action history or not (default `true`)
* @param origin Transaction origin; if set to 'silent-change' and {@link undoable} is false,
* it won't emit model {@link changed}.
*/
transact(f: () => void, undoable = true): void {
transact(f: () => void, undoable = true, origin: any = null): void {
!this.notebook || this.notebook.disableDocumentWideUndoRedo
? this.ymodel.doc == null
? f()
: this.ymodel.doc.transact(f, undoable ? this : null)
: this.ymodel.doc.transact(f, undoable ? this : origin)
: this.notebook.transact(f, undoable);
}

Expand Down Expand Up @@ -656,8 +658,13 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
/**
* Handle a change to the ymodel.
*/
private _modelObserver = (events: Y.YEvent<any>[]) => {
this._changed.emit(this.getChanges(events));
private _modelObserver = (
events: Y.YEvent<any>[],
transaction: Y.Transaction
) => {
if (transaction.origin !== 'silent-change') {
this._changed.emit(this.getChanges(events));
}
};

protected _metadataChanged = new Signal<this, IMapChange>(this);
Expand Down Expand Up @@ -783,14 +790,19 @@ export class YCodeCell
updateOutputs(
start: number,
end: number,
outputs: Array<nbformat.IOutput> = []
outputs: Array<nbformat.IOutput> = [],
origin: any = null
): void {
const fin =
end < this._youtputs.length ? end - start : this._youtputs.length - start;
this.transact(() => {
this._youtputs.delete(start, fin);
this._youtputs.insert(start, outputs);
}, false);
this.transact(
() => {
this._youtputs.delete(start, fin);
this._youtputs.insert(start, outputs);
},
false,
origin
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions javascript/src/ydocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,13 @@ export abstract class YDocument<T extends DocumentChange>
/**
* Perform a transaction. While the function f is called, all changes to the shared
* document are bundled into a single event.
*
* @param f Transaction to execute
* @param undoable Whether to track the change in the action history or not (default `true`)
* @param origin Transaction origin
*/
transact(f: () => void, undoable = true): void {
this.ydoc.transact(f, undoable ? this : null);
transact(f: () => void, undoable = true, origin: any = null): void {
this.ydoc.transact(f, undoable ? this : origin);
}

/**
Expand Down
Loading