Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "reflux", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"homepage": "https://github.com/spoike/reflux", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -230,3 +230,6 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Reflux=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | ||
functor = function() { | ||
action.emit(eventLabel, Array.prototype.slice.call(arguments, 0)); | ||
functor.preEmit.apply(functor, arguments); | ||
if (functor.shouldEmit.apply(functor, arguments)) { | ||
action.emit(eventLabel, arguments); | ||
} | ||
}; | ||
@@ -252,2 +255,18 @@ | ||
/** | ||
* 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; | ||
@@ -292,4 +311,3 @@ | ||
Store.prototype.trigger = function() { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
store.emit(eventLabel, args); | ||
store.emit(eventLabel, arguments); | ||
}; | ||
@@ -332,3 +350,5 @@ | ||
module.exports.extend = function(obj) { | ||
if (!isObject(obj)) return obj; | ||
if (!isObject(obj)) { | ||
return obj; | ||
} | ||
var source, prop; | ||
@@ -335,0 +355,0 @@ for (var i = 1, length = arguments.length; i < length; i++) { |
@@ -1,1 +0,1 @@ | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.Reflux=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){"use strict";function c(){this._events={}}c.prototype.listeners=function(a){return Array.apply(this,this._events[a]||[])},c.prototype.emit=function(a,b,c,d,e,f){if(!this._events||!this._events[a])return!1;var g,h,i=this._events[a],j=i.length,k=arguments.length,l=i[0];if(1===j)switch(l.__EE3_once&&this.removeListener(a,l),k){case 1:l.call(l.__EE3_context||this);break;case 2:l.call(l.__EE3_context||this,b);break;case 3:l.call(l.__EE3_context||this,b,c);break;case 4:l.call(l.__EE3_context||this,b,c,d);break;case 5:l.call(l.__EE3_context||this,b,c,d,e);break;case 6:l.call(l.__EE3_context||this,b,c,d,e,f);break;default:for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];l.apply(l.__EE3_context||this,g)}else{for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];for(h=0;j>h;l=i[++h])l.__EE3_once&&this.removeListener(a,l),l.apply(l.__EE3_context||this,g)}return!0},c.prototype.on=function(a,b,c){return this._events||(this._events={}),this._events[a]||(this._events[a]=[]),b.__EE3_context=c,this._events[a].push(b),this},c.prototype.once=function(a,b,c){return b.__EE3_once=!0,this.on(a,b,c)},c.prototype.removeListener=function(a,b){if(!this._events||!this._events[a])return this;for(var c=this._events[a],d=[],e=0,f=c.length;f>e;e++)b&&c[e]!==b&&d.push(c[e]);return this._events[a]=d.length?d:null,this},c.prototype.removeAllListeners=function(a){return this._events?(a?this._events[a]=null:this._events={},this):this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prototype.setMaxListeners=function(){return this},c.EventEmitter=c,c.EventEmitter2=c,c.EventEmitter3=c;try{b.exports=c}catch(d){}},{}],2:[function(a,b){b.exports={componentWillMount:function(){this.subscriptions=[]},listenTo:function(a,b){var c=a.listen(b,this);this.subscriptions.push(c)},componentWillUnmount:function(){this.subscriptions.forEach(function(a){a()}),this.subscriptions=[]}}},{}],3:[function(a,b){var c=a("./utils");b.exports=function(){var a,b=new c.EventEmitter,d="action";return a=function(){b.emit(d,Array.prototype.slice.call(arguments,0))},a.listen=function(a,c){var e=function(b){a.apply(c,b)};return b.addListener(d,e),function(){b.removeListener(d,e)}},a}},{"./utils":6}],4:[function(a,b){var c=a("./utils");b.exports=function(a){function b(){this.init&&c.isFunction(this.init)&&this.init()}var d=new c.EventEmitter,e="change";return c.extend(b.prototype,a),b.prototype.listenTo=function(a,b){if(!c.isFunction(a.listen))throw new TypeError(a+" is missing a listen method");return a.listen(b,this)},b.prototype.listen=function(a,b){var c=function(c){a.apply(b,c)};return d.addListener(e,c),function(){d.removeListener(e,c)}},b.prototype.trigger=function(){var a=Array.prototype.slice.call(arguments,0);d.emit(e,a)},new b}},{"./utils":6}],5:[function(a,b,c){c.createAction=a("./createAction"),c.createStore=a("./createStore"),c.ListenerMixin=a("./ListenerMixin"),c.createActions=function(a){for(var b=0,d={};b<a.length;b++)d[a[b]]=c.createAction();return d},c.setEventEmitter=function(b){var c=a("./utils");c.EventEmitter=b}},{"./ListenerMixin":2,"./createAction":3,"./createStore":4,"./utils":6}],6:[function(a,b){var c=b.exports.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a};b.exports.extend=function(a){if(!c(a))return a;for(var b,d,e=1,f=arguments.length;f>e;e++){b=arguments[e];for(d in b)a[d]=b[d]}return a},b.exports.isFunction=function(a){return"function"==typeof a},b.exports.EventEmitter=a("eventemitter3")},{eventemitter3:1}]},{},[5])(5)}); | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.Reflux=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){"use strict";function c(){this._events={}}c.prototype.listeners=function(a){return Array.apply(this,this._events[a]||[])},c.prototype.emit=function(a,b,c,d,e,f){if(!this._events||!this._events[a])return!1;var g,h,i=this._events[a],j=i.length,k=arguments.length,l=i[0];if(1===j)switch(l.__EE3_once&&this.removeListener(a,l),k){case 1:l.call(l.__EE3_context||this);break;case 2:l.call(l.__EE3_context||this,b);break;case 3:l.call(l.__EE3_context||this,b,c);break;case 4:l.call(l.__EE3_context||this,b,c,d);break;case 5:l.call(l.__EE3_context||this,b,c,d,e);break;case 6:l.call(l.__EE3_context||this,b,c,d,e,f);break;default:for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];l.apply(l.__EE3_context||this,g)}else{for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];for(h=0;j>h;l=i[++h])l.__EE3_once&&this.removeListener(a,l),l.apply(l.__EE3_context||this,g)}return!0},c.prototype.on=function(a,b,c){return this._events||(this._events={}),this._events[a]||(this._events[a]=[]),b.__EE3_context=c,this._events[a].push(b),this},c.prototype.once=function(a,b,c){return b.__EE3_once=!0,this.on(a,b,c)},c.prototype.removeListener=function(a,b){if(!this._events||!this._events[a])return this;for(var c=this._events[a],d=[],e=0,f=c.length;f>e;e++)b&&c[e]!==b&&d.push(c[e]);return this._events[a]=d.length?d:null,this},c.prototype.removeAllListeners=function(a){return this._events?(a?this._events[a]=null:this._events={},this):this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prototype.setMaxListeners=function(){return this},c.EventEmitter=c,c.EventEmitter2=c,c.EventEmitter3=c;try{b.exports=c}catch(d){}},{}],2:[function(a,b){b.exports={componentWillMount:function(){this.subscriptions=[]},listenTo:function(a,b){var c=a.listen(b,this);this.subscriptions.push(c)},componentWillUnmount:function(){this.subscriptions.forEach(function(a){a()}),this.subscriptions=[]}}},{}],3:[function(a,b){var c=a("./utils");b.exports=function(){var a,b=new c.EventEmitter,d="action";return a=function(){a.preEmit.apply(a,arguments),a.shouldEmit.apply(a,arguments)&&b.emit(d,arguments)},a.listen=function(a,c){var e=function(b){a.apply(c,b)};return b.addListener(d,e),function(){b.removeListener(d,e)}},a.preEmit=function(){},a.shouldEmit=function(){return!0},a}},{"./utils":6}],4:[function(a,b){var c=a("./utils");b.exports=function(a){function b(){this.init&&c.isFunction(this.init)&&this.init()}var d=new c.EventEmitter,e="change";return c.extend(b.prototype,a),b.prototype.listenTo=function(a,b){if(!c.isFunction(a.listen))throw new TypeError(a+" is missing a listen method");return a.listen(b,this)},b.prototype.listen=function(a,b){var c=function(c){a.apply(b,c)};return d.addListener(e,c),function(){d.removeListener(e,c)}},b.prototype.trigger=function(){d.emit(e,arguments)},new b}},{"./utils":6}],5:[function(a,b,c){c.createAction=a("./createAction"),c.createStore=a("./createStore"),c.ListenerMixin=a("./ListenerMixin"),c.createActions=function(a){for(var b=0,d={};b<a.length;b++)d[a[b]]=c.createAction();return d},c.setEventEmitter=function(b){var c=a("./utils");c.EventEmitter=b}},{"./ListenerMixin":2,"./createAction":3,"./createStore":4,"./utils":6}],6:[function(a,b){var c=b.exports.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a};b.exports.extend=function(a){if(!c(a))return a;for(var b,d,e=1,f=arguments.length;f>e;e++){b=arguments[e];for(d in b)a[d]=b[d]}return a},b.exports.isFunction=function(a){return"function"==typeof a},b.exports.EventEmitter=a("eventemitter3")},{eventemitter3:1}]},{},[5])(5)}); |
@@ -7,2 +7,5 @@ module.exports = function(grunt) { | ||
files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'], | ||
options: { | ||
jshintrc: '.jshintrc' | ||
} | ||
}, | ||
@@ -9,0 +12,0 @@ mochaTest: { |
{ | ||
"name": "reflux", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A simple library for uni-directional dataflow application architecture inspired by ReactJS Flux", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "node test/index.js" | ||
"test": "grunt test" | ||
}, | ||
@@ -9,0 +9,0 @@ "author": "Mikael Brassman", |
@@ -77,2 +77,23 @@ # Reflux | ||
#### Action hooks | ||
There are a couple of hooks avaiable for each action. | ||
* `preEmit` - Is called before the action emits an event. It receives the arguments from the action invocation. | ||
* `shouldEmit` - Is called after `preEmit` and before the action emits an event. By default it returns `true` which will let the action emit the event. You may override this if you need to check the arguments that the action receives and see if it needs to emit the event. | ||
Example usage: | ||
```javascript | ||
Actions.statusUpdate.preEmit = function() { console.log(arguments); }; | ||
Actions.statusUpdate.shouldEmit = function(value) { | ||
return value > 0; | ||
}; | ||
Actions.statusUpdate(0); | ||
Actions.statusUpdate(1); | ||
// Should output: 1 | ||
``` | ||
### Creating data stores | ||
@@ -79,0 +100,0 @@ |
@@ -13,3 +13,6 @@ var _ = require('./utils'); | ||
functor = function() { | ||
action.emit(eventLabel, Array.prototype.slice.call(arguments, 0)); | ||
functor.preEmit.apply(functor, arguments); | ||
if (functor.shouldEmit.apply(functor, arguments)) { | ||
action.emit(eventLabel, arguments); | ||
} | ||
}; | ||
@@ -35,4 +38,20 @@ | ||
/** | ||
* 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; | ||
}; |
@@ -35,4 +35,3 @@ var _ = require('./utils'); | ||
Store.prototype.trigger = function() { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
store.emit(eventLabel, args); | ||
store.emit(eventLabel, arguments); | ||
}; | ||
@@ -39,0 +38,0 @@ |
@@ -12,3 +12,5 @@ /* | ||
module.exports.extend = function(obj) { | ||
if (!isObject(obj)) return obj; | ||
if (!isObject(obj)) { | ||
return obj; | ||
} | ||
var source, prop; | ||
@@ -15,0 +17,0 @@ for (var i = 1, length = arguments.length; i < length; i++) { |
var chai = require('chai'), | ||
assert = chai.assert, | ||
Reflux = require('../src'), | ||
Q = require('q'), | ||
internalUtils = require('../src/utils'); | ||
@@ -6,0 +5,0 @@ |
@@ -10,6 +10,8 @@ var chai = require('chai'), | ||
var action; | ||
var action, | ||
testArgs; | ||
beforeEach(function () { | ||
action = Reflux.createAction(); | ||
testArgs = [1337, 'test']; | ||
}); | ||
@@ -35,3 +37,2 @@ | ||
it('should receive the correct arguments', function() { | ||
var testArgs = [1337, 'test']; | ||
action(testArgs[0], testArgs[1]); | ||
@@ -42,2 +43,77 @@ | ||
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); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -44,0 +120,0 @@ |
@@ -11,3 +11,3 @@ var chai = require('chai'), | ||
describe('with one store listening to a simple action', function() { | ||
var action, | ||
var action, | ||
store, | ||
@@ -52,9 +52,13 @@ promise, | ||
it('shouldn\'t have been called when action is called', function(done) { | ||
action(1337, 'ninja'); | ||
var resolved = false; | ||
promise.then(function() { | ||
assert.fail(); | ||
resolved = true; | ||
}); | ||
setTimeout(done, 20); | ||
action(1337, 'ninja'); | ||
setTimeout(function() { | ||
assert.isFalse(resolved); | ||
done(); | ||
}, 20); | ||
}); | ||
@@ -87,9 +91,13 @@ | ||
it('shouldn\'t have been called when action is called', function(done) { | ||
var resolved = false; | ||
storeListenPromise.then(function() { | ||
resolved = true; | ||
}); | ||
action(1337, 'ninja'); | ||
promise.then(function() { | ||
assert.fail(); | ||
}); | ||
setTimeout(done, 20); | ||
setTimeout(function() { | ||
assert.isFalse(resolved); | ||
done(); | ||
}, 20); | ||
}); | ||
@@ -100,2 +108,2 @@ }); | ||
}); | ||
}); |
@@ -66,3 +66,3 @@ var Reflux = require('../src'); | ||
var consoleComponent = new ConsoleComponent(); | ||
new ConsoleComponent(); | ||
@@ -69,0 +69,0 @@ // Invoking the action with arbitrary parameters |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46323
22
900
251