Skip to content

Commit b922aa0

Browse files
committed
Add optional origin to transaction, and don't emit changes from 'modeldb' origin
1 parent b3905b3 commit b922aa0

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

javascript/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export interface ISharedBase extends IObservableDisposable {
7878
* @param f Transaction to execute
7979
* @param undoable Whether to track the change in the action history or not (default `true`)
8080
*/
81-
transact(f: () => void, undoable?: boolean): void;
81+
transact(f: () => void, undoable?: boolean, origin?: any): void;
8282
}
8383

8484
/**

javascript/src/ycell.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
564564
* @param f Transaction to execute
565565
* @param undoable Whether to track the change in the action history or not (default `true`)
566566
*/
567-
transact(f: () => void, undoable = true): void {
567+
transact(f: () => void, undoable = true, origin: any = null): void {
568568
!this.notebook || this.notebook.disableDocumentWideUndoRedo
569569
? this.ymodel.doc == null
570570
? f()
571-
: this.ymodel.doc.transact(f, undoable ? this : null)
571+
: this.ymodel.doc.transact(f, undoable ? this : origin)
572572
: this.notebook.transact(f, undoable);
573573
}
574574

@@ -656,8 +656,10 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
656656
/**
657657
* Handle a change to the ymodel.
658658
*/
659-
private _modelObserver = (events: Y.YEvent<any>[]) => {
660-
this._changed.emit(this.getChanges(events));
659+
private _modelObserver = (events: Y.YEvent<any>[], transaction: Y.Transaction) => {
660+
if (transaction.origin !== 'modeldb') {
661+
this._changed.emit(this.getChanges(events));
662+
}
661663
};
662664

663665
protected _metadataChanged = new Signal<this, IMapChange>(this);
@@ -838,15 +840,16 @@ export class YCodeCell
838840
updateOutputs(
839841
start: number,
840842
end: number,
841-
outputs: Array<nbformat.IOutput> = []
843+
outputs: Array<nbformat.IOutput> = [],
844+
origin: any = null
842845
): void {
843846
const fin =
844847
end < this._youtputs.length ? end - start : this._youtputs.length - start;
845848
this.transact(() => {
846849
this._youtputs.delete(start, fin);
847850
const newOutputs = this.createOutputs(outputs);
848851
this._youtputs.insert(start, newOutputs);
849-
}, false);
852+
}, false, origin);
850853
}
851854

852855
/**

javascript/src/ydocument.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ export abstract class YDocument<T extends DocumentChange>
170170
* Perform a transaction. While the function f is called, all changes to the shared
171171
* document are bundled into a single event.
172172
*/
173-
transact(f: () => void, undoable = true): void {
174-
this.ydoc.transact(f, undoable ? this : null);
173+
transact(f: () => void, undoable = true, origin: any = null): void {
174+
this.ydoc.transact(f, undoable ? this : origin);
175175
}
176176

177177
/**

0 commit comments

Comments
 (0)