Skip to content

Commit

Permalink
Backport PR jupyter-server#246: Add optional origin to transaction, f…
Browse files Browse the repository at this point in the history
…ilter out 'modeldb' origin
  • Loading branch information
Zsailer authored and fcollonval committed Nov 29, 2024
1 parent 8f8a1ca commit 94bc1d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
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

0 comments on commit 94bc1d0

Please sign in to comment.