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 notify because we are stepping
39 runtime.notify(id, v, timestep);
40 recordEvent(id, v, timestep);
44 function wrapRunDelayed(func, delayMs) {
45 return safeSetTimeout(func, delayMs);
48 function safeSetTimeout(func, delayMs) {
49 var timerId = setTimeout(func, delayMs);
50 asyncCallbacks.push({ func:func, delayMs:delayMs, timerId:timerId });
54 function recordEvent(id, v, timestep) {
55 recordedEvents.push({ id:id, value:v, timestep:timestep });
58 function clearAsyncCallbacks() {
59 asyncCallbacks.forEach(function(timer) {
60 clearTimeout(timer.timerId);
64 function clearRecordedEvents() {
68 function getRecordedEventsLength() {
69 return recordedEvents.length;
72 function getRecordedEventAt(i) {
73 return recordedEvents[i];
76 function setPaused(v) {
80 function getPaused() {
84 var wrappedRuntime = {
86 runDelayed: wrapRunDelayed,
88 display: runtime.display,
90 addListener: runtime.addListener,
91 inputs: runtime.inputs
94 var moduleInstance = module.make(wrappedRuntime);
95 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
98 moduleInstance: moduleInstance,
99 moduleNodes: moduleNodes,
100 initialNodeValues: saveNodeValues(moduleNodes),
101 initialAsyncCallbacks: asyncCallbacks.slice(),
103 clearAsyncCallbacks: clearAsyncCallbacks,
104 clearRecordedEvents: clearRecordedEvents,
105 getRecordedEventsLength: getRecordedEventsLength,
106 getRecordedEventAt: getRecordedEventAt,
107 getPaused: getPaused,
108 setPaused: setPaused,
109 tracePath: tracePathInit(runtime)
113function debuggerInit(debugModule, runtime) {
114 var Signal = Elm.Signal.make(runtime);
116 var tracePathNode = A2(Signal.lift, debugModule.tracePath.graphicsUpdate, debugModule.moduleInstance.main);
117 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
119 var eventCounter = 0;
121 function resetProgram() {
122 debugModule.clearAsyncCallbacks();
123 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
127 function restartProgram() {
129 debugModule.clearRecordedEvents();
130 debugModule.initialAsyncCallbacks.forEach(function(timer) {
131 var func = timer.func;
136 function stepTo(index) {
137 if (!debugModule.getPaused()) {
138 debugModule.setPaused(true);
142 if (index < 0 || index > getMaxSteps()) {
143 throw "Index out of bounds: " + index;
146 if (index < eventCounter) {
151 assert(index >= eventCounter, "index must be bad");
152 while (eventCounter < index) {
153 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
154 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
158 assert(eventCounter == index, "while loop didn't work");
161 function getMaxSteps() {
162 return debugModule.getRecordedEventsLength() - 1;
165 function doPlayback(eventList) {
166 var x = eventList.shift();
168 runtime.notify(x[0], x[1], x[2]);
170 if (eventList.length > 0) {
171 var delta = eventList[0][2] - time;
172 setTimeout(function() { doPlayback(eventList); }, delta);
176 function redrawGraphics() {
177 var graphicsNode = debugModule.moduleInstance.main.kids[0];
178 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
183 restart: restartProgram,
184 getMaxSteps: getMaxSteps,
191function Point(x, y) {
195 this.translate = function(x, y) {
196 return new Point(this.x + x, this.y + y);
199 this.equals = function(p) {
200 return this.x == p.x && this.y == p.y;
204function tracePathInit(runtime) {
205 var List = Elm.List.make(runtime);
206 var tracePathCanvas = createCanvas();
207 var tracePositions = {};
209 function findPositions(currentScene) {
211 function processElement(elem, offset) {
212 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
214 List.map(F2(processForm)(offset))(elem.element.model.forms);
216 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
218 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
222 function processForm(offset, form) {
223 if (form.form.ctor == "FElement")
225 processElement(form.form._0, offset.translate(form.x, -form.y));
229 processElement(currentScene, new Point(0, 0));
233 function appendPositions(positions) {
234 for (var id in positions) {
235 var pos = positions[id];
236 if (tracePositions.hasOwnProperty(id)) {
237 var points = tracePositions[id];
238 if (!pos.equals(points[points.length-1])) {
243 tracePositions[id] = [pos];
248 function graphicsUpdate(currentScene) {
249 var ctx = tracePathCanvas.getContext('2d');
250 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
253 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
254 appendPositions(findPositions(currentScene));
255 for (var id in tracePositions)
258 var points = tracePositions[id];
259 for (var i=0; i < points.length; i++)
263 ctx.moveTo(p.x, p.y);
266 ctx.lineTo(p.x, p.y);
271 ctx.strokeStyle = "black";
277 function clearTraces() {
282 graphicsUpdate: graphicsUpdate,
283 canvas: tracePathCanvas,
284 clearTraces: clearTraces
289function createCanvas() {
290 var c = document.createElement('canvas');
291 c.width = window.innerWidth;
292 c.height = window.innerHeight;
293 c.style.position = "absolute";
299function assert(bool, msg) {
301 throw "Assertion error: " + msg;
305function saveNodeValues(allNodes) {
308 allNodes.forEach(function(node) {
309 nodeValues.push({ value: node.value, id: node.id });
316function restoreNodeValues(allNodes, nodeStates) {
317 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
318 for (var i=0; i < allNodes.length; i++) {
319 var node = allNodes[i];
320 var state = nodeStates[i];
321 assert(node.id == state.id, "the nodes moved position");
323 node.value = state.value;
327function flattenNodes(nodes) {
328 var treeReduce = function(prev, cur, i, arr) {
329 var flatKids = cur.kids.reduce(treeReduce, []);
331 return prev.concat(flatKids);
334 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