Skip to content

Commit

Permalink
Added hooks preEmit and shouldEmit on actions.
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
spoike committed Jul 29, 2014
1 parent e25d813 commit 5e16f19
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 6 deletions.
21 changes: 20 additions & 1 deletion dist/reflux.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ module.exports = function() {
functor;

functor = function() {
action.emit(eventLabel, arguments);
functor.preEmit.apply(functor, arguments);
if (functor.shouldEmit.apply(functor, arguments)) {
action.emit(eventLabel, arguments);
}
};

/**
Expand All @@ -248,6 +251,22 @@ module.exports = function() {
};
};

/**
* Hook used by the action functor that is invoked before emitting
* and before `shouldEmit`. The arguments are the ones that the action
* is invoked with.
*/
functor.preEmit = function() {};

/**
* Hook used by the action functor after `preEmit` to determine if the
* event should be emitted with given arguments. This may be overridden
* in your application, default implementation always returns true.
*
* @returns {Boolean} true if event should be emitted
*/
functor.shouldEmit = function() { return true; };

return functor;

};
Expand Down
2 changes: 1 addition & 1 deletion dist/reflux.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/createAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ module.exports = function() {
functor;

functor = function() {
action.emit(eventLabel, arguments);
functor.preEmit.apply(functor, arguments);
if (functor.shouldEmit.apply(functor, arguments)) {
action.emit(eventLabel, arguments);
}
};

/**
Expand All @@ -31,6 +34,22 @@ module.exports = function() {
};
};

/**
* Hook used by the action functor that is invoked before emitting
* and before `shouldEmit`. The arguments are the ones that the action
* is invoked with.
*/
functor.preEmit = function() {};

/**
* Hook used by the action functor after `preEmit` to determine if the
* event should be emitted with given arguments. This may be overridden
* in your application, default implementation always returns true.
*
* @returns {Boolean} true if event should be emitted
*/
functor.shouldEmit = function() { return true; };

return functor;

};
80 changes: 78 additions & 2 deletions test/creatingActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ chai.use(require('chai-as-promised'));

describe('Creating action', function() {

var action;
var action,
testArgs;

beforeEach(function () {
action = Reflux.createAction();
testArgs = [1337, 'test'];
});

it('should be a callable functor', function() {
Expand All @@ -31,12 +33,86 @@ describe('Creating action', function() {


it('should receive the correct arguments', function() {
var testArgs = [1337, 'test'];
action(testArgs[0], testArgs[1]);

return assert.eventually.deepEqual(promise, testArgs);
});

describe('when adding preEmit hook', function() {

var savedPreEmit,
receivedArgs;

beforeEach(function() {
savedPreEmit = action.preEmit;
action.preEmit = function() {
receivedArgs = Array.prototype.slice.call(arguments, 0);
};
});

afterEach(function () {
action.preEmit = savedPreEmit;
});

it('should receive arguments from action functor', function() {
action.apply(null, testArgs);

assert.deepEqual(receivedArgs, testArgs);
});

});

describe('when replacing shouldEmit', function() {

var savedShouldEmit,
emitReturnValue,
receivedArgs;

beforeEach(function () {
emitReturnValue = true;
savedShouldEmit = action.shouldEmit;
action.shouldEmit = function() {
receivedArgs = Array.prototype.slice.call(arguments, 0);
return emitReturnValue;
};
hasRun = false;
});

afterEach(function() {
action.shouldEmit = savedShouldEmit;
});

it('should receive arguments from action functor', function() {
action.apply(null, testArgs);

assert.deepEqual(receivedArgs, testArgs);
});

describe('when shouldEmit returns false', function() {

beforeEach(function() {
emitReturnValue = false;
});


it('should not emit when shouldEmit returns false', function(done) {
var resolved = false;
promise.then(function() {
resolved = true;
});

action.apply(null, testArgs);

setTimeout(function() {
assert.isFalse(resolved);
done();
}, 20);
});

});

});

});

});
Expand Down
2 changes: 1 addition & 1 deletion test/creatingStores.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Creating stores', function() {
setTimeout(function() {
assert.isFalse(resolved);
done();
}, 200);
}, 20);
});

});
Expand Down

0 comments on commit 5e16f19

Please sign in to comment.