on master help sign in
Debugger: hot swapping with preserved debugger state!
commited0d84bbb4914a0a008389d348ab3f2e065c307c authorLaszlo Pandy <laszlo.pandy@prezi.com> dateTue 5 Nov 2013, 18:55 (13 years ago) parent521f94b
1 file changed+46−4
runtime/Debugger.js+46−4
runtime/Debugger.js+46−4
@@ -3,11 +3,11 @@
33'use strict';
44
55Elm.Debugger = null;
6−Elm.debuggerAttach = function(module) {
6+Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
77 return {
88 make: function(runtime) {
99 var wrappedModule = debugModule(module, runtime);
10− Elm.Debugger = debuggerInit(wrappedModule, runtime);
10+ Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState);
1111 dispatchElmDebuggerInit();
1212 return wrappedModule.moduleInstance;
1313 }
@@ -84,6 +84,14 @@ function debugModule(module, runtime) {
8484 return recordedEvents[i];
8585 }
8686
87+ function copyRecordedEvents() {
88+ return recordedEvents.slice();
89+ }
90+
91+ function loadRecordedEvents(events) {
92+ recordedEvents = events.slice();
93+ }
94+
8795 function setPaused(v) {
8896 programPaused = v;
8997 if (programPaused) {
@@ -125,13 +133,15 @@ function debugModule(module, runtime) {
125133 clearRecordedEvents: clearRecordedEvents,
126134 getRecordedEventsLength: getRecordedEventsLength,
127135 getRecordedEventAt: getRecordedEventAt,
136+ copyRecordedEvents: copyRecordedEvents,
137+ loadRecordedEvents: loadRecordedEvents,
128138 getPaused: getPaused,
129139 setPaused: setPaused,
130140 tracePath: tracePath
131141 };
132142}
133143
134−function debuggerInit(debugModule, runtime) {
144+function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
135145 var Signal = Elm.Signal.make(runtime);
136146
137147 var tracePathNode = A2(Signal.lift, debugModule.tracePath.graphicsUpdate, debugModule.moduleInstance.main);
@@ -210,13 +220,45 @@ function debuggerInit(debugModule, runtime) {
210220 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
211221 }
212222
223+ function getHotSwapState() {
224+ if (debugModule.getPaused()) {
225+ return {
226+ recordedEvents: debugModule.copyRecordedEvents(),
227+ eventCounter: eventCounter
228+ };
229+ }
230+ else {
231+ return null;
232+ }
233+ }
234+
235+ function dispose() {
236+ var parentNode = runtime.node.parentNode;
237+ parentNode.removeChild(debugModule.tracePath.canvas);
238+ parentNode.removeChild(runtime.node);
239+ }
240+
241+ if (hotSwapState) {
242+ debugModule.setPaused(true);
243+ debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
244+
245+ // draw new trace path
246+ debugModule.tracePath.startRecording();
247+ stepTo(getMaxSteps());
248+ debugModule.tracePath.stopRecording();
249+
250+ stepTo(hotSwapState.eventCounter);
251+ }
252+
213253 var elmDebugger = {
214254 restart: restartProgram,
215255 pause: pauseProgram,
216256 kontinue: continueProgram,
217257 getMaxSteps: getMaxSteps,
218258 stepTo: stepTo,
219− getPaused: debugModule.getPaused
259+ getPaused: debugModule.getPaused,
260+ getHotSwapState: getHotSwapState,
261+ dispose: dispose
220262 };
221263
222264 return elmDebugger;