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 = [];
31 var asyncCallbacks = [];
33 function wrapNotify(id, v) {
34 var timestep = Date.now();
37 // ignore notify because we are stepping
40 runtime.notify(id, v, timestep);
41 recordEvent(id, v, timestep);
45 function wrapRunDelayed(func, delayMs) {
46 return safeSetTimeout(func, delayMs);
49 function safeSetTimeout(func, delayMs) {
50 var timerId = setTimeout(func, delayMs);
51 asyncCallbacks.push({ func:func, delayMs:delayMs, timerId:timerId });
55 function recordEvent(id, v, timestep) {
56 recordedEvents.push({ id:id, value:v, timestep:timestep });
59 function clearAsyncCallbacks() {
60 asyncCallbacks.forEach(function(timer) {
61 clearTimeout(timer.timerId);
65 function clearRecordedEvents() {
69 function getRecordedEvents() {
70 return recordedEvents.slice();
73 function setPaused(v) {
77 function getPaused() {
81 var wrappedRuntime = {
83 runDelayed: wrapRunDelayed,
85 display: runtime.display,
87 addListener: runtime.addListener,
88 inputs: runtime.inputs
91 var moduleInstance = module.make(wrappedRuntime);
92 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
95 moduleInstance: moduleInstance,
96 moduleNodes: moduleNodes,
97 initialNodeValues: saveNodeValues(moduleNodes),
98 initialAsyncCallbacks: asyncCallbacks.slice(),
100 clearAsyncCallbacks: clearAsyncCallbacks,
101 clearRecordedEvents: clearRecordedEvents,
102 getRecordedEvents: getRecordedEvents,
103 getPaused: getPaused,
108function debuggerInit(debugModule, runtime) {
109 var Signal = Elm.Signal.make(runtime);
111 var tracePath = tracePathInit(runtime);
112 var tracePathNode = A2(Signal.lift, tracePath.graphicsUpdate, debugModule.moduleInstance.main);
113 runtime.node.parentNode.appendChild(tracePath.canvas);
115 function resetProgram() {
116 debugModule.clearAsyncCallbacks();
117 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
121 function restartProgram() {
123 debugModule.clearRecordedEvents();
124 debugModule.initialAsyncCallbacks.forEach(function(timer) {
125 var func = timer.func;
130 function stepTo(index) {
131 if (!debugModule.getPaused()) {
132 debugModule.setPaused(true);
136 if (index < 0 || index >= recordedEvents.length) {
137 throw "Index out of bounds: " + index;
140 if (index < eventCounter) {
145 assert(index >= eventCounter, "index must be bad");
146 while (eventCounter < index) {
147 var nextEvent = recordedEvents[eventCounter];
148 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
152 assert(eventCounter == index, "while loop didnt' work");
155 function getMaxSteps() {
156 return recordedEvents.length - 1;
159 function doPlayback(eventList) {
160 var x = eventList.shift();
162 runtime.notify(x[0], x[1], x[2]);
164 if (eventList.length > 0) {
165 var delta = eventList[0][2] - time;
166 setTimeout(function() { doPlayback(eventList); }, delta);
170 function redrawGraphics() {
171 var graphicsNode = debugModule.moduleInstance.main.kids[0];
172 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
177 restart: restartProgram,
178 getMaxSteps: getMaxSteps,
185function Point(x, y) {
189 this.translate = function(x, y) {
190 return new Point(this.x + x, this.y + y);
193 this.equals = function(p) {
194 return this.x == p.x && this.y == p.y;
198function tracePathInit(runtime) {
199 var List = Elm.List.make(runtime);
200 var tracePathCanvas = createCanvas();
201 var tracePositions = {};
203 function findPositions(currentScene) {
205 function processElement(elem, offset) {
206 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
208 List.map(F2(processForm)(offset))(elem.element.model.forms);
210 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
212 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
216 function processForm(offset, form) {
217 if (form.form.ctor == "FElement")
219 processElement(form.form._0, offset.translate(form.x, -form.y));
223 processElement(currentScene, new Point(0, 0));
227 function appendPositions(positions) {
228 for (var id in positions) {
229 var pos = positions[id];
230 if (tracePositions.hasOwnProperty(id)) {
231 var points = tracePositions[id];
232 if (!pos.equals(points[points.length-1])) {
237 tracePositions[id] = [pos];
242 function graphicsUpdate(currentScene) {
243 var ctx = tracePathCanvas.getContext('2d');
244 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
247 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
248 appendPositions(findPositions(currentScene));
249 for (var id in tracePositions)
252 var points = tracePositions[id];
253 for (var i=0; i < points.length; i++)
257 ctx.moveTo(p.x, p.y);
260 ctx.lineTo(p.x, p.y);
265 ctx.strokeStyle = "black";
271 function clearTraces() {
276 graphicsUpdate: graphicsUpdate,
277 canvas: tracePathCanvas,
278 clearTraces: clearTraces
283function createCanvas() {
284 var c = document.createElement('canvas');
285 c.width = window.innerWidth;
286 c.height = window.innerHeight;
287 c.style.position = "absolute";
293function assert(bool, msg) {
295 throw "Assertion error: " + msg;
299function saveNodeValues(allNodes) {
302 allNodes.forEach(function(node) {
303 nodeValues.push({ value: node.value, id: node.id });
310function restoreNodeValues(allNodes, nodeStates) {
311 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
312 for (var i=0; i < allNodes.length; i++) {
313 var node = allNodes[i];
314 var state = nodeStates[i];
315 assert(node.id == state.id, "the nodes moved position");
317 node.value = state.value;
321function flattenNodes(nodes) {
322 var treeReduce = function(prev, cur, i, arr) {
323 var flatKids = cur.kids.reduce(treeReduce, []);
325 return prev.concat(flatKids);
328 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