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

Add optional origin to transaction, filter out 'modeldb' origin #246

Merged
merged 1 commit into from
Jun 28, 2024
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
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
Loading