Skip to content

Commit

Permalink
New SendByGroup component
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Oct 8, 2013
1 parent 82bde5f commit 6ea0bd4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions component.json
Expand Up @@ -22,6 +22,7 @@
"components/FilterByGroup.coffee",
"components/Objectify.coffee",
"components/ReadGroup.coffee",
"components/SendByGroup.coffee",
"components/CollectGroups.coffee",
"components/FirstGroup.coffee",
"components/MapGroup.coffee",
Expand All @@ -42,6 +43,7 @@
"FilterByGroup": "components/FilterByGroup.coffee",
"Objectify": "components/Objectify.coffee",
"ReadGroup": "components/ReadGroup.coffee",
"SendByGroup": "components/SendByGroup.coffee",
"CollectGroups": "components/CollectGroups.coffee",
"FirstGroup": "components/FirstGroup.coffee",
"MapGroup": "components/MapGroup.coffee",
Expand Down
54 changes: 54 additions & 0 deletions components/SendByGroup.coffee
@@ -0,0 +1,54 @@
noflo = require 'noflo'

class SendByGroup extends noflo.Component
description: 'Send packet held in "data" when receiving
matching set of groups in "in"'
icon: 'share-sign'

constructor: ->
@data = {}
@ungrouped = null
@dataGroups = []
@inGroups = []

@inPorts =
in: new noflo.Port 'bang'
data: new noflo.Port 'all'

@outPorts =
out: new noflo.ArrayPort 'all'

@inPorts.data.on 'begingroup', (group) =>
@dataGroups.push group
@inPorts.data.on 'data', (data) =>
unless @dataGroups.length
@ungrouped = data
return
@data[@groupId(@dataGroups)] = data
@inPorts.data.on 'endgroup', =>
@dataGroups.pop()

@inPorts.in.on 'begingroup', (group) =>
@inGroups.push group
@inPorts.in.on 'data', (data) =>
unless @dataGroups.length
@send @ungrouped if @ungrouped isnt null
return
id = @groupId @inGroups
unless @data[@groupId]
return
@send @data[id]
@inPorts.in.on 'endgroup', =>
@inGroups.pop()
@inPorts.in.on 'disconnect', =>
@outPorts.out.disconnect()

groupId: (groups) ->
groups.join ':'

send: (data) ->
for group in @inGroups
@outPorts.out.beginGroup group
@outPorts.out.send data
for group in @inGroups
@outPorts.out.endGroup()
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -59,6 +59,7 @@
"FilterByGroup": "./components/FilterByGroup.coffee",
"Objectify": "./components/Objectify.coffee",
"ReadGroup": "./components/ReadGroup.coffee",
"SendByGroup": "components/SendByGroup.coffee",
"CollectGroups": "./components/CollectGroups.coffee",
"FirstGroup": "./components/FirstGroup.coffee",
"MapGroup": "./components/MapGroup.coffee",
Expand Down

0 comments on commit 6ea0bd4

Please sign in to comment.