Skip to content

Commit 94bc1d0

Browse files
Zsailerfcollonval
authored andcommitted
Backport PR jupyter-server#246: Add optional origin to transaction, filter out 'modeldb' origin
1 parent 8f8a1ca commit 94bc1d0

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

javascript/src/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ export interface ISharedBase extends IObservableDisposable {
7777
*
7878
* @param f Transaction to execute
7979
* @param undoable Whether to track the change in the action history or not (default `true`)
80+
* @param origin Transaction origin
8081
*/
81-
transact(f: () => void, undoable?: boolean): void;
82+
transact(f: () => void, undoable?: boolean, origin?: any): void;
8283
}
8384

8485
/**

javascript/src/ycell.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,14 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
563563
*
564564
* @param f Transaction to execute
565565
* @param undoable Whether to track the change in the action history or not (default `true`)
566+
* @param origin Transaction origin; if set to 'silent-change' and {@link undoable} is false,
567+
* it won't emit model {@link changed}.
566568
*/
567-
transact(f: () => void, undoable = true): void {
569+
transact(f: () => void, undoable = true, origin: any = null): void {
568570
!this.notebook || this.notebook.disableDocumentWideUndoRedo
569571
? this.ymodel.doc == null
570572
? f()
571-
: this.ymodel.doc.transact(f, undoable ? this : null)
573+
: this.ymodel.doc.transact(f, undoable ? this : origin)
572574
: this.notebook.transact(f, undoable);
573575
}
574576

@@ -656,8 +658,13 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
656658
/**
657659
* Handle a change to the ymodel.
658660
*/
659-
private _modelObserver = (events: Y.YEvent<any>[]) => {
660-
this._changed.emit(this.getChanges(events));
661+
private _modelObserver = (
662+
events: Y.YEvent<any>[],
663+
transaction: Y.Transaction
664+
) => {
665+
if (transaction.origin !== 'silent-change') {
666+
this._changed.emit(this.getChanges(events));
667+
}
661668
};
662669

663670
protected _metadataChanged = new Signal<this, IMapChange>(this);
@@ -783,14 +790,19 @@ export class YCodeCell
783790
updateOutputs(
784791
start: number,
785792
end: number,
786-
outputs: Array<nbformat.IOutput> = []
793+
outputs: Array<nbformat.IOutput> = [],
794+
origin: any = null
787795
): void {
788796
const fin =
789797
end < this._youtputs.length ? end - start : this._youtputs.length - start;
790-
this.transact(() => {
791-
this._youtputs.delete(start, fin);
792-
this._youtputs.insert(start, outputs);
793-
}, false);
798+
this.transact(
799+
() => {
800+
this._youtputs.delete(start, fin);
801+
this._youtputs.insert(start, outputs);
802+
},
803+
false,
804+
origin
805+
);
794806
}
795807

796808
/**

javascript/src/ydocument.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ export abstract class YDocument<T extends DocumentChange>
169169
/**
170170
* Perform a transaction. While the function f is called, all changes to the shared
171171
* document are bundled into a single event.
172+
*
173+
* @param f Transaction to execute
174+
* @param undoable Whether to track the change in the action history or not (default `true`)
175+
* @param origin Transaction origin
172176
*/
173-
transact(f: () => void, undoable = true): void {
174-
this.ydoc.transact(f, undoable ? this : null);
177+
transact(f: () => void, undoable = true, origin: any = null): void {
178+
this.ydoc.transact(f, undoable ? this : origin);
175179
}
176180

177181
/**

0 commit comments

Comments
 (0)