Browse files

Added Throttle component.

  • Loading branch information...
1 parent aad7c76 commit 3263445e82f9d2d19a7a5388d3bf5bc3c186ea03 @rybesh rybesh committed Apr 17, 2012
Showing with 48 additions and 0 deletions.
  1. +48 −0 src/components/Throttle.coffee
View
48 src/components/Throttle.coffee
@@ -0,0 +1,48 @@
+noflo = require "noflo"
+util = require "util"
+
+class Throttle extends noflo.Component
+
+ constructor: ->
+ @inPorts =
+ in: new noflo.Port()
+ load: new noflo.Port()
+ max: new noflo.Port()
+ @outPorts =
+ out: new noflo.Port()
+
+ @q = []
+ @load = 0
+ @max = 10
+
+ @inPorts.load.on "data", (data) =>
+ @load = data
+ @process()
+
+ @inPorts.max.on "data", (data) =>
+ @max = data
+ @process()
+
+ @inPorts.in.on "begingroup", (group) =>
+ @push "begingroup", group
+ @inPorts.in.on "data", (data) =>
+ @push "data", data
+ @inPorts.in.on "endgroup", =>
+ @push "endgroup"
+ @inPorts.in.on "disconnect", =>
+ @push "disconnect"
+
+ push: (eventname, data) ->
+ @q.push { name: eventname, data: data }
+ @process()
+
+ process: ->
+ while @q.length > 0 and @load < @max
+ event = @q.shift()
+ switch event.name
+ when "begingroup" then @outPorts.out.beginGroup event.data
+ when "data" then @outPorts.out.send event.data
+ when "endgroup" then @outPorts.out.endGroup()
+ when "disconnect" then @outPorts.out.disconnect()
+
+exports.getComponent = -> new Throttle()

0 comments on commit 3263445

Please sign in to comment.