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 should only be called when continuing, not when restarting.
91 //executeCallbacks(asyncCallbacks, false);
92 tracePath.startRecording();
96 function getPaused() {
100 var wrappedRuntime = {
102 runDelayed: wrapRunDelayed,
104 display: runtime.display,
106 addListener: runtime.addListener,
107 inputs: runtime.inputs
110 var moduleInstance = module.make(wrappedRuntime);
111 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
112 var tracePath = tracePathInit(runtime);
115 moduleInstance: moduleInstance,
116 moduleNodes: moduleNodes,
117 initialNodeValues: saveNodeValues(moduleNodes),
118 initialAsyncCallbacks: asyncCallbacks.slice(),
120 clearAsyncCallbacks: clearAsyncCallbacks,
121 clearRecordedEvents: clearRecordedEvents,
122 getRecordedEventsLength: getRecordedEventsLength,
123 getRecordedEventAt: getRecordedEventAt,
124 getPaused: getPaused,
125 setPaused: setPaused,
130function debuggerInit(debugModule, runtime) {
131 var Signal = Elm.Signal.make(runtime);
133 var tracePathNode = A2(Signal.lift, debugModule.tracePath.graphicsUpdate, debugModule.moduleInstance.main);
134 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
136 var eventCounter = 0;
138 function resetProgram() {
139 debugModule.clearAsyncCallbacks();
140 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
144 function restartProgram() {
146 debugModule.tracePath.clearTraces();
147 debugModule.clearRecordedEvents();
148 debugModule.setPaused(false);
149 executeCallbacks(debugModule.initialAsyncCallbacks, true);
152 function pauseProgram() {
153 debugModule.setPaused(true);
154 eventCounter = debugModule.getRecordedEventsLength();
157 function continueProgram() {
158 if (debugModule.getPaused())
160 debugModule.setPaused(false);
164 function stepTo(index) {
165 if (!debugModule.getPaused()) {
166 debugModule.setPaused(true);
170 if (index < 0 || index > getMaxSteps()) {
171 throw "Index out of bounds: " + index;
174 if (index < eventCounter) {
179 assert(index >= eventCounter, "index must be bad");
180 while (eventCounter < index) {
181 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
182 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
186 assert(eventCounter == index, "while loop didn't work");
189 function getMaxSteps() {
190 return debugModule.getRecordedEventsLength();
193 function doPlayback(eventList) {
194 var x = eventList.shift();
196 runtime.notify(x[0], x[1], x[2]);
198 if (eventList.length > 0) {
199 var delta = eventList[0][2] - time;
200 setTimeout(function() { doPlayback(eventList); }, delta);
204 function redrawGraphics() {
205 var graphicsNode = debugModule.moduleInstance.main.kids[0];
206 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
210 restart: restartProgram,
212 kontinue: continueProgram,
213 getMaxSteps: getMaxSteps,
215 getPaused: debugModule.getPaused
221function Point(x, y) {
225 this.translate = function(x, y) {
226 return new Point(this.x + x, this.y + y);
229 this.equals = function(p) {
230 return this.x == p.x && this.y == p.y;
234function tracePathInit(runtime) {
235 var List = Elm.List.make(runtime);
236 var tracePathCanvas = createCanvas();
237 var tracePositions = {};
238 var recordingTraces = true;
240 function findPositions(currentScene) {
242 function processElement(elem, offset) {
243 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
245 List.map(F2(processForm)(offset))(elem.element.model.forms);
247 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
249 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
253 function processForm(offset, form) {
254 if (form.form.ctor == "FElement")
256 processElement(form.form._0, offset.translate(form.x, -form.y));
260 processElement(currentScene, new Point(0, 0));
264 function appendPositions(positions) {
265 for (var id in positions) {
266 var pos = positions[id];
267 if (tracePositions.hasOwnProperty(id)) {
268 var points = tracePositions[id];
269 if (!pos.equals(points[points.length-1])) {
274 tracePositions[id] = [pos];
279 function graphicsUpdate(currentScene) {
280 if (!recordingTraces) {
284 var ctx = tracePathCanvas.getContext('2d');
285 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
288 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
289 appendPositions(findPositions(currentScene));
290 for (var id in tracePositions)
293 var points = tracePositions[id];
294 for (var i=0; i < points.length; i++)
298 ctx.moveTo(p.x, p.y);
301 ctx.lineTo(p.x, p.y);
306 ctx.strokeStyle = "black";
312 function clearTraces() {
316 function stopRecording() {
317 recordingTraces = false;
320 function startRecording() {
321 recordingTraces = true;
325 graphicsUpdate: graphicsUpdate,
326 canvas: tracePathCanvas,
327 clearTraces: clearTraces,
328 stopRecording: stopRecording,
329 startRecording: startRecording
334function executeCallbacks(callbacks, reexecute) {
335 callbacks.forEach(function(timer) {
336 if (reexecute || !timer.executed) {
337 var func = timer.func;
343function createCanvas() {
344 var c = document.createElement('canvas');
345 c.width = window.innerWidth;
346 c.height = window.innerHeight;
347 c.style.position = "absolute";
353function assert(bool, msg) {
355 throw "Assertion error: " + msg;
359function saveNodeValues(allNodes) {
362 allNodes.forEach(function(node) {
363 nodeValues.push({ value: node.value, id: node.id });
370function restoreNodeValues(allNodes, nodeStates) {
371 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
372 for (var i=0; i < allNodes.length; i++) {
373 var node = allNodes[i];
374 var state = nodeStates[i];
375 assert(node.id == state.id, "the nodes moved position");
377 node.value = state.value;
381function flattenNodes(nodes) {
382 var treeReduce = function(prev, cur, i, arr) {
383 var flatKids = cur.kids.reduce(treeReduce, []);
385 return prev.concat(flatKids);
388 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