Permalink
Browse files

add LastGroup component

  • Loading branch information...
1 parent 553da10 commit dbe4bf35979bfa6fb1e79b45e0c48311c2c54411 @djdeath djdeath committed Apr 22, 2014
Showing with 87 additions and 0 deletions.
  1. +2 −0 component.json
  2. +41 −0 components/LastGroup.coffee
  3. +1 −0 package.json
  4. +43 −0 test/LastGroup.coffee
View
@@ -27,6 +27,7 @@
"components/CollectObject.coffee",
"components/CollectTree.coffee",
"components/FirstGroup.coffee",
+ "components/LastGroup.coffee",
"components/MapGroup.coffee",
"components/MergeGroups.coffee",
"components/GroupByObjectKey.coffee",
@@ -51,6 +52,7 @@
"CollectObject": "components/CollectObject.coffee",
"CollectTree": "components/CollectTree.coffee",
"FirstGroup": "components/FirstGroup.coffee",
+ "LastGroup": "components/LastGroup.coffee",
"MapGroup": "components/MapGroup.coffee",
"MergeGroups": "components/MergeGroups.coffee",
"GroupByObjectKey": "components/GroupByObjectKey.coffee"
@@ -0,0 +1,41 @@
+noflo = require 'noflo'
+
+class LastGroup extends noflo.Component
+ constructor: ->
+ @inPorts =
+ in: new noflo.Port
+ @outPorts =
+ out: new noflo.Port
+
+ @groups = []
+ @groupSent = 0
+
+ @inPorts.in.on 'begingroup', (group) =>
+ @storeGroup group
+ @inPorts.in.on 'data', (data) =>
+ @sendBeginGroup()
+ @outPorts.out.send data if @outPorts.out.isAttached()
+ @inPorts.in.on 'endgroup', (group) =>
+ @sendEndGroup()
+ @inPorts.in.on 'disconnect', =>
+ @outPorts.out.disconnect() if @outPorts.out.isConnected()
+
+ storeGroup: (group) ->
+ @groups.push { name: group, emitted: false }
+
+ sendBeginGroup: () ->
+ return unless @groups.length > 0
+ group = @groups[@groups.length - 1]
+ return if group.emitted
+ group.emitted = true
+ @outPorts.out.beginGroup group.name if @outPorts.out.isAttached()
+
+ sendEndGroup: () ->
+ group = @groups.pop()
+ return unless group.emitted
+ @outPorts.out.endGroup() if @outPorts.out.isAttached()
+
+ clearGroups: () ->
+ @groups = []
+
+exports.getComponent = -> new LastGroup
View
@@ -65,6 +65,7 @@
"CollectObject": "components/CollectObject.coffee",
"CollectTree": "components/CollectTree.coffee",
"FirstGroup": "./components/FirstGroup.coffee",
+ "LastGroup": "./components/LastGroup.coffee",
"MapGroup": "./components/MapGroup.coffee",
"MergeGroups": "./components/MergeGroups.coffee",
"GroupByObjectKey": "./components/GroupByObjectKey.coffee"
View
@@ -0,0 +1,43 @@
+test = require "noflo-test"
+
+test.component("groups/LastGroup").
+ discuss("pass no group").
+ send.connect("in").
+ send.data("in", "data").
+ send.disconnect("in").
+ discuss("no group is sent").
+ receive.connect("out").
+ receive.data("out", "data").
+ receive.disconnect("out").
+
+ next().
+ discuss("pass one group & data").
+ send.connect("in").
+ send.beginGroup("in", "group").
+ send.data("in", "data").
+ send.endGroup("in").
+ send.disconnect("in").
+ discuss("one group is sent").
+ receive.connect("out").
+ receive.beginGroup("out", "group").
+ receive.data("out", "data").
+ receive.endGroup("out").
+ receive.disconnect("out").
+
+ next().
+ discuss("pass 2 groups & data").
+ send.connect("in").
+ send.beginGroup("in", "group1").
+ send.beginGroup("in", "group2").
+ send.data("in", "data").
+ send.endGroup("in").
+ send.endGroup("in").
+ send.disconnect("in").
+ discuss("one group is sent").
+ receive.connect("out").
+ receive.beginGroup("out", "group2").
+ receive.data("out", "data").
+ receive.endGroup("out").
+ receive.disconnect("out").
+
+export module

0 comments on commit dbe4bf3

Please sign in to comment.