on master help sign in
Get timers to detach themselves when a swap occurs.
commitb30482a38f515937eb12ee8d5ba8deee3262c3f0 authorevancz <info@elm-lang.org> dateMon 3 Jun 2013, 00:19 (13 years ago) parent90cf421
2 files changed+10−3
libraries/Native/Signal/Time.js+5−2 runtime/Init.js+5−1
libraries/Native/Signal/Time.js+5−2
@@ -32,8 +32,11 @@ Elm.Native.Time = function(elm) {
3232
3333 function everyWhen(t, isOn) {
3434 var clock = Signal.constant(Date.now());
35− function tellTime() { elm.notify(clock.id, Date.now()); }
36− setInterval(tellTime, t);
35+ var id = setInterval(function tellTime() {
36+ if (!elm.notify(clock.id, Date.now())) {
37+ clearInterval(id);
38+ }
39+ }, t);
3740 return clock;
3841 }
3942
runtime/Init.js+5−1
@@ -33,9 +33,12 @@ function init(display, container, module, moduleToReplace) {
3333
3434 function notify(id, v) {
3535 var timestep = Date.now();
36+ var changed = false;
3637 for (var i = inputs.length; i--; ) {
37− inputs[i].recv(timestep, id, v);
38+ // order is important here to avoid short-circuiting
39+ changed = inputs[i].recv(timestep, id, v) || changed;
3840 }
41+ return changed;
3942 }
4043
4144 container.offsetX = 0;
@@ -86,6 +89,7 @@ function init(display, container, module, moduleToReplace) {
8689 removeListeners(listeners);
8790 var div = document.createElement('div');
8891 var newElm = init(display, div, newModule, elm);
92+ inputs = [];
8993 // elm.send = newElm.send;
9094 // elm.recv = newElm.recv;
9195 // elm.swap = newElm.swap;