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);
45 function wrapRunDelayed(func, delayMs) {
46 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
47 var timerId = setTimeout(function() {
48 cbObj.executed = true;
51 cbObj.timerId = timerId;
52 asyncCallbacks.push(cbObj);
56 function recordEvent(id, v, timestep) {
57 recordedEvents.push({ id:id, value:v, timestep:timestep });
60 function clearAsyncCallbacks() {
61 asyncCallbacks.forEach(function(timer) {
62 if (!timer.executed) {
63 clearTimeout(timer.timerId);
68 function clearRecordedEvents() {
72 function getRecordedEventsLength() {
73 return recordedEvents.length;
76 function getRecordedEventAt(i) {
77 return recordedEvents[i];
80 function setPaused(v) {
83 clearAsyncCallbacks();
86 executeCallbacks(asyncCallbacks);
90 function getPaused() {
94 var wrappedRuntime = {
96 runDelayed: wrapRunDelayed,
98 display: runtime.display,
100 addListener: runtime.addListener,
101 inputs: runtime.inputs
104 var moduleInstance = module.make(wrappedRuntime);
105 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
108 moduleInstance: moduleInstance,
109 moduleNodes: moduleNodes,
110 initialNodeValues: saveNodeValues(moduleNodes),
111 initialAsyncCallbacks: asyncCallbacks.slice(),
113 clearAsyncCallbacks: clearAsyncCallbacks,
114 clearRecordedEvents: clearRecordedEvents,
115 getRecordedEventsLength: getRecordedEventsLength,
116 getRecordedEventAt: getRecordedEventAt,
117 getPaused: getPaused,
118 setPaused: setPaused,
119 tracePath: tracePathInit(runtime)
123function debuggerInit(debugModule, runtime) {
124 var Signal = Elm.Signal.make(runtime);
126 var tracePathNode = A2(Signal.lift, debugModule.tracePath.graphicsUpdate, debugModule.moduleInstance.main);
127 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
129 var eventCounter = 0;
131 function resetProgram() {
132 debugModule.clearAsyncCallbacks();
133 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
137 function restartProgram() {
139 debugModule.clearRecordedEvents();
140 executeCallbacks(debugModule.initialAsyncCallbacks);
143 function pauseProgram() {
144 debugModule.setPaused(true);
147 function continueProgram() {
148 if (debugModule.getPaused())
150 debugModule.setPaused(false);
154 function stepTo(index) {
155 if (!debugModule.getPaused()) {
156 debugModule.setPaused(true);
160 if (index < 0 || index > getMaxSteps()) {
161 throw "Index out of bounds: " + index;
164 if (index < eventCounter) {
169 assert(index >= eventCounter, "index must be bad");
170 while (eventCounter < index) {
171 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
172 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
176 assert(eventCounter == index, "while loop didn't work");
179 function getMaxSteps() {
180 return debugModule.getRecordedEventsLength() - 1;
183 function doPlayback(eventList) {
184 var x = eventList.shift();
186 runtime.notify(x[0], x[1], x[2]);
188 if (eventList.length > 0) {
189 var delta = eventList[0][2] - time;
190 setTimeout(function() { doPlayback(eventList); }, delta);
194 function redrawGraphics() {
195 var graphicsNode = debugModule.moduleInstance.main.kids[0];
196 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
200 restart: restartProgram,
202 kontinue: continueProgram,
203 getMaxSteps: getMaxSteps,
210function Point(x, y) {
214 this.translate = function(x, y) {
215 return new Point(this.x + x, this.y + y);
218 this.equals = function(p) {
219 return this.x == p.x && this.y == p.y;
223function tracePathInit(runtime) {
224 var List = Elm.List.make(runtime);
225 var tracePathCanvas = createCanvas();
226 var tracePositions = {};
228 function findPositions(currentScene) {
230 function processElement(elem, offset) {
231 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
233 List.map(F2(processForm)(offset))(elem.element.model.forms);
235 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
237 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
241 function processForm(offset, form) {
242 if (form.form.ctor == "FElement")
244 processElement(form.form._0, offset.translate(form.x, -form.y));
248 processElement(currentScene, new Point(0, 0));
252 function appendPositions(positions) {
253 for (var id in positions) {
254 var pos = positions[id];
255 if (tracePositions.hasOwnProperty(id)) {
256 var points = tracePositions[id];
257 if (!pos.equals(points[points.length-1])) {
262 tracePositions[id] = [pos];
267 function graphicsUpdate(currentScene) {
268 var ctx = tracePathCanvas.getContext('2d');
269 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
272 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
273 appendPositions(findPositions(currentScene));
274 for (var id in tracePositions)
277 var points = tracePositions[id];
278 for (var i=0; i < points.length; i++)
282 ctx.moveTo(p.x, p.y);
285 ctx.lineTo(p.x, p.y);
290 ctx.strokeStyle = "black";
296 function clearTraces() {
301 graphicsUpdate: graphicsUpdate,
302 canvas: tracePathCanvas,
303 clearTraces: clearTraces
308function executeCallbacks(callbacks) {
309 callbacks.forEach(function(timer) {
310 if (!timer.executed) {
311 var func = timer.func;
317function createCanvas() {
318 var c = document.createElement('canvas');
319 c.width = window.innerWidth;
320 c.height = window.innerHeight;
321 c.style.position = "absolute";
327function assert(bool, msg) {
329 throw "Assertion error: " + msg;
333function saveNodeValues(allNodes) {
336 allNodes.forEach(function(node) {
337 nodeValues.push({ value: node.value, id: node.id });
344function restoreNodeValues(allNodes, nodeStates) {
345 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
346 for (var i=0; i < allNodes.length; i++) {
347 var node = allNodes[i];
348 var state = nodeStates[i];
349 assert(node.id == state.id, "the nodes moved position");
351 node.value = state.value;
355function flattenNodes(nodes) {
356 var treeReduce = function(prev, cur, i, arr) {
357 var flatKids = cur.kids.reduce(treeReduce, []);
359 return prev.concat(flatKids);
362 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