on master help sign in
Merge pull request #691 from michaelbjames/debugger

Debugger fix for time jumping

commit93a9d61a4ddeb8a1f901822b5bb1f37eaf62bd7c authorEvan Czaplicki <info@elm-lang.org> dateMon 21 Jul 2014, 16:42 (12 years ago) parent74e3a592a3eb06
5 files changed+39−8
libraries/Native/Signal/Signal.js+1−1 libraries/Native/Signal/Time.js+4−4 libraries/Native/Signal/Touch.js+2−2 runtime/Debugger.js+4 runtime/Init.js+28−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+4−4
@@ -12,10 +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);
1717 function tick(zero) { return function() {
18− curr = Date.now();
18+ curr = elm.timer.now();
1919 diff = zero ? 0 : curr - prev;
2020 prev = curr;
2121 elm.notify(ticker.id, diff);
@@ -35,9 +35,9 @@ Elm.Native.Time.make = function(elm) {
3535 }
3636
3737 function every(t) {
38− var clock = NS.input(Date.now());
38+ var clock = NS.input(elm.timer.now());
3939 function tellTime() {
40− elm.notify(clock.id, Date.now());
40+ elm.notify(clock.id, elm.timer.now());
4141 elm.runDelayed(tellTime, t);
4242 }
4343 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+4
@@ -40,6 +40,7 @@ function debugModule(module, runtime) {
4040 var recordedEvents = [];
4141 var asyncCallbacks = [];
4242 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
43+ var now = 0;
4344
4445 function wrapNotify(id, v) {
4546 var timestep = Date.now();
@@ -112,9 +113,12 @@ function debugModule(module, runtime) {
112113 programPaused = true;
113114 clearAsyncCallbacks();
114115 tracePath.stopRecording();
116+ now = Date.now();
115117 }
116118
117119 function setContinue(position) {
120+ var timerDelay = Date.now() - now;
121+ runtime.timer.addDelay(timerDelay);
118122 programPaused = false;
119123 if (position > 0) {
120124 // If we're not unpausing at the head, then we need to dump the
runtime/Init.js+28−1
@@ -31,6 +31,32 @@ function init(display, container, module, ports, moduleToReplace) {
3131 // defining state needed for an instance of the Elm RTS
3232 var inputs = [];
3333
34+ /* OFFSET
35+ * Elm's time traveling debugger lets you interrupt the smooth flow of time
36+ * by pausing and continuing program execution. To ensure the user sees a
37+ * program that moves smoothly through the pause/continue time gap,
38+ * we need to adjsut the value of Date.now().
39+ */
40+ var timer = function() {
41+ var inducedDelay = 0;
42+
43+ var now = function() {
44+ return Date.now() - inducedDelay;
45+ };
46+
47+ var addDelay = function(d) {
48+ if (typeof d !== "number") {
49+ throw "addDelay must take a number (preferably an int), not: " + d
50+ }
51+ inducedDelay += d;
52+ return inducedDelay;
53+ }
54+
55+ return { now : now
56+ , addDelay : addDelay
57+ }
58+ }();
59+
3460 var updateInProgress = false;
3561 function notify(id, v) {
3662 if (updateInProgress) {
@@ -40,7 +66,7 @@ function init(display, container, module, ports, moduleToReplace) {
4066 'Definitely report this to <https://github.com/evancz/Elm/issues>\n');
4167 }
4268 updateInProgress = true;
43− var timestep = Date.now();
69+ var timestep = timer.now();
4470 for (var i = inputs.length; i--; ) {
4571 inputs[i].recv(timestep, id, v);
4672 }
@@ -72,6 +98,7 @@ function init(display, container, module, ports, moduleToReplace) {
7298 id:ElmRuntime.guid(),
7399 addListener:addListener,
74100 inputs:inputs,
101+ timer:timer,
75102 ports: { incoming:ports, outgoing:{}, uses:portUses }
76103 };
77104