-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CollectTree component (a simpler CollectGroups)
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
noflo = require 'noflo' | ||
|
||
class CollectTree extends noflo.Component | ||
description: 'Collect grouped packets into a simple tree structure | ||
and send on disconnect' | ||
|
||
constructor: -> | ||
@data = null | ||
@groups = [] | ||
@inPorts = | ||
in: new noflo.Port 'all' | ||
@outPorts = | ||
out: new noflo.Port 'all' | ||
error: new noflo.Port 'object' | ||
|
||
@inPorts.in.on 'connect', => | ||
@data = {} | ||
@inPorts.in.on 'begingroup', (group) => | ||
@groups.push group | ||
@inPorts.in.on 'data', (data) => | ||
return unless @groups.length | ||
d = @data | ||
for g, idx in @groups | ||
if idx < @groups.length - 1 | ||
d[g] = {} unless d[g] | ||
d = d[g] | ||
continue | ||
unless d[g] | ||
d[g] = data | ||
return | ||
unless Array.isArray d[g] | ||
d[g] = [d[g]] | ||
d[g].push data | ||
@inPorts.in.on 'endgroup', => | ||
@groups.pop() | ||
@inPorts.in.on 'disconnect', => | ||
@groups = [] | ||
if Object.keys(@data).length | ||
@outPorts.out.send @data | ||
@outPorts.out.disconnect() | ||
@data = null | ||
return | ||
@data = null | ||
err = new Error 'No tree information was collected' | ||
if @outPorts.error.isAttached() | ||
@outPorts.error.send err | ||
@outPorts.error.disconnect() | ||
return | ||
throw err | ||
|
||
exports.getComponent = -> new CollectTree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
noflo = require 'noflo' | ||
|
||
unless noflo.isBrowser() | ||
chai = require 'chai' unless chai | ||
CollectTree = require '../components/CollectTree.coffee' | ||
else | ||
CollectTree = require 'noflo-groups/components/CollectTree.js' | ||
|
||
describe 'CollectTree component', -> | ||
c = null | ||
ins = null | ||
out = null | ||
err = null | ||
|
||
beforeEach -> | ||
c = CollectTree.getComponent() | ||
c.inPorts.in.attach noflo.internalSocket.createSocket() | ||
c.outPorts.out.attach noflo.internalSocket.createSocket() | ||
c.outPorts.error.attach noflo.internalSocket.createSocket() | ||
ins = c.inPorts.in | ||
out = c.outPorts.out | ||
err = c.outPorts.error | ||
|
||
describe 'without any groups provided', -> | ||
it 'should send an error and no data', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(true).to.equal false | ||
|
||
err.on 'data', (data) -> | ||
chai.expect(data).to.be.an 'object' | ||
chai.expect(c.data).to.equal null | ||
chai.expect(c.groups.length).to.equal 0 | ||
done() | ||
|
||
ins.send 'foo' | ||
ins.send 'bar' | ||
ins.disconnect() | ||
|
||
describe 'with a single-level group', -> | ||
it 'should send out an object matching the one packet', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(data).to.eql | ||
foo: 'bar' | ||
out.on 'disconnect', -> | ||
done() | ||
|
||
ins.beginGroup 'foo' | ||
ins.send 'bar' | ||
ins.endGroup() | ||
ins.disconnect() | ||
it 'should send out an object matching the two packets', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(data).to.eql | ||
foo: ['bar', 'baz'] | ||
out.on 'disconnect', -> | ||
done() | ||
|
||
ins.beginGroup 'foo' | ||
ins.send 'bar' | ||
ins.send 'baz' | ||
ins.endGroup() | ||
ins.disconnect() | ||
it 'should send out an object matching the two packets despite group level change', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(data).to.eql | ||
foo: ['bar', 'baz'] | ||
out.on 'disconnect', -> | ||
done() | ||
|
||
ins.beginGroup 'foo' | ||
ins.send 'bar' | ||
ins.endGroup() | ||
ins.beginGroup 'foo' | ||
ins.send 'baz' | ||
ins.endGroup() | ||
ins.disconnect() | ||
|
||
describe 'with a multi-level group', -> | ||
it 'should send out an object matching the one packet', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(data).to.eql | ||
baz: | ||
foo: 'bar' | ||
out.on 'disconnect', -> | ||
done() | ||
|
||
ins.beginGroup 'baz' | ||
ins.beginGroup 'foo' | ||
ins.send 'bar' | ||
ins.endGroup() | ||
ins.endGroup() | ||
ins.disconnect() | ||
it 'should send out an object matching the three packets', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(data).to.eql | ||
baz: [ | ||
foo: 'bar' | ||
'ping' | ||
] | ||
hello: 'world' | ||
out.on 'disconnect', -> | ||
done() | ||
|
||
ins.beginGroup 'baz' | ||
ins.beginGroup 'foo' | ||
ins.send 'bar' | ||
ins.endGroup() | ||
ins.send 'ping' | ||
ins.endGroup() | ||
ins.beginGroup 'hello' | ||
ins.send 'world' | ||
ins.endGroup() | ||
ins.disconnect() | ||
it 'should send out an object matching the two packets despite endgroups', (done) -> | ||
out.on 'data', (data) -> | ||
chai.expect(data).to.eql | ||
baz: | ||
foo: 'bar' | ||
hello: 'world' | ||
out.on 'disconnect', -> | ||
done() | ||
|
||
ins.beginGroup 'baz' | ||
ins.beginGroup 'foo' | ||
ins.send 'bar' | ||
ins.endGroup() | ||
ins.endGroup() | ||
ins.beginGroup 'baz' | ||
ins.beginGroup 'hello' | ||
ins.send 'world' | ||
ins.endGroup() | ||
ins.endGroup() | ||
ins.disconnect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters