Permalink
Browse files

Merging

  • Loading branch information...
2 parents d36a612 + dbe4bf3 commit 9f594e21a3ccd6ad411c39ffb4fd7dc8d4488422 @bergie bergie committed May 9, 2014
Showing with 88 additions and 1 deletion.
  1. +2 −0 component.json
  2. +41 −0 components/LastGroup.coffee
  3. +2 −1 package.json
  4. +43 −0 test/LastGroup.coffee
View
@@ -28,6 +28,7 @@
"components/CollectObject.coffee",
"components/CollectTree.coffee",
"components/FirstGroup.coffee",
+ "components/LastGroup.coffee",
"components/MapGroup.coffee",
"components/MergeGroups.coffee",
"components/GroupByObjectKey.coffee",
@@ -48,6 +49,7 @@
"Group": "components/Group.coffee",
"GroupByObjectKey": "components/GroupByObjectKey.coffee",
"GroupZip": "components/GroupZip.coffee",
+ "LastGroup": "components/LastGroup.coffee",
"MapGroup": "components/MapGroup.coffee",
"MergeGroups": "components/MergeGroups.coffee",
"Objectify": "components/Objectify.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
@@ -63,6 +63,7 @@
"Group": "components/Group.coffee",
"GroupByObjectKey": "components/GroupByObjectKey.coffee",
"GroupZip": "components/GroupZip.coffee",
+ "LastGroup": "components/LastGroup.coffee",
"MapGroup": "components/MapGroup.coffee",
"MergeGroups": "components/MergeGroups.coffee",
"Objectify": "components/Objectify.coffee",
@@ -80,4 +81,4 @@
"scripts": {
"test": "grunt test"
}
-}
+}
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 9f594e2

Please sign in to comment.