Skip to content

Commit

Permalink
feat(command): expose change trigger via <commandStack.changed> event
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Jan 22, 2021
1 parent 244ef78 commit 9a41cbc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/command/CommandStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ export default function CommandStack(eventBus, injector) {
* Current active commandStack execution
*
* @type {Object}
* @property {Object[]} actions
* @property {Object[]} dirty
* @property { 'undo' | 'redo' | 'clear' | 'execute' | null } trigger the cause of the current excecution
*/
this._currentExecution = {
actions: [],
dirty: []
dirty: [],
trigger: null
};


Expand Down Expand Up @@ -141,6 +145,8 @@ CommandStack.prototype.execute = function(command, context) {
throw new Error('command required');
}

this._currentExecution.trigger = 'execute';

var action = { command: command, context: context };

this._pushAction(action);
Expand Down Expand Up @@ -201,7 +207,7 @@ CommandStack.prototype.clear = function(emit) {
this._stackIdx = -1;

if (emit !== false) {
this._fire('changed');
this._fire('changed', { trigger: 'clear' });
}
};

Expand All @@ -214,6 +220,8 @@ CommandStack.prototype.undo = function() {
next;

if (action) {
this._currentExecution.trigger = 'undo';

this._pushAction(action);

while (action) {
Expand All @@ -240,6 +248,8 @@ CommandStack.prototype.redo = function() {
next;

if (action) {
this._currentExecution.trigger = 'redo';

this._pushAction(action);

while (action) {
Expand Down Expand Up @@ -446,6 +456,7 @@ CommandStack.prototype._pushAction = function(action) {

CommandStack.prototype._popAction = function() {
var execution = this._currentExecution,
trigger = execution.trigger,
actions = execution.actions,
dirty = execution.dirty;

Expand All @@ -456,7 +467,9 @@ CommandStack.prototype._popAction = function() {

dirty.length = 0;

this._fire('changed');
this._fire('changed', { trigger: trigger });

execution.trigger = null;
}
};

Expand Down
83 changes: 83 additions & 0 deletions test/spec/command/CommandStackSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,87 @@ describe('command/CommandStack', function() {

});


describe('trigger', function() {

beforeEach(inject(function(commandStack) {
commandStack.registerHandler('complex-command', ComplexCommand);
commandStack.registerHandler('pre-command', PreCommand);
commandStack.registerHandler('post-command', PostCommand);
}));


it('should indicate <execute>', inject(function(eventBus, commandStack) {

// given
var changedSpy = sinon.spy(function(event) {
expect(event.trigger).to.eql('execute');
});

// when
eventBus.on('commandStack.changed', changedSpy);

commandStack.execute('complex-command', { element: { trace: [] } });

// then
expect(changedSpy).to.have.been.calledOnce;
}));


it('with trigger <undo>', inject(function(eventBus, commandStack) {

// given
var changedSpy = sinon.spy(function(event) {
expect(event.trigger).to.eql('undo');
});

commandStack.execute('complex-command', { element: { trace: [] } });

// when
eventBus.on('commandStack.changed', changedSpy);

commandStack.undo();

// then
expect(changedSpy).to.have.been.calledOnce;
}));


it('with trigger <redo>', inject(function(eventBus, commandStack) {

// given
var changedSpy = sinon.spy(function(event) {
expect(event.trigger).to.eql('redo');
});

commandStack.execute('complex-command', { element: { trace: [] } });
commandStack.undo();

// when
eventBus.on('commandStack.changed', changedSpy);
commandStack.redo();

// then
expect(changedSpy).to.have.been.calledOnce;
}));


it('with trigger <clear>', inject(function(eventBus, commandStack) {

// given
var changedSpy = sinon.spy(function(event) {
expect(event.trigger).to.eql('clear');
});

// when
eventBus.on('commandStack.changed', changedSpy);

commandStack.clear();

// then
expect(changedSpy).to.have.been.calledOnce;
}));

});

});

0 comments on commit 9a41cbc

Please sign in to comment.