UNPKG

1.18 kBtext/coffeescriptView Raw
1noflo = require 'noflo'
2
3class RunTimeout extends noflo.Component
4 description: 'Send a packet after the given time in ms'
5 icon: 'clock-o'
6 constructor: ->
7 @timer = null
8 @time = null
9 @inPorts = new noflo.InPorts
10 time:
11 datatype: 'number'
12 description: 'Time after which a packet will be sent'
13 start:
14 datatype: 'bang'
15 description: 'Start the timeout before sending a packet'
16 clear:
17 datatype: 'bang'
18 description: 'Clear the timeout'
19 required: no
20 @outPorts = new noflo.OutPorts
21 out:
22 datatype: 'bang'
23
24 @inPorts.time.on 'data', (@time) =>
25 @startTimer()
26
27 @inPorts.start.on 'data', =>
28 @startTimer()
29
30 @inPorts.clear.on 'data', =>
31 @stopTimer() if @timer
32
33 startTimer: ->
34 @stopTimer() if @timer
35 @outPorts.out.connect()
36 @timer = setTimeout =>
37 @outPorts.out.send true
38 @outPorts.out.disconnect()
39 @timer = null
40 , @time
41
42 stopTimer: ->
43 return unless @timer
44 clearTimeout @timer
45 @timer = null
46 @outPorts.out.disconnect()
47
48 shutdown: ->
49 @stopTimer() if @timer
50
51exports.getComponent = -> new RunTimeout