Skip to content
Browse files

New SendByGroup component

  • Loading branch information...
1 parent 82bde5f commit 6ea0bd42f020cf7e7e294674017dc9d48b733292 @bergie bergie committed Oct 8, 2013
Showing with 57 additions and 0 deletions.
  1. +2 −0 component.json
  2. +54 −0 components/SendByGroup.coffee
  3. +1 −0 package.json
View
2 component.json
@@ -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",
@@ -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",
View
54 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()
View
1 package.json
@@ -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",

0 comments on commit 6ea0bd4

Please sign in to comment.
Something went wrong with that request. Please try again.