Skip to content

Commit

Permalink
Make scope-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Oct 23, 2017
1 parent 3cb316f commit 3fbf924
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions components/RunTimeout.coffee
Expand Up @@ -5,7 +5,7 @@ exports.getComponent = ->
c.description = 'Send a packet after the given time in ms'
c.icon = 'clock-o'

c.timer = null
c.timer = {}

c.inPorts.add 'time',
datatype: 'number'
Expand All @@ -21,27 +21,28 @@ exports.getComponent = ->
c.forwardBrackets =
start: ['out']

c.stopTimer = ->
return unless c.timer
clearTimeout c.timer.timeout
c.timer.deactivate()
c.timer = null
c.stopTimer = (scope) ->
return unless c.timer[scope]
clearTimeout c.timer[scope].timeout
c.timer[scope].deactivate()
delete c.timer[scope]

c.tearDown = (callback) ->
c.stopTimer()
for scope, timer of c.timer
c.stopTimer scope
callback()

c.process (input, output, context) ->
return unless input.hasData 'time', 'start'
time = input.getData 'time'
bang = input.getData 'start'
# Ensure we deactivate previous timeout, if any
c.stopTimer()
c.stopTimer input.scope
# Set up new timer
context.timeout = setTimeout ->
c.timer = null
output.sendDone
out: true
, time
c.timer = context
c.timer[input.scope] = context
return

0 comments on commit 3fbf924

Please sign in to comment.