Comparing version 0.0.1 to 0.0.2
@@ -16,17 +16,20 @@ "use strict"; | ||
var behaviours = {}; | ||
behaviours.carry = function (flow, defaults, name, data) { | ||
flow.data = function () { | ||
for (var _len = arguments.length, data = Array(_len), _key = 0; _key < _len; _key++) { | ||
data[_key] = arguments[_key]; | ||
behaviours.cancellable = function (flow, defaults, name) { | ||
flow.cancel = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (!data.length) { | ||
return flow.data.value.length <= 1 ? flow.data.value[0] : flow.data.value; | ||
} | ||
var oldData = flow.data.value; | ||
flow.data.value = data; | ||
dispatchInternalEvent(flow, "data", data.length > 1 ? data : data[0], oldData.length > 1 ? oldData : oldData[0]); | ||
assert(args.length, ERRORS.invalidCancelArgs); | ||
dispatchInternalEvent(flow, "cancel", true); | ||
flow.status.value = STATUS.CANCELLED; | ||
return flow; | ||
}; | ||
flow.data.value = data; | ||
flow.isCancelled = function () { | ||
return [flow].concat(flow.parents()).some(function (e) { | ||
return e.status.value == STATUS.CANCELLED; | ||
}); | ||
}; | ||
}; | ||
@@ -76,6 +79,9 @@ behaviours.connect = function (flow) { | ||
assert(args.length, ERRORS.invalidChildren); | ||
//TODO handle circular deps | ||
var childMap = {}; | ||
return getChildren(flow); | ||
function getChildren(flow) { | ||
if (childMap[flow.guid()]) { | ||
return []; | ||
}childMap[flow.guid()] = true; | ||
var c = flow.children.value; | ||
@@ -98,4 +104,5 @@ var gc = flow.children.value.map(getChildren); | ||
detach(flow); | ||
dispatchInternalEvent(flow, "childRemoved", previousParent); | ||
attach(parent); | ||
dispatchInternalEvent(flow, "parent", parent, previousParent); | ||
dispatchInternalEvent(flow, "childAdded", parent, previousParent); | ||
return flow; | ||
@@ -333,3 +340,3 @@ }; | ||
listener.apply(event, event.data.value); | ||
return flow.status() == STATUS.FLOWING; | ||
return event.status() == STATUS.FLOWING; | ||
}); | ||
@@ -346,2 +353,18 @@ return true; | ||
}; | ||
behaviours.stateful = function (flow, defaults, name, data) { | ||
flow.data = function () { | ||
for (var _len = arguments.length, data = Array(_len), _key = 0; _key < _len; _key++) { | ||
data[_key] = arguments[_key]; | ||
} | ||
if (!data.length) { | ||
return flow.data.value.length <= 1 ? flow.data.value[0] : flow.data.value; | ||
} | ||
var oldData = flow.data.value; | ||
flow.data.value = data; | ||
dispatchInternalEvent(flow, "data", data.length > 1 ? data : data[0], oldData.length > 1 ? oldData : oldData[0]); | ||
return flow; | ||
}; | ||
flow.data.value = data; | ||
}; | ||
/** | ||
@@ -370,3 +393,3 @@ * consts | ||
}, | ||
behaviours: [behaviours.identify, behaviours.carry, behaviours.connect, behaviours.create, behaviours.emit, behaviours.listen, behaviours.log], | ||
behaviours: [behaviours.identify, behaviours.stateful, behaviours.connect, behaviours.create, behaviours.emit, behaviours.listen, behaviours.cancellable, behaviours.log], | ||
direction: DIRECTION.DEFAULT | ||
@@ -384,2 +407,3 @@ }; | ||
invalidStatus: "Invalid Argument. The .status() API is read only", | ||
invalidCancelArgs: "Invalid Argument. The .cancel() API requires no parameters", | ||
invalidRoot: "Invalid Argument. The .parents.root() API is read only" | ||
@@ -403,3 +427,3 @@ }; | ||
function isInternal(flow) { | ||
return flow && flow.name && flow.name.isFlow; | ||
return flow && flow.name && flow.name.isInternal; | ||
} | ||
@@ -432,5 +456,10 @@ | ||
e.emit(); | ||
e.data.value = [flow, newData, oldData]; | ||
e.direction.value = DIRECTION.UPSTREAM; | ||
e.name.value = "flow.children." + name; | ||
e.emit(); | ||
e.direction.value = DIRECTION.DOWNSTREAM; | ||
e.name.value = "flow.parent." + name; | ||
e.emit(); | ||
} | ||
@@ -437,0 +466,0 @@ var instance = create(DEFAULTS, "flow"); |
{ | ||
"name": "nflow", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "hierarchical event dispatcher", | ||
"main": "dist/flow.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "gulp mocha" | ||
}, | ||
@@ -9,0 +9,0 @@ "keywords": [ |
@@ -32,7 +32,7 @@ | ||
flow | ||
.parent() // returns the arent flow object | ||
.parent() // return the parent flow object | ||
.parent(null) // detach the flow object, creating a new subtree | ||
.parent(flow) // re-parent the flow object | ||
flow.parents() // returns all upstream flow objects | ||
flow.parents() // return all upstream flow objects | ||
@@ -47,3 +47,3 @@ flow | ||
flow | ||
.children() //returns all direct descendant nodes | ||
.children() //return all direct descendant nodes | ||
.children.all() // return all child nodes recursively | ||
@@ -65,1 +65,12 @@ | ||
``` | ||
### Internal Events | ||
When a flow object mutates in any way, all connected flow objects are notified of the change. | ||
`.data(newData)` triggers: | ||
- `.on('flow.data', newData, oldData)` on the current flow object | ||
- `.on('flow.children.data', flow, newData, oldData)` on all parent objects | ||
- `.on('flow.parent.data', flow, newData, oldData)` on all child objects (recursively) | ||
@@ -31,6 +31,8 @@ behaviours.connect = (flow)=>{ | ||
assert(args.length, ERRORS.invalidChildren) | ||
//TODO handle circular deps | ||
var childMap = {} | ||
return getChildren(flow) | ||
function getChildren(flow){ | ||
if (childMap[flow.guid()]) return []; | ||
childMap[flow.guid()] = true | ||
var c = flow.children.value | ||
@@ -52,4 +54,5 @@ var gc = flow.children.value.map(getChildren) | ||
detach(flow) | ||
dispatchInternalEvent(flow, 'childRemoved', previousParent) | ||
attach(parent) | ||
dispatchInternalEvent(flow, 'parent', parent, previousParent) | ||
dispatchInternalEvent(flow, 'childAdded', parent, previousParent) | ||
return flow | ||
@@ -56,0 +59,0 @@ } |
@@ -29,3 +29,3 @@ behaviours.listen = (flow)=>{ | ||
listener.apply(event, event.data.value) | ||
return (flow.status() == STATUS.FLOWING) | ||
return (event.status() == STATUS.FLOWING) | ||
}) | ||
@@ -32,0 +32,0 @@ return true |
@@ -24,3 +24,3 @@ /** | ||
behaviours.identify, | ||
behaviours.carry, | ||
behaviours.stateful, | ||
behaviours.connect, | ||
@@ -30,2 +30,3 @@ behaviours.create, | ||
behaviours.listen, | ||
behaviours.cancellable, | ||
behaviours.log | ||
@@ -45,3 +46,4 @@ ], | ||
, invalidStatus:'Invalid Argument. The .status() API is read only' | ||
, invalidCancelArgs:'Invalid Argument. The .cancel() API requires no parameters' | ||
, invalidRoot:'Invalid Argument. The .parents.root() API is read only' | ||
} |
@@ -22,3 +22,3 @@ /** | ||
&& flow.name | ||
&& flow.name.isFlow | ||
&& flow.name.isInternal | ||
} | ||
@@ -53,2 +53,3 @@ | ||
e.emit() | ||
e.data.value = [flow, newData, oldData] | ||
e.direction.value= DIRECTION.UPSTREAM | ||
@@ -58,2 +59,6 @@ e.name.value="flow.children."+name | ||
e.direction.value= DIRECTION.DOWNSTREAM | ||
e.name.value="flow.parent."+name | ||
e.emit() | ||
@@ -60,0 +65,0 @@ |
@@ -59,2 +59,12 @@ | ||
}) | ||
it('.all() should handle circular dependencies', function(){ | ||
var a = sut.create('a') | ||
var b = a.create('b') | ||
var c = b.create('c') | ||
var c1 = b.create('c1') | ||
a.parent(c) | ||
var children = b.children.all() | ||
expect(children.map(e=>e.name())).to.eql(["c","c1","a","b"]) | ||
}) | ||
@@ -61,0 +71,0 @@ it('should find child by String', function(){ |
@@ -63,2 +63,29 @@ | ||
it('should deliver to multiple listeners on the same node', function(done){ | ||
var payload1 = {} | ||
var payload2 = {} | ||
var listener1 = function(data1, data2){ | ||
expect(data1).to.equal(payload1) | ||
expect(data2).to.equal(payload2) | ||
listener1.done = true | ||
listener1.done | ||
&& listener2.done | ||
&& done() | ||
} | ||
var listener2 = function(data1, data2){ | ||
expect(data1).to.equal(payload1) | ||
expect(data2).to.equal(payload2) | ||
listener2.done = true | ||
listener1.done | ||
&& listener2.done | ||
&& done() | ||
} | ||
sut.on('bar', listener1, listener2) | ||
.create('bar') | ||
.data(payload1, payload2) | ||
.emit() | ||
}) | ||
it('should emit lightweight nodes', function(done){ | ||
@@ -65,0 +92,0 @@ var listener = function(data){ |
@@ -15,6 +15,5 @@ | ||
describe('parent() API', function(){ | ||
it('should dispatch flow.parent internal event', function(done){ | ||
it('should dispatch flow.childRemoved internal event', function(done){ | ||
var payload = {} | ||
var listener = function(newParent, oldParent){ | ||
expect(newParent).to.equal(sut1) | ||
var listener = function(oldParent){ | ||
expect(oldParent).to.equal(sut) | ||
@@ -28,7 +27,7 @@ done() | ||
.create('test') | ||
.on('flow.parent', listener) | ||
.on('flow.childRemoved', listener) | ||
.parent(sut1) | ||
}) | ||
it('should dispatch flow.children.parent internal event', function(done){ | ||
it('should dispatch flow.childAdded internal event on reparenting', function(done){ | ||
var payload = {} | ||
@@ -44,7 +43,41 @@ var listener = function(newParent, oldParent){ | ||
sut | ||
.on('flow.children.parent', listener) | ||
.create('test') | ||
.on('flow.childAdded', listener) | ||
.parent(sut1) | ||
}) | ||
it('should dispatch flow.children.childRemoved internal event', function(done){ | ||
var payload = {} | ||
var listener = function(f, oldParent){ | ||
expect(oldParent).to.equal(sut) | ||
done() | ||
} | ||
var sut1 = sut.create('newParent') | ||
sut | ||
.on('flow.children.childRemoved', listener) | ||
.create('test') | ||
.parent(sut1) | ||
}) | ||
it('should dispatch flow.children.childAdded internal event', function(done){ | ||
var payload = {} | ||
var listener = function(f, newParent, oldParent){ | ||
expect(f.name()).to.equal("test") | ||
expect(newParent.name()).to.equal("newParent") | ||
expect(oldParent.name()).to.equal("test2") | ||
done() | ||
} | ||
var sut1 = sut.create('newParent') | ||
sut | ||
.on('flow.children.childAdded', listener) | ||
.create('test0') | ||
.create('test1') | ||
.create('test2') | ||
.create('test') | ||
.parent(sut1) | ||
}) | ||
}) | ||
@@ -70,3 +103,3 @@ | ||
var payload = {} | ||
var listener = function(newData, oldData){ | ||
var listener = function(f, newData, oldData){ | ||
expect(newData).to.equal(data2) | ||
@@ -73,0 +106,0 @@ expect(oldData).to.equal(data1) |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
73942
26
1442
2
74