on master help sign in
fix for time jumping by introducing a timer offset that hooks into the Time.js library
commitf6120616cfd38f749e315a0213b42fa332b4a6ef authorMichael James <michael@michaelbjames.com> dateFri 18 Jul 2014, 10:20 (12 years ago) parent74e3a59
2 files changed+26−9
libraries/Native/Signal/Time.js+11−4 runtime/Debugger.js+15−5
libraries/Native/Signal/Time.js+11−4
@@ -14,8 +14,14 @@ Elm.Native.Time.make = function(elm) {
1414 var msPerFrame = 1000 / desiredFPS;
1515 var prev = Date.now(), curr = prev, diff = 0, wasOn = true;
1616 var ticker = NS.input(diff);
17− function tick(zero) { return function() {
18− curr = Date.now();
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 || 0);
1925 diff = zero ? 0 : curr - prev;
2026 prev = curr;
2127 elm.notify(ticker.id, diff);
@@ -36,8 +42,9 @@ Elm.Native.Time.make = function(elm) {
3642
3743 function every(t) {
3844 var clock = NS.input(Date.now());
39− function tellTime() {
40− elm.notify(clock.id, 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 || 0));
4148 elm.runDelayed(tellTime, t);
4249 }
4350 elm.runDelayed(tellTime, t);
runtime/Debugger.js+15−5
@@ -40,6 +40,8 @@ function debugModule(module, runtime) {
4040 var recordedEvents = [];
4141 var asyncCallbacks = [];
4242 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
43+ var timerOffset = 0;
44+ var now = 0;
4345
4446 function wrapNotify(id, v) {
4547 var timestep = Date.now();
@@ -68,7 +70,7 @@ function debugModule(module, runtime) {
6870 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
6971 var timerId = setTimeout(function() {
7072 cbObj.executed = true;
71− func();
73+ func(timerOffset);
7274 }, delayMs);
7375 cbObj.timerId = timerId;
7476 asyncCallbacks.push(cbObj);
@@ -108,19 +110,25 @@ function debugModule(module, runtime) {
108110 recordedEvents = events.slice();
109111 }
110112
113+ function resetTimeOffset() {
114+ timerOffset = 0;
115+ }
116+
111117 function setPaused() {
112118 programPaused = true;
113119 clearAsyncCallbacks();
114120 tracePath.stopRecording();
121+ now = Date.now();
115122 }
116123
117124 function setContinue(position) {
125+ timerOffset += Date.now() - now;
118126 programPaused = false;
119127 if (position > 0) {
120128 // If we're not unpausing at the head, then we need to dump the
121129 // events that are ahead of where we're continuing.
122130 recordedEvents = recordedEvents.slice(0,position);
123− executeCallbacks(asyncCallbacks, false);
131+ executeCallbacks(asyncCallbacks, false, timerOffset);
124132 }
125133 tracePath.startRecording();
126134 }
@@ -165,6 +173,7 @@ function debugModule(module, runtime) {
165173 getRecordedEventAt: getRecordedEventAt,
166174 copyRecordedEvents: copyRecordedEvents,
167175 loadRecordedEvents: loadRecordedEvents,
176+ resetTimeOffset: resetTimeOffset,
168177 getPaused: getPaused,
169178 setPaused: setPaused,
170179 setContinue: setContinue,
@@ -188,7 +197,8 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
188197 debugModule.tracePath.clearTraces();
189198 debugModule.clearRecordedEvents();
190199 debugModule.setContinue(0);
191− executeCallbacks(debugModule.initialAsyncCallbacks, true);
200+ debugModule.resetTimeOffset();
201+ executeCallbacks(debugModule.initialAsyncCallbacks, true, 0);
192202 }
193203
194204 function pauseProgram() {
@@ -443,12 +453,12 @@ function tracePathInit(runtime, mainNode) {
443453}
444454
445455
446−function executeCallbacks(callbacks, reexecute) {
456+function executeCallbacks(callbacks, reexecute, offset) {
447457 callbacks.forEach(function(timer) {
448458 if (reexecute || !timer.executed) {
449459 var func = timer.func;
450460 timer.executed = true;
451− func();
461+ func(offset);
452462 }
453463 });
454464}