6Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
8 make: function(runtime) {
9 var wrappedModule = debugModule(module, runtime);
10 Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState);
11 dispatchElmDebuggerInit();
12 return wrappedModule.moduleInstance;
17function dispatchElmDebuggerInit() {
19 var dispatch = function(_) {
20 parent.window.postMessage("elmDebuggerInit", window.location.origin);
22 if (parent.window.document.readyState === "complete") {
26 parent.window.addEventListener("load", dispatch);
31function debugModule(module, runtime) {
32 var programPaused = false;
33 var recordedEvents = [];
34 var asyncCallbacks = [];
35 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
37 function wrapNotify(id, v) {
38 var timestep = Date.now();
41 // ignore async events generated while playing back
42 // or user events while program is paused
46 var changed = runtime.notify(id, v, timestep);
47 recordEvent(id, v, timestep);
49 parent.window.postMessage("elmNotify", window.location.origin);
55 function wrapRunDelayed(func, delayMs) {
56 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
57 var timerId = setTimeout(function() {
58 cbObj.executed = true;
61 cbObj.timerId = timerId;
62 asyncCallbacks.push(cbObj);
66 function recordEvent(id, v, timestep) {
67 watchTracker.pushFrame();
68 recordedEvents.push({ id:id, value:v, timestep:timestep });
71 function clearAsyncCallbacks() {
72 asyncCallbacks.forEach(function(timer) {
73 if (!timer.executed) {
74 clearTimeout(timer.timerId);
79 function clearRecordedEvents() {
83 function getRecordedEventsLength() {
84 return recordedEvents.length;
87 function getRecordedEventAt(i) {
88 return recordedEvents[i];
91 function copyRecordedEvents() {
92 return recordedEvents.slice();
95 function loadRecordedEvents(events) {
96 recordedEvents = events.slice();
99 function setPaused(v) {
102 clearAsyncCallbacks();
103 tracePath.stopRecording();
106 // executeCallbacks should only be called when continuing, not when restarting.
107 //executeCallbacks(asyncCallbacks, false);
108 tracePath.startRecording();
112 function getPaused() {
113 return programPaused;
116 // runtime is the prototype of wrappedRuntime
117 // so we can access all runtime properties too
118 var wrappedRuntime = Object.create(runtime);
119 wrappedRuntime.notify = wrapNotify;
120 wrappedRuntime.runDelayed = wrapRunDelayed;
122 var assignedPropTracker = Object.create(wrappedRuntime);
123 var moduleInstance = module.make(assignedPropTracker);
125 // make sure the signal graph is actually a signal & extract the visual model
126 if ( !('recv' in moduleInstance.main) ) {
127 moduleInstance.main = Elm.Signal.make(runtime).constant(moduleInstance.main);
130 // The main module stores imported modules onto the runtime.
131 // To ensure only one instance of each module is created,
132 // we assign them back on the original runtime object.
133 Object.keys(assignedPropTracker).forEach(function(key) {
134 runtime[key] = assignedPropTracker[key];
137 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
138 var tracePath = tracePathInit(runtime, moduleInstance.main);
141 moduleInstance: moduleInstance,
142 moduleNodes: moduleNodes,
143 initialNodeValues: saveNodeValues(moduleNodes),
144 initialAsyncCallbacks: asyncCallbacks.slice(),
146 clearAsyncCallbacks: clearAsyncCallbacks,
147 clearRecordedEvents: clearRecordedEvents,
148 getRecordedEventsLength: getRecordedEventsLength,
149 getRecordedEventAt: getRecordedEventAt,
150 copyRecordedEvents: copyRecordedEvents,
151 loadRecordedEvents: loadRecordedEvents,
152 getPaused: getPaused,
153 setPaused: setPaused,
154 tracePath: tracePath,
155 watchTracker: watchTracker,
159function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
160 var eventCounter = 0;
162 function resetProgram() {
163 debugModule.clearAsyncCallbacks();
164 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
168 function restartProgram() {
170 debugModule.watchTracker.clear();
171 debugModule.tracePath.clearTraces();
172 debugModule.clearRecordedEvents();
173 debugModule.setPaused(false);
174 executeCallbacks(debugModule.initialAsyncCallbacks, true);
177 function pauseProgram() {
178 debugModule.setPaused(true);
179 eventCounter = debugModule.getRecordedEventsLength();
182 function continueProgram() {
183 if (debugModule.getPaused())
185 debugModule.setPaused(false);
189 function stepTo(index) {
190 if (!debugModule.getPaused()) {
191 debugModule.setPaused(true);
195 if (index < 0 || index > getMaxSteps()) {
196 throw "Index out of bounds: " + index;
199 if (index < eventCounter) {
204 assert(index >= eventCounter, "index must be bad");
205 while (eventCounter < index) {
206 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
207 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
211 assert(eventCounter == index, "while loop didn't work");
214 function getMaxSteps() {
215 return debugModule.getRecordedEventsLength();
218 function doPlayback(eventList) {
219 var x = eventList.shift();
221 runtime.notify(x[0], x[1], x[2]);
223 if (eventList.length > 0) {
224 var delta = eventList[0][2] - time;
225 setTimeout(function() { doPlayback(eventList); }, delta);
229 function redrawGraphics() {
230 var graphicsNode = debugModule.moduleInstance.main.kids[0];
231 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
234 function getHotSwapState() {
235 var counter = eventCounter;
236 if (!debugModule.getPaused()) {
237 counter = getMaxSteps();
240 recordedEvents: debugModule.copyRecordedEvents(),
241 eventCounter: counter
246 var parentNode = runtime.node.parentNode;
247 parentNode.removeChild(debugModule.tracePath.canvas);
248 parentNode.removeChild(runtime.node);
252 debugModule.setPaused(true);
253 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
255 // draw new trace path
256 debugModule.tracePath.startRecording();
257 stepTo(getMaxSteps());
258 debugModule.tracePath.stopRecording();
260 stepTo(hotSwapState.eventCounter);
263 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
266 restart: restartProgram,
268 kontinue: continueProgram,
269 getMaxSteps: getMaxSteps,
271 getPaused: debugModule.getPaused,
272 getHotSwapState: getHotSwapState,
274 allNodes: debugModule.moduleNodes,
275 watchTracker: debugModule.watchTracker,
276 mainNode: debugModule.moduleInstance.main
282function Point(x, y) {
286 this.translate = function(x, y) {
287 return new Point(this.x + x, this.y + y);
290 this.equals = function(p) {
291 return this.x == p.x && this.y == p.y;
295function tracePathInit(runtime, mainNode) {
296 var List = Elm.List.make(runtime);
297 var Signal = Elm.Signal.make(runtime);
298 var tracePathNode = A2(Signal.lift, graphicsUpdate, mainNode);
299 var tracePathCanvas = createCanvas();
300 var tracePositions = {};
301 var recordingTraces = true;
303 function findPositions(currentScene) {
305 function processElement(elem, offset) {
306 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
308 List.map(F2(processForm)(offset))(elem.element.model.forms);
310 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
312 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
316 function processForm(offset, form) {
317 if (form.form.ctor == "FElement")
319 processElement(form.form._0, offset.translate(form.x, -form.y));
323 processElement(currentScene, new Point(0, 0));
327 function appendPositions(positions) {
328 for (var id in positions) {
329 var pos = positions[id];
330 if (tracePositions.hasOwnProperty(id)) {
331 var points = tracePositions[id];
332 if (!pos.equals(points[points.length-1])) {
337 tracePositions[id] = [pos];
342 function graphicsUpdate(currentScene) {
343 if (!recordingTraces) {
347 var ctx = tracePathCanvas.getContext('2d');
348 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
351 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
352 appendPositions(findPositions(currentScene));
353 for (var id in tracePositions)
356 var points = tracePositions[id];
357 for (var i=0; i < points.length; i++)
361 ctx.moveTo(p.x, p.y);
364 ctx.lineTo(p.x, p.y);
368 ctx.strokeStyle = "#888a85";
371 for (var i=0; i < points.length; i++)
375 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
376 ctx.fillStyle = "#2e3436";
384 function clearTraces() {
388 function stopRecording() {
389 recordingTraces = false;
392 function startRecording() {
393 recordingTraces = true;
397 graphicsUpdate: graphicsUpdate,
398 canvas: tracePathCanvas,
399 clearTraces: clearTraces,
400 stopRecording: stopRecording,
401 startRecording: startRecording
406function executeCallbacks(callbacks, reexecute) {
407 callbacks.forEach(function(timer) {
408 if (reexecute || !timer.executed) {
409 var func = timer.func;
415function createCanvas() {
416 var c = document.createElement('canvas');
417 c.width = window.innerWidth;
418 c.height = window.innerHeight;
419 c.style.position = "absolute";
422 c.style.pointerEvents = "none";
426function assert(bool, msg) {
428 throw "Assertion error: " + msg;
432function saveNodeValues(allNodes) {
435 allNodes.forEach(function(node) {
436 nodeValues.push({ value: node.value, id: node.id });
443function restoreNodeValues(allNodes, nodeStates) {
444 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
445 for (var i=0; i < allNodes.length; i++) {
446 var node = allNodes[i];
447 var state = nodeStates[i];
448 assert(node.id == state.id, "the nodes moved position");
450 node.value = state.value;
454function flattenNodes(nodes) {
457 function addAllToDict(node) {
458 nodesById[node.id] = node;
459 node.kids.forEach(addAllToDict);
461 nodes.forEach(addAllToDict);
463 var allNodes = Object.keys(nodesById).sort().map(function(key) {
464 return nodesById[key];
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list