Skip to content

Commit

Permalink
update Y.Text name in a transactional way
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-i committed Dec 19, 2024
1 parent 66a7305 commit cc9c0b0
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/workflows/workflowDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ export class WorkflowDoc
super();

this._name = this.ydoc.getText('name');
this._previousName = this._name.toString();
this._name.observe(this._nameObserver);
}

private _nameObserver = (event: Y.YTextEvent): void => {
this._nameChanged.emit({ newValue: 'WOW NEW VALUE' });
const oldValue = this._previousName;
const newValue = this._name.toString();

this._previousName = newValue;

this._nameChanged.emit({ oldValue, newValue });
};

private _name: Y.Text;
private _nameChanged = new Signal<IWorkflowDoc, StringChange>(this);
get nameChanged(): ISignal<IWorkflowDoc, StringChange> {
return this._nameChanged;
}

get name(): string {
return this._name.toString();
}

get version(): string {
return '0.0.1';
}
Expand All @@ -35,8 +41,14 @@ export class WorkflowDoc
}

setName(name: string): void {
const newName = new Y.Text();
newName.insert(0, name);
this._name = newName;
const currentLength = this._name.length;
if (currentLength > 0) {
this._name.delete(0, currentLength);
}
this._name.insert(0, name);
}

private _name: Y.Text;
private _previousName: string;
private _nameChanged = new Signal<IWorkflowDoc, StringChange>(this);
}

0 comments on commit cc9c0b0

Please sign in to comment.