on master help sign in
global timer and delay update at Elm.timer.{now(),addDelay(d)}
commit494dfdbd5335ff90b0a57c198298ea505e8c4997 authorMichael James <michael@michaelbjames.com> dateFri 18 Jul 2014, 13:55 (12 years ago) parentfb4e298
5 files changed+41−30
libraries/Native/Signal/Signal.js+1−1 libraries/Native/Signal/Time.js+6−13 libraries/Native/Signal/Touch.js+2−2 runtime/Debugger.js+7−13 runtime/Init.js+25−1
libraries/Native/Signal/Signal.js+1−1
@@ -138,7 +138,7 @@ Elm.Native.Signal.make = function(elm) {
138138 }
139139
140140 function timestamp(a) {
141− function update() { return Utils.Tuple2(Date.now(), a.value); }
141+ function update() { return Utils.Tuple2(Elm.timer.now(), a.value); }
142142 return new LiftN(update, [a]);
143143 }
144144
libraries/Native/Signal/Time.js+6−13
@@ -12,16 +12,10 @@ Elm.Native.Time.make = function(elm) {
1212
1313 function fpsWhen(desiredFPS, isOn) {
1414 var msPerFrame = 1000 / desiredFPS;
15− var prev = Date.now(), curr = prev, diff = 0, wasOn = true;
15+ var prev = Elm.timer.now(), curr = prev, diff = 0, wasOn = true;
1616 var ticker = NS.input(diff);
17− function tick(zero) { return function(offset) {
18− /**
19− * The offset is used for the debugger module. To pause and
20− * unpause time in a continuous way (not jumping ahead by seconds)
21− * or minutes, we introduce a way to offset real time by the amount
22− * of time spend paused.
23− */
24− curr = Date.now() - (offset|0);
17+ function tick(zero) { return function() {
18+ curr = Elm.timer.now();
2519 diff = zero ? 0 : curr - prev;
2620 prev = curr;
2721 elm.notify(ticker.id, diff);
@@ -41,10 +35,9 @@ Elm.Native.Time.make = function(elm) {
4135 }
4236
4337 function every(t) {
44− var clock = NS.input(Date.now());
45− function tellTime(offset) {
46− // Leave the offset. It's for debugging. See above.
47− elm.notify(clock.id, Date.now() - (offset|0));
38+ var clock = NS.input(Elm.timer.now());
39+ function tellTime() {
40+ elm.notify(clock.id, Elm.timer.now());
4841 elm.runDelayed(tellTime, t);
4942 }
5043 elm.runDelayed(tellTime, t);
libraries/Native/Signal/Touch.js+2−2
@@ -61,11 +61,11 @@ Elm.Native.Touch.make = function(elm) {
6161 dict.insert(e.identifier,
6262 {x: point._0,
6363 y: point._1,
64− t: Date.now()});
64+ t: Elm.timer.now()});
6565 }
6666 function end(e) {
6767 var t = dict.remove(e.identifier);
68− if (Date.now() - t.t < tapTime) {
68+ if (Elm.timer.now() - t.t < tapTime) {
6969 hasTap = true;
7070 tap = {_:{}, x:t.x, y:t.y};
7171 }
runtime/Debugger.js+7−13
@@ -40,7 +40,6 @@ function debugModule(module, runtime) {
4040 var recordedEvents = [];
4141 var asyncCallbacks = [];
4242 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
43− var timerOffset = 0;
4443 var now = 0;
4544
4645 function wrapNotify(id, v) {
@@ -70,7 +69,7 @@ function debugModule(module, runtime) {
7069 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
7170 var timerId = setTimeout(function() {
7271 cbObj.executed = true;
73− func(timerOffset);
72+ func();
7473 }, delayMs);
7574 cbObj.timerId = timerId;
7675 asyncCallbacks.push(cbObj);
@@ -110,10 +109,6 @@ function debugModule(module, runtime) {
110109 recordedEvents = events.slice();
111110 }
112111
113− function resetTimeOffset() {
114− timerOffset = 0;
115− }
116−
117112 function setPaused() {
118113 programPaused = true;
119114 clearAsyncCallbacks();
@@ -122,13 +117,14 @@ function debugModule(module, runtime) {
122117 }
123118
124119 function setContinue(position) {
125− timerOffset += Date.now() - now;
120+ var timerDelay = Date.now() - now;
121+ Elm.timer.addDelay(timerDelay);
126122 programPaused = false;
127123 if (position > 0) {
128124 // If we're not unpausing at the head, then we need to dump the
129125 // events that are ahead of where we're continuing.
130126 recordedEvents = recordedEvents.slice(0,position);
131− executeCallbacks(asyncCallbacks, false, timerOffset);
127+ executeCallbacks(asyncCallbacks, false);
132128 }
133129 tracePath.startRecording();
134130 }
@@ -173,7 +169,6 @@ function debugModule(module, runtime) {
173169 getRecordedEventAt: getRecordedEventAt,
174170 copyRecordedEvents: copyRecordedEvents,
175171 loadRecordedEvents: loadRecordedEvents,
176− resetTimeOffset: resetTimeOffset,
177172 getPaused: getPaused,
178173 setPaused: setPaused,
179174 setContinue: setContinue,
@@ -197,8 +192,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
197192 debugModule.tracePath.clearTraces();
198193 debugModule.clearRecordedEvents();
199194 debugModule.setContinue(0);
200− debugModule.resetTimeOffset();
201− executeCallbacks(debugModule.initialAsyncCallbacks, true, 0);
195+ executeCallbacks(debugModule.initialAsyncCallbacks, true);
202196 }
203197
204198 function pauseProgram() {
@@ -453,12 +447,12 @@ function tracePathInit(runtime, mainNode) {
453447}
454448
455449
456−function executeCallbacks(callbacks, reexecute, offset) {
450+function executeCallbacks(callbacks, reexecute) {
457451 callbacks.forEach(function(timer) {
458452 if (reexecute || !timer.executed) {
459453 var func = timer.func;
460454 timer.executed = true;
461− func(offset);
455+ func();
462456 }
463457 });
464458}
runtime/Init.js+25−1
@@ -27,6 +27,30 @@ Elm.worker = function(module, ports) {
2727 return init(ElmRuntime.Display.NONE, {}, module, ports || {});
2828};
2929
30+/* OFFSET
31+ * Elm's time traveling debugger lets you interrupt the smooth flow of time
32+ * by pausing and continuing program execution. To ensure the user sees a
33+ * program that moves smoothly through the pause/continue time gap,
34+ * we need to adjsut the value of Date.now().
35+ */
36+Elm.timer = function() {
37+ var inducedDelay = 0;
38+
39+ var now = function() {
40+ return Date.now() - inducedDelay;
41+ };
42+
43+ var addDelay = function(d) {
44+ var delay = (d|0);
45+ inducedDelay += delay;
46+ return inducedDelay;
47+ }
48+
49+ return { now : now
50+ , addDelay : addDelay
51+ }
52+}();
53+
3054function init(display, container, module, ports, moduleToReplace) {
3155 // defining state needed for an instance of the Elm RTS
3256 var inputs = [];
@@ -40,7 +64,7 @@ function init(display, container, module, ports, moduleToReplace) {
4064 'Definitely report this to <https://github.com/evancz/Elm/issues>\n');
4165 }
4266 updateInProgress = true;
43− var timestep = Date.now();
67+ var timestep = Elm.timer.now();
4468 for (var i = inputs.length; i--; ) {
4569 inputs[i].recv(timestep, id, v);
4670 }