1 | noflo = require 'noflo'
|
2 |
|
3 | exports.getComponent = ->
|
4 | c = new noflo.Component
|
5 | c.description = 'Send a packet after the given time in ms'
|
6 | c.icon = 'clock-o'
|
7 |
|
8 | c.timer = {}
|
9 |
|
10 | c.inPorts.add 'time',
|
11 | datatype: 'number'
|
12 | description: 'Time after which a packet will be sent'
|
13 | required: true
|
14 | control: true
|
15 | c.inPorts.add 'start',
|
16 | datatype: 'bang'
|
17 | description: 'Start the timeout before sending a packet'
|
18 | c.outPorts.add 'out',
|
19 | datatype: 'bang'
|
20 |
|
21 | c.forwardBrackets =
|
22 | start: ['out']
|
23 |
|
24 | c.stopTimer = (scope) ->
|
25 | return unless c.timer[scope]
|
26 | clearTimeout c.timer[scope].timeout
|
27 | c.timer[scope].deactivate()
|
28 | delete c.timer[scope]
|
29 |
|
30 | c.tearDown = (callback) ->
|
31 | for scope, timer of c.timer
|
32 | c.stopTimer scope
|
33 | callback()
|
34 |
|
35 | c.process (input, output, context) ->
|
36 | return unless input.hasData 'time', 'start'
|
37 | time = input.getData 'time'
|
38 | bang = input.getData 'start'
|
39 |
|
40 | c.stopTimer input.scope
|
41 |
|
42 | context.timeout = setTimeout ->
|
43 | c.timer = null
|
44 | output.sendDone
|
45 | out: true
|
46 | , time
|
47 | c.timer[input.scope] = context
|
48 | return
|