Skip to content

Commit

Permalink
implemented pause/resume for delayed events!
Browse files Browse the repository at this point in the history
  • Loading branch information
Touffy committed May 9, 2013
1 parent 1e32077 commit f183986
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 2 additions & 2 deletions SCxml.js
Expand Up @@ -100,11 +100,11 @@ SCxml.prototype={

timeout: function(args)
{
return new Timeout(args, !this.paused)
return new Timeout(args, !this.paused, this)
},
interval: function(args)
{
return new Interval(args, !this.paused)
return new Interval(args, !this.paused, this)
},

clean: function()
Expand Down
5 changes: 4 additions & 1 deletion SCxmlDebug.js
Expand Up @@ -51,7 +51,9 @@ SCxml.prototype.pause=function()
if(this.paused || !this.running) return;
this.paused=true
this.html.dispatchEvent(new Event("pause"))
// todo: pause timers
for(var i=0, sends=sc.dom.querySelectorAll("send"), send; send=sends[i]; i++)
if(send.sent && send.sent.length)
for(var j=0, timer; timer=send.sent[j]; j++) timer.stop()
}

// resume a running SC
Expand All @@ -61,5 +63,6 @@ SCxml.prototype.resume=function()
this.nextPauseBefore=0
this.paused=false
this.html.dispatchEvent(new Event("resume"))
for(var timer; timer=this.timeouts.pop(); timer.start());
this.mainEventLoop()
}
5 changes: 2 additions & 3 deletions SCxmlExecute.js
Expand Up @@ -152,7 +152,7 @@ SCxml.executableContent={
var e=proc.createEvent(event, sc, data, element)
if(delay > -1)
(element.sent || (element.sent=[])).push(
window.setTimeout(proc.send, delay, e, target, element, sc))
new Delay(delay, !sc.paused, sc, proc, e, target, element))
else proc.send(e, target, element, sc)
},

Expand All @@ -161,8 +161,7 @@ SCxml.executableContent={
var id=element.getAttribute("sendid")
||sc.expr(element.getAttribute("sendidexpr"))
for(var timer, sent=sc.dom.querySelector("send[id="+id+"]").sent;
timer=sent.pop();)
try{window.clearTimeout(timer)} catch(err){}
timer=sent.pop(); timer.cancel());
},

log: function(sc, element)
Expand Down
30 changes: 25 additions & 5 deletions delays.js
Expand Up @@ -2,36 +2,50 @@
wraps setTimeout and setInterval calls so they can be paused and resumed
*/

function Delay(delay, startNow, sc, proc, e, target, element)
{
this.executed=false
this.time=delay
this.event=e
this.target=target
this.proc=proc
this.sc=sc
this.element=element
if(startNow) this.start()
else this.sc.timeouts.push(this)
}

function Timeout(args, startNow, sc)
{
this.executed=false
this.time=args[1]
this.f=args[0]
this.args=[]
this.sc=sc
// this.element=sc.datamodel._element
for(var i=2; args[i]; i++) this.args.push(args[i])
if(startNow) this.start()
else this.sc.timeouts.push(this)
}

Timeout.timesUp=function(t){ t.timesUp() }
Delay.timesUp=function(t){ t.timesUp() }

Timeout.prototype.start=function()
Delay.prototype.start=Timeout.prototype.start=function()
{
if(!this.timer && !this.executed)
{
this.timer=setTimeout(this.time, Timeout.timesUp, this)
this.timer=setTimeout(Delay.timesUp, this.time, this)
this.started=+new Date()
}
}
Timeout.prototype.cancel=function()
Delay.prototype.cancel=Timeout.prototype.cancel=function()
{
if(this.timer && !this.executed){
clearTimeout(this.timer)
delete this.timer
}
}
Timeout.prototype.stop=function()
Delay.prototype.stop=Timeout.prototype.stop=function()
{
if(this.timer && !this.executed && this.sc.running){
this.time-=new Date()-this.started
Expand All @@ -44,6 +58,12 @@ Timeout.prototype.stop=function()
return false
}

Delay.prototype.timesUp=function()
{
this.executed=true
with(this) proc.send(event, target, element, sc)
}

Timeout.prototype.timesUp=function()
{
this.executed=true
Expand Down

0 comments on commit f183986

Please sign in to comment.