Skip to content

Commit

Permalink
Add optional origin to transaction, and don't emit changes from 'mode…
Browse files Browse the repository at this point in the history
…ldb' origin
  • Loading branch information
davidbrochart committed Jun 27, 2024
1 parent b3905b3 commit 9a6072a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion javascript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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`)
*/
transact(f: () => void, undoable?: boolean): void;
transact(f: () => void, undoable?: boolean, origin?: any): void;
}

/**
Expand Down
30 changes: 20 additions & 10 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,11 @@ 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`)
*/
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 +656,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 !== 'modeldb') {
this._changed.emit(this.getChanges(events));
}
};

protected _metadataChanged = new Signal<this, IMapChange>(this);
Expand Down Expand Up @@ -838,15 +843,20 @@ 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);
const newOutputs = this.createOutputs(outputs);
this._youtputs.insert(start, newOutputs);
}, false);
this.transact(
() => {
this._youtputs.delete(start, fin);
const newOutputs = this.createOutputs(outputs);
this._youtputs.insert(start, newOutputs);
},
false,
origin
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions javascript/src/ydocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ 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.
*/
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 9a6072a

Please sign in to comment.