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 = [];
36 function wrapNotify(id, v) {
37 var timestep = Date.now();
40 // ignore async events generated while playing back
41 // or user events while program is paused
45 var changed = runtime.notify(id, v, timestep);
46 recordEvent(id, v, timestep);
48 parent.window.postMessage("elmNotify", window.location.origin);
54 function wrapRunDelayed(func, delayMs) {
55 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
56 var timerId = setTimeout(function() {
57 cbObj.executed = true;
60 cbObj.timerId = timerId;
61 asyncCallbacks.push(cbObj);
65 function recordEvent(id, v, timestep) {
66 recordedEvents.push({ id:id, value:v, timestep:timestep });
69 function clearAsyncCallbacks() {
70 asyncCallbacks.forEach(function(timer) {
71 if (!timer.executed) {
72 clearTimeout(timer.timerId);
77 function clearRecordedEvents() {
81 function getRecordedEventsLength() {
82 return recordedEvents.length;
85 function getRecordedEventAt(i) {
86 return recordedEvents[i];
89 function copyRecordedEvents() {
90 return recordedEvents.slice();
93 function loadRecordedEvents(events) {
94 recordedEvents = events.slice();
97 function setPaused(v) {
100 clearAsyncCallbacks();
101 tracePath.stopRecording();
104 // executeCallbacks should only be called when continuing, not when restarting.
105 //executeCallbacks(asyncCallbacks, false);
106 tracePath.startRecording();
110 function getPaused() {
111 return programPaused;
114 var wrappedRuntime = {
116 runDelayed: wrapRunDelayed,
118 display: runtime.display,
120 addListener: runtime.addListener,
121 inputs: runtime.inputs
124 var moduleInstance = module.make(wrappedRuntime);
125 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
126 var tracePath = tracePathInit(runtime);
129 moduleInstance: moduleInstance,
130 moduleNodes: moduleNodes,
131 initialNodeValues: saveNodeValues(moduleNodes),
132 initialAsyncCallbacks: asyncCallbacks.slice(),
134 clearAsyncCallbacks: clearAsyncCallbacks,
135 clearRecordedEvents: clearRecordedEvents,
136 getRecordedEventsLength: getRecordedEventsLength,
137 getRecordedEventAt: getRecordedEventAt,
138 copyRecordedEvents: copyRecordedEvents,
139 loadRecordedEvents: loadRecordedEvents,
140 getPaused: getPaused,
141 setPaused: setPaused,
146function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
147 var Signal = Elm.Signal.make(runtime);
149 var tracePathNode = A2(Signal.lift, debugModule.tracePath.graphicsUpdate, debugModule.moduleInstance.main);
150 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
152 var eventCounter = 0;
154 function resetProgram() {
155 debugModule.clearAsyncCallbacks();
156 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
160 function restartProgram() {
162 debugModule.tracePath.clearTraces();
163 debugModule.clearRecordedEvents();
164 debugModule.setPaused(false);
165 executeCallbacks(debugModule.initialAsyncCallbacks, true);
168 function pauseProgram() {
169 debugModule.setPaused(true);
170 eventCounter = debugModule.getRecordedEventsLength();
173 function continueProgram() {
174 if (debugModule.getPaused())
176 debugModule.setPaused(false);
180 function stepTo(index) {
181 if (!debugModule.getPaused()) {
182 debugModule.setPaused(true);
186 if (index < 0 || index > getMaxSteps()) {
187 throw "Index out of bounds: " + index;
190 if (index < eventCounter) {
195 assert(index >= eventCounter, "index must be bad");
196 while (eventCounter < index) {
197 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
198 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
202 assert(eventCounter == index, "while loop didn't work");
205 function getMaxSteps() {
206 return debugModule.getRecordedEventsLength();
209 function doPlayback(eventList) {
210 var x = eventList.shift();
212 runtime.notify(x[0], x[1], x[2]);
214 if (eventList.length > 0) {
215 var delta = eventList[0][2] - time;
216 setTimeout(function() { doPlayback(eventList); }, delta);
220 function redrawGraphics() {
221 var graphicsNode = debugModule.moduleInstance.main.kids[0];
222 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
225 function getHotSwapState() {
226 var counter = eventCounter;
227 if (!debugModule.getPaused()) {
228 counter = getMaxSteps();
231 recordedEvents: debugModule.copyRecordedEvents(),
232 eventCounter: counter
237 var parentNode = runtime.node.parentNode;
238 parentNode.removeChild(debugModule.tracePath.canvas);
239 parentNode.removeChild(runtime.node);
243 debugModule.setPaused(true);
244 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
246 // draw new trace path
247 debugModule.tracePath.startRecording();
248 stepTo(getMaxSteps());
249 debugModule.tracePath.stopRecording();
251 stepTo(hotSwapState.eventCounter);
255 restart: restartProgram,
257 kontinue: continueProgram,
258 getMaxSteps: getMaxSteps,
260 getPaused: debugModule.getPaused,
261 getHotSwapState: getHotSwapState,
268function Point(x, y) {
272 this.translate = function(x, y) {
273 return new Point(this.x + x, this.y + y);
276 this.equals = function(p) {
277 return this.x == p.x && this.y == p.y;
281function tracePathInit(runtime) {
282 var List = Elm.List.make(runtime);
283 var tracePathCanvas = createCanvas();
284 var tracePositions = {};
285 var recordingTraces = true;
287 function findPositions(currentScene) {
289 function processElement(elem, offset) {
290 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
292 List.map(F2(processForm)(offset))(elem.element.model.forms);
294 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
296 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
300 function processForm(offset, form) {
301 if (form.form.ctor == "FElement")
303 processElement(form.form._0, offset.translate(form.x, -form.y));
307 processElement(currentScene, new Point(0, 0));
311 function appendPositions(positions) {
312 for (var id in positions) {
313 var pos = positions[id];
314 if (tracePositions.hasOwnProperty(id)) {
315 var points = tracePositions[id];
316 if (!pos.equals(points[points.length-1])) {
321 tracePositions[id] = [pos];
326 function graphicsUpdate(currentScene) {
327 if (!recordingTraces) {
331 var ctx = tracePathCanvas.getContext('2d');
332 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
335 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
336 appendPositions(findPositions(currentScene));
337 for (var id in tracePositions)
340 var points = tracePositions[id];
341 for (var i=0; i < points.length; i++)
345 ctx.moveTo(p.x, p.y);
348 ctx.lineTo(p.x, p.y);
352 ctx.strokeStyle = "#888a85";
355 for (var i=0; i < points.length; i++)
359 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
360 ctx.fillStyle = "#2e3436";
368 function clearTraces() {
372 function stopRecording() {
373 recordingTraces = false;
376 function startRecording() {
377 recordingTraces = true;
381 graphicsUpdate: graphicsUpdate,
382 canvas: tracePathCanvas,
383 clearTraces: clearTraces,
384 stopRecording: stopRecording,
385 startRecording: startRecording
390function executeCallbacks(callbacks, reexecute) {
391 callbacks.forEach(function(timer) {
392 if (reexecute || !timer.executed) {
393 var func = timer.func;
399function createCanvas() {
400 var c = document.createElement('canvas');
401 c.width = window.innerWidth;
402 c.height = window.innerHeight;
403 c.style.position = "absolute";
409function assert(bool, msg) {
411 throw "Assertion error: " + msg;
415function saveNodeValues(allNodes) {
418 allNodes.forEach(function(node) {
419 nodeValues.push({ value: node.value, id: node.id });
426function restoreNodeValues(allNodes, nodeStates) {
427 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
428 for (var i=0; i < allNodes.length; i++) {
429 var node = allNodes[i];
430 var state = nodeStates[i];
431 assert(node.id == state.id, "the nodes moved position");
433 node.value = state.value;
437function flattenNodes(nodes) {
440 function addAllToDict(node) {
441 nodesById[node.id] = node;
442 node.kids.forEach(addAllToDict);
444 nodes.forEach(addAllToDict);
446 var allNodes = Object.keys(nodesById).sort().map(function(key) {
447 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