1 | noflo = require 'noflo'
|
2 |
|
3 | exports.getComponent = ->
|
4 | c = new noflo.Component
|
5 | c.description = 'Send a packet at the given interval'
|
6 | c.icon = 'clock-o'
|
7 | c.inPorts.add 'interval',
|
8 | datatype: 'number'
|
9 | description: 'Interval at which output packets are emitted (ms)'
|
10 | required: true
|
11 | control: true
|
12 | c.inPorts.add 'start',
|
13 | datatype: 'bang'
|
14 | description: 'Start the emission'
|
15 | c.inPorts.add 'stop',
|
16 | datatype: 'bang'
|
17 | description: 'Stop the emission'
|
18 | c.outPorts.add 'out',
|
19 | datatype: 'bang'
|
20 |
|
21 | c.timers = {}
|
22 |
|
23 | cleanUp = (scope) ->
|
24 | return unless c.timers[scope]
|
25 | clearInterval c.timers[scope].interval
|
26 | c.timers[scope].deactivate()
|
27 | c.timers[scope] = null
|
28 |
|
29 | c.tearDown = (callback) ->
|
30 | for scope, context of c.timers
|
31 | cleanUp scope
|
32 | c.timers = {}
|
33 | callback()
|
34 |
|
35 | c.forwardBrackets = {}
|
36 | c.process (input, output, context) ->
|
37 | if input.hasData 'start'
|
38 | return unless input.hasData 'interval'
|
39 | start = input.get 'start'
|
40 | return unless start.type is 'data'
|
41 | interval = parseInt input.getData 'interval'
|
42 |
|
43 | cleanUp start.scope
|
44 |
|
45 |
|
46 | context.interval = setInterval ->
|
47 | bang = new noflo.IP 'data', true
|
48 | bang.scope = start.scope
|
49 | c.outPorts.out.sendIP bang
|
50 | , interval
|
51 |
|
52 |
|
53 | c.timers[start.scope] = context
|
54 | return
|
55 |
|
56 | if input.hasData 'stop'
|
57 | stop = input.get 'stop'
|
58 | return unless stop.type is 'data'
|
59 |
|
60 | cleanUp stop.scope
|
61 | return
|