Comparing version 0.1.13 to 0.1.14
{ | ||
"name": "reflux", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"homepage": "https://github.com/spoike/reflux", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "reflux", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"description": "A simple library for uni-directional dataflow application architecture inspired by ReactJS Flux", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -6,2 +6,3 @@ /** | ||
var slice = Array.prototype.slice, | ||
_ = require("./utils"), | ||
createStore = require("./createStore"), | ||
@@ -38,2 +39,3 @@ strategyMethodNames = { | ||
return function(/* listenables..., callback*/){ | ||
_.throwIf(arguments.length < 3,'Cannot create a join with less than 2 listenables!'); | ||
var listenables = slice.call(arguments), | ||
@@ -47,7 +49,14 @@ callback = listenables.pop(), | ||
strategy: strategy | ||
}; | ||
for (var i = 0; i < numberOfListenables; i++) { | ||
this.listenTo(listenables[i],newListener(i,join)); | ||
}, i, cancels = [], subobj; | ||
for (i = 0; i < numberOfListenables; i++) { | ||
_.throwIf(this.validateListening(listenables[i])); | ||
} | ||
for (i = 0; i < numberOfListenables; i++) { | ||
cancels.push(listenables[i].listen(newListener(i,join),this)); | ||
} | ||
reset(join); | ||
subobj = {listenable: listenables}; | ||
subobj.stop = makeStopper(subobj,cancels,this); | ||
this.subscriptions = (this.subscriptions || []).concat(subobj); | ||
return subobj; | ||
}; | ||
@@ -58,2 +67,14 @@ }; | ||
function makeStopper(subobj,cancels,context){ | ||
return function() { | ||
var i, subs = context.subscriptions; | ||
index = (subs ? subs.indexOf(subobj) : -1); | ||
_.throwIf(index === -1,'Tried to remove join already gone from subscriptions list!'); | ||
for(i=0;i < cancels.length; i++){ | ||
cancels[i](); | ||
} | ||
subs.splice(index, 1); | ||
}; | ||
} | ||
function reset(join) { | ||
@@ -60,0 +81,0 @@ join.listenablesEmitted = new Array(join.numberOfListenables); |
@@ -16,8 +16,10 @@ var _ = require('./utils'), | ||
hasListener: function(listenable) { | ||
var i = 0, | ||
listener; | ||
var i = 0, j, listener, listenables; | ||
for (;i < (this.subscriptions||[]).length; ++i) { | ||
listener = this.subscriptions[i].listenable; | ||
if (listener === listenable || listener.hasListener && listener.hasListener(listenable)) { | ||
return true; | ||
listenables = [].concat(this.subscriptions[i].listenable); | ||
for (j = 0; j < listenables.length; j++){ | ||
listener = listenables[j]; | ||
if (listener === listenable || listener.hasListener && listener.hasListener(listenable)) { | ||
return true; | ||
} | ||
} | ||
@@ -145,2 +147,3 @@ } | ||
* @param {Function|String} callback The method to call when all publishers have emitted | ||
* @returns {Object} A subscription obj where `stop` is an unsub function and `listenable` is an array of listenables | ||
*/ | ||
@@ -154,2 +157,3 @@ joinTrailing: maker("last"), | ||
* @param {Function|String} callback The method to call when all publishers have emitted | ||
* @returns {Object} A subscription obj where `stop` is an unsub function and `listenable` is an array of listenables | ||
*/ | ||
@@ -163,2 +167,3 @@ joinLeading: maker("first"), | ||
* @param {Function|String} callback The method to call when all publishers have emitted | ||
* @returns {Object} A subscription obj where `stop` is an unsub function and `listenable` is an array of listenables | ||
*/ | ||
@@ -172,2 +177,3 @@ joinConcat: maker("all"), | ||
* @param {Function|String} callback The method to call when all publishers have emitted | ||
* @returns {Object} A subscription obj where `stop` is an unsub function and `listenable` is an array of listenables | ||
*/ | ||
@@ -174,0 +180,0 @@ joinStrict: maker("strict"), |
@@ -5,5 +5,43 @@ var assert = require('chai').assert, | ||
Store = Reflux.createStore, | ||
fn = function(){}; | ||
fn = function(){}, | ||
sinon = require('sinon'); | ||
describe('Stopping',function(){ | ||
describe('listening to a publisher that\'s only part of a join',function(){ | ||
var store = Store(), | ||
action1 = Action(), | ||
action2 = Action(); | ||
store.joinTrailing(action1,action2,function(){}); | ||
it('should fail',function(){ | ||
assert.equal(store.stopListeningTo(action1),false); | ||
assert.equal(store.subscriptions.length,1); | ||
}); | ||
}); | ||
describe('a join',function(){ | ||
var store = Store(), | ||
action1 = Action({sync:true}), | ||
action2 = Action({sync:true}), | ||
action3 = Action({sync:true}), | ||
indivcallback = sinon.spy(), | ||
joincallback = sinon.spy(), | ||
subobj; | ||
store.listenTo(action2,indivcallback); | ||
subobj = store.joinLeading(action1,action2,action3,joincallback); | ||
subobj.stop(); | ||
action1("A"); | ||
action2("B"); | ||
action3("C"); | ||
it('should leave the individual listening intact',function(){ | ||
assert.equal(store.subscriptions.length,1); | ||
assert.equal(store.subscriptions[0].listenable,action2); | ||
action2("foo","bar"); | ||
assert.deepEqual(["foo","bar"],indivcallback.lastCall.args); | ||
}); | ||
it('should not fire join callback anymore',function(done){ | ||
setTimeout(function(){ | ||
assert.equal(joincallback.callCount,0); | ||
done(); | ||
},10); | ||
}); | ||
}); | ||
describe('a single listen', function(){ | ||
@@ -10,0 +48,0 @@ describe('by calling stop directly',function(){ |
@@ -104,2 +104,17 @@ var assert = require('chai').assert, | ||
describe('with instance methods',function(){ | ||
describe('when validation fails',function(){ | ||
var store = Reflux.createStore(), | ||
action1 = {listen:sinon.spy()}, | ||
action2 = {listen:sinon.spy()}, | ||
action3 = {listen:sinon.spy()}; | ||
store.validateListening = sinon.stub().returns('ERROR! ERROR!'); | ||
it('should throw an error and not set any listens',function(){ | ||
assert.throws(function(){ | ||
store.joinTrailing(action1,action2,action3,function(){}); | ||
}); | ||
assert.equal(0,action1.listen.callCount); | ||
assert.equal(0,action2.listen.callCount); | ||
assert.equal(0,action3.listen.callCount); | ||
}); | ||
}); | ||
describe('keeping trailing arguments',function(){ | ||
@@ -110,4 +125,7 @@ var action1 = Reflux.createAction(), | ||
store = Reflux.createStore(), | ||
spy = sinon.spy(); | ||
store.joinTrailing(action1,action2,action3,spy); | ||
callback = sinon.spy(), | ||
validate = sinon.spy(), | ||
result; | ||
store.validateListening = validate; | ||
result = store.joinTrailing(action1,action2,action3,callback); | ||
action1('a'); | ||
@@ -118,3 +136,3 @@ action2('b'); | ||
it("should emit with the trailing arguments",function(){ | ||
assert.deepEqual(spy.firstCall.args,[['x'],['b'],['c']]); | ||
assert.deepEqual(callback.firstCall.args,[['x'],['b'],['c']]); | ||
}); | ||
@@ -126,3 +144,3 @@ action1('1'); | ||
it("should emit again after all fire a second time",function(){ | ||
assert.deepEqual(spy.secondCall.args,[['11'],['2'],['3']]); | ||
assert.deepEqual(callback.secondCall.args,[['11'],['2'],['3']]); | ||
}); | ||
@@ -132,4 +150,18 @@ action1('FOO'); | ||
it("should not emit a third time since not all fired three times",function(){ | ||
assert.equal(spy.callCount,2); | ||
assert.equal(callback.callCount,2); | ||
}); | ||
it("should return a subscription object with stop function and listenable array",function(){ | ||
assert.deepEqual([action1,action2,action3],result.listenable); | ||
assert.isFunction(result.stop); | ||
}); | ||
it("should add the returned subscription object to the context subscriptions array",function(){ | ||
assert.equal(1,store.subscriptions.length); | ||
assert.equal(result,store.subscriptions[0]); | ||
}); | ||
it("should validate each individual listenable in the join",function(){ | ||
assert.equal(3,validate.callCount); | ||
assert.equal(action1,validate.firstCall.args[0]); | ||
assert.equal(action2,validate.secondCall.args[0]); | ||
assert.equal(action3,validate.thirdCall.args[0]); | ||
}); | ||
}); | ||
@@ -202,3 +234,13 @@ describe('keeping leading arguments',function(){ | ||
}); | ||
describe('with less than 2 participants in the join',function(){ | ||
it('should fail',function(){ | ||
assert.throws(function(){ | ||
Reflux.createStore().joinConcat(Reflux.createAction(),function(){}); | ||
}); | ||
assert.throws(function(){ | ||
Reflux.createStore().joinConcat(function(){}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -105,2 +105,22 @@ var chai = require('chai'), | ||
describe('the hasListener method',function(){ | ||
var action1 = Reflux.createAction(), | ||
action2 = Reflux.createAction(), | ||
action3 = Reflux.createAction(), | ||
action4 = Reflux.createAction(), | ||
store = Reflux.createStore(); | ||
store.listenTo(action1,function(){}); | ||
store.joinLeading(action1,action2,action3,function(){}); | ||
it('should return true if context is listening',function(){ | ||
assert.equal(true,store.hasListener(action1)); | ||
}); | ||
it('should return false if context isn\'t listening',function(){ | ||
assert.equal(false,store.hasListener(action4)); | ||
}); | ||
it('should return true if context is listening to listenable as part of a join',function(){ | ||
assert.equal(true,store.hasListener(action2)); | ||
assert.equal(true,store.hasListener(action3)); | ||
}); | ||
}); | ||
}); |
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
167731
3316