Skip to content

Commit 9173b94

Browse files
stee-reca-d
authored andcommitted
feat: extend subscriber handling to publish on all changes (including undo & redo)
1 parent 9899f8e commit 9173b94

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

XMLEditor.spec.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,6 @@ describe('XMLEditor', () => {
233233
expect(called).to.equal(1);
234234
expect(editor.past).to.have.lengthOf(1);
235235

236-
editor.undo();
237-
expect(called).to.equal(1);
238-
expect(editor.past).to.have.lengthOf(0);
239-
expect(editor.future).to.have.lengthOf(1);
240-
241-
editor.redo();
242-
expect(called).to.equal(1);
243-
expect(editor.past).to.have.lengthOf(1);
244-
expect(editor.future).to.have.lengthOf(0);
245-
246236
const unsubscribed = unsubscribe();
247237
expect(unsubscribed).to.equal(callback);
248238

@@ -252,6 +242,52 @@ describe('XMLEditor', () => {
252242
expect(editor.past).to.have.lengthOf(2);
253243
});
254244

245+
it('notifies subscribers on undo with the previous commit', () => {
246+
const node = sclDoc.querySelector('Substation')!;
247+
const edit = { node };
248+
249+
const subscriber = sinon.spy();
250+
251+
editor.subscribe(subscriber);
252+
253+
const firstCommit = editor.commit(edit, { title: 'first' });
254+
sinon.assert.calledOnce(subscriber);
255+
sinon.assert.calledWithExactly(subscriber, firstCommit);
256+
257+
const secondCommit = editor.commit(edit, { title: 'second' });
258+
sinon.assert.calledTwice(subscriber);
259+
sinon.assert.calledWithExactly(subscriber, secondCommit);
260+
261+
editor.undo();
262+
sinon.assert.calledThrice(subscriber);
263+
sinon.assert.calledWithExactly(subscriber, firstCommit);
264+
});
265+
266+
it('notifies subscribers on redo with the redone commit', () => {
267+
const node = sclDoc.querySelector('Substation')!;
268+
const edit = { node };
269+
270+
const subscriber = sinon.spy();
271+
272+
editor.subscribe(subscriber);
273+
274+
const firstCommit = editor.commit(edit, { title: 'first' });
275+
sinon.assert.calledOnce(subscriber);
276+
sinon.assert.calledWithExactly(subscriber, firstCommit);
277+
278+
const secondCommit = editor.commit(edit, { title: 'second' });
279+
sinon.assert.calledTwice(subscriber);
280+
sinon.assert.calledWithExactly(subscriber, secondCommit);
281+
282+
editor.undo();
283+
sinon.assert.calledThrice(subscriber);
284+
sinon.assert.calledWithExactly(subscriber, firstCommit);
285+
286+
editor.redo();
287+
sinon.assert.callCount(subscriber, 4);
288+
sinon.assert.calledWithExactly(subscriber, secondCommit);
289+
});
290+
255291
it('unsubscribes the correct subscriber among many', () => {
256292
const node = sclDoc.querySelector('Substation')!;
257293
const edit = { node };

XMLEditor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
Transactor,
88
} from '@omicronenergy/oscd-api';
99

10+
const EMPTY_COMMIT: Commit<EditV2> = { undo: [], redo: [] };
11+
1012
export class XMLEditor implements Transactor<EditV2> {
1113
past: Commit<EditV2>[] = [];
1214
future: Commit<EditV2>[] = [];
@@ -36,6 +38,8 @@ export class XMLEditor implements Transactor<EditV2> {
3638
if (!commit) return;
3739
handleEdit(commit.undo);
3840
this.future.unshift(commit);
41+
const previousCommit = this.past[this.past.length - 1] || EMPTY_COMMIT;
42+
this.#subscribers.forEach(subscriber => subscriber(previousCommit));
3943
return commit;
4044
}
4145

@@ -44,6 +48,7 @@ export class XMLEditor implements Transactor<EditV2> {
4448
if (!commit) return;
4549
handleEdit(commit.redo);
4650
this.past.push(commit);
51+
this.#subscribers.forEach(subscriber => subscriber(commit));
4752
return commit;
4853
}
4954

0 commit comments

Comments
 (0)