6Elm.debuggerAttach = function(module) {
8 make: function(runtime) {
9 var wrappedModule = debugModule(module, runtime);
10 Elm.Debugger = debuggerInit(wrappedModule, runtime);
12 var dispatch = function(_) {
13 parent.window.postMessage("elmDebuggerInit", window.location.origin);
15 if (parent.window.document.readyState === "complete") {
19 parent.window.addEventListener("load", dispatch);
22 return wrappedModule.moduleInstance;
27function debugModule(module, runtime) {
28 var programPaused = false;
29 var recordedEvents = [];
30 var asyncCallbacks = [];
32 function wrapNotify(id, v) {
33 var timestep = Date.now();
36 // ignore async events generated while playing back
37 // or user events while program is paused
40 runtime.notify(id, v, timestep);
41 recordEvent(id, v, timestep);
43 parent.window.postMessage("elmNotify", window.location.origin);
48 function wrapRunDelayed(func, delayMs) {
49 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
50 var timerId = setTimeout(function() {
51 cbObj.executed = true;
54 cbObj.timerId = timerId;
55 asyncCallbacks.push(cbObj);
59 function recordEvent(id, v, timestep) {
60 recordedEvents.push({ id:id, value:v, timestep:timestep });
63 function clearAsyncCallbacks() {
64 asyncCallbacks.forEach(function(timer) {
65 if (!timer.executed) {
66 clearTimeout(timer.timerId);
71 function clearRecordedEvents() {
75 function getRecordedEventsLength() {
76 return recordedEvents.length;
79 function getRecordedEventAt(i) {
80 return recordedEvents[i];
83 function setPaused(v) {
86 clearAsyncCallbacks();
87 tracePath.stopRecording();
90 executeCallbacks(asyncCallbacks);
91 tracePath.startRecording();
95 function getPaused() {
99 var wrappedRuntime = {
101 runDelayed: wrapRunDelayed,
103 display: runtime.display,
105 addListener: runtime.addListener,
106 inputs: runtime.inputs
109 var moduleInstance = module.make(wrappedRuntime);
110 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
111 var tracePath = tracePathInit(runtime);
114 moduleInstance: moduleInstance,
115 moduleNodes: moduleNodes,
116 initialNodeValues: saveNodeValues(moduleNodes),
117 initialAsyncCallbacks: asyncCallbacks.slice(),
119 clearAsyncCallbacks: clearAsyncCallbacks,
120 clearRecordedEvents: clearRecordedEvents,
121 getRecordedEventsLength: getRecordedEventsLength,
122 getRecordedEventAt: getRecordedEventAt,
123 getPaused: getPaused,
124 setPaused: setPaused,
129function debuggerInit(debugModule, runtime) {
130 var Signal = Elm.Signal.make(runtime);
132 var tracePathNode = A2(Signal.lift, debugModule.tracePath.graphicsUpdate, debugModule.moduleInstance.main);
133 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
135 var eventCounter = 0;
137 function resetProgram() {
138 debugModule.clearAsyncCallbacks();
139 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
143 function restartProgram() {
145 debugModule.clearRecordedEvents();
146 executeCallbacks(debugModule.initialAsyncCallbacks);
149 function pauseProgram() {
150 debugModule.setPaused(true);
151 eventCounter = debugModule.getRecordedEventsLength();
154 function continueProgram() {
155 if (debugModule.getPaused())
157 debugModule.setPaused(false);
161 function stepTo(index) {
162 if (!debugModule.getPaused()) {
163 debugModule.setPaused(true);
167 if (index < 0 || index > getMaxSteps()) {
168 throw "Index out of bounds: " + index;
171 if (index < eventCounter) {
176 assert(index >= eventCounter, "index must be bad");
177 while (eventCounter < index) {
178 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
179 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
183 assert(eventCounter == index, "while loop didn't work");
186 function getMaxSteps() {
187 return debugModule.getRecordedEventsLength();
190 function doPlayback(eventList) {
191 var x = eventList.shift();
193 runtime.notify(x[0], x[1], x[2]);
195 if (eventList.length > 0) {
196 var delta = eventList[0][2] - time;
197 setTimeout(function() { doPlayback(eventList); }, delta);
201 function redrawGraphics() {
202 var graphicsNode = debugModule.moduleInstance.main.kids[0];
203 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
207 restart: restartProgram,
209 kontinue: continueProgram,
210 getMaxSteps: getMaxSteps,
212 getPaused: debugModule.getPaused
218function Point(x, y) {
222 this.translate = function(x, y) {
223 return new Point(this.x + x, this.y + y);
226 this.equals = function(p) {
227 return this.x == p.x && this.y == p.y;
231function tracePathInit(runtime) {
232 var List = Elm.List.make(runtime);
233 var tracePathCanvas = createCanvas();
234 var tracePositions = {};
235 var recordingTraces = true;
237 function findPositions(currentScene) {
239 function processElement(elem, offset) {
240 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
242 List.map(F2(processForm)(offset))(elem.element.model.forms);
244 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
246 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
250 function processForm(offset, form) {
251 if (form.form.ctor == "FElement")
253 processElement(form.form._0, offset.translate(form.x, -form.y));
257 processElement(currentScene, new Point(0, 0));
261 function appendPositions(positions) {
262 for (var id in positions) {
263 var pos = positions[id];
264 if (tracePositions.hasOwnProperty(id)) {
265 var points = tracePositions[id];
266 if (!pos.equals(points[points.length-1])) {
271 tracePositions[id] = [pos];
276 function graphicsUpdate(currentScene) {
277 if (!recordingTraces) {
281 var ctx = tracePathCanvas.getContext('2d');
282 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
285 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
286 appendPositions(findPositions(currentScene));
287 for (var id in tracePositions)
290 var points = tracePositions[id];
291 for (var i=0; i < points.length; i++)
295 ctx.moveTo(p.x, p.y);
298 ctx.lineTo(p.x, p.y);
303 ctx.strokeStyle = "black";
309 function clearTraces() {
313 function stopRecording() {
314 recordingTraces = false;
317 function startRecording() {
318 recordingTraces = true;
322 graphicsUpdate: graphicsUpdate,
323 canvas: tracePathCanvas,
324 clearTraces: clearTraces,
325 stopRecording: stopRecording,
326 startRecording: startRecording
331function executeCallbacks(callbacks) {
332 callbacks.forEach(function(timer) {
333 if (!timer.executed) {
334 var func = timer.func;
340function createCanvas() {
341 var c = document.createElement('canvas');
342 c.width = window.innerWidth;
343 c.height = window.innerHeight;
344 c.style.position = "absolute";
350function assert(bool, msg) {
352 throw "Assertion error: " + msg;
356function saveNodeValues(allNodes) {
359 allNodes.forEach(function(node) {
360 nodeValues.push({ value: node.value, id: node.id });
367function restoreNodeValues(allNodes, nodeStates) {
368 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
369 for (var i=0; i < allNodes.length; i++) {
370 var node = allNodes[i];
371 var state = nodeStates[i];
372 assert(node.id == state.id, "the nodes moved position");
374 node.value = state.value;
378function flattenNodes(nodes) {
379 var treeReduce = function(prev, cur, i, arr) {
380 var flatKids = cur.kids.reduce(treeReduce, []);
382 return prev.concat(flatKids);
385 return nodes.reduce(treeReduce, []);
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