UNPKG

949 Btext/coffeescriptView Raw
1noflo = require 'noflo'
2
3exports.getComponent = ->
4 c = new noflo.Component
5 c.description = 'Forward packet after a set delay'
6 c.icon = 'clock-o'
7
8 c.timers = []
9
10 c.inPorts.add 'in',
11 datatype: 'all'
12 description: 'Packet to be forwarded with a delay'
13 c.inPorts.add 'delay',
14 datatype: 'number'
15 description: 'How much to delay'
16 default: 500
17 control: true
18
19 c.outPorts.add 'out',
20 datatype: 'all'
21
22 c.tearDown = (callback) ->
23 clearTimeout timer for timer in c.timers
24 c.timers = []
25 callback()
26
27 c.process (input, output) ->
28 return unless input.hasData 'in'
29 return if input.attached('delay').length and not input.hasData 'delay'
30
31 delay = 500
32 if input.hasData 'delay'
33 delay = input.getData 'delay'
34 payload = input.get 'in'
35
36 timer = setTimeout ->
37 c.timers.splice c.timers.indexOf(timer), 1
38 output.sendDone
39 out: payload
40 , delay
41 c.timers.push timer