6Elm.debuggerAttach = function(module) {
8 make: function(runtime) {
9 var wrappedModule = debugModule(module, runtime);
10 Elm.Debugger = debuggerInit(wrappedModule, runtime);
11 return wrappedModule.moduleInstance;
16function debugModule(module, runtime) {
17 var programPaused = false;
18 var recordedEvents = [];
22 function wrapNotify(id, v) {
23 var timestep = Date.now();
26 // ignore notify because we are stepping
29 runtime.notify(id, v, timestep);
30 recordEvent(id, v, timestep);
34 function wrapRunDelayed(func, delayMs) {
35 return safeSetTimeout(func, delayMs);
38 function safeSetTimeout(func, delayMs) {
39 var timerId = setTimeout(func, delayMs);
40 asyncTimers.push({ func:func, delayMs:delayMs, timerId:timerId });
44 function recordEvent(id, v, timestep) {
45 recordedEvents.push({ id:id, value:v, timestep:timestep });
48 function clearAsyncEvents() {
49 asyncTimers.forEach(function(timer) {
50 clearTimeout(timer.timerId);
54 function clearRecordedEvents() {
58 function getRecordedEvents() {
59 return recordedEvents.slice();
62 function setPaused(v) {
66 function getPaused() {
70 var wrappedRuntime = {
72 runDelayed: wrapRunDelayed,
74 display: runtime.display,
76 addListener: runtime.addListener,
77 inputs: runtime.inputs
80 var moduleInstance = module.make(wrappedRuntime);
81 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
84 moduleInstance: moduleInstance,
85 moduleNodes: moduleNodes,
86 initialNodeValues: saveNodeValues(moduleNodes),
87 initialAsyncTimers: asyncTimers.slice(),
89 clearAsyncEvents: clearAsyncEvents,
90 clearRecordedEvents: clearRecordedEvents,
91 getRecordedEvents: getRecordedEvents,
97function debuggerInit(debugModule, runtime) {
98 var Signal = Elm.Signal.make(runtime);
100 var tracePath = tracePathInit(runtime);
101 var tracePathNode = A2(Signal.lift, tracePath.graphicsUpdate, debugModule.moduleInstance.main);
102 runtime.node.parentNode.appendChild(tracePath.canvas);
104 function resetProgram() {
105 debugModule.clearAsyncEvents();
106 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
110 function restartProgram() {
112 debugModule.clearRecordedEvents();
113 debugModule.initialAsyncTimers.forEach(function(timer) {
114 var func = timer.func;
119 function stepTo(index) {
120 if (!debugModule.getPaused()) {
121 debugModule.setPaused(true);
125 if (index < 0 || index >= recordedEvents.length) {
126 throw "Index out of bounds: " + index;
129 if (index < eventCounter) {
134 assert(index >= eventCounter, "index must be bad");
135 while (eventCounter < index) {
136 var nextEvent = recordedEvents[eventCounter];
137 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
141 assert(eventCounter == index, "while loop didnt' work");
144 function getMaxSteps() {
145 return recordedEvents.length - 1;
148 function doPlayback(eventList) {
149 var x = eventList.shift();
151 runtime.notify(x[0], x[1], x[2]);
153 if (eventList.length > 0) {
154 var delta = eventList[0][2] - time;
155 setTimeout(function() { doPlayback(eventList); }, delta);
159 function redrawGraphics() {
160 var graphicsNode = debugModule.moduleInstance.main.kids[0];
161 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
166 restart: restartProgram,
167 getMaxSteps: getMaxSteps,
174function Point(x, y) {
178 this.translate = function(x, y) {
179 return new Point(this.x + x, this.y + y);
182 this.equals = function(p) {
183 return this.x == p.x && this.y == p.y;
187function tracePathInit(runtime) {
188 var List = Elm.List.make(runtime);
189 var tracePathCanvas = createCanvas();
190 var tracePositions = {};
192 function findPositions(currentScene) {
194 function processElement(elem, offset) {
195 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
197 List.map(F2(processForm)(offset))(elem.element.model.forms);
199 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
201 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
205 function processForm(offset, form) {
206 if (form.form.ctor == "FElement")
208 processElement(form.form._0, offset.translate(form.x, -form.y));
212 processElement(currentScene, new Point(0, 0));
216 function appendPositions(positions) {
217 for (var id in positions) {
218 var pos = positions[id];
219 if (tracePositions.hasOwnProperty(id)) {
220 var points = tracePositions[id];
221 if (!pos.equals(points[points.length-1])) {
226 tracePositions[id] = [pos];
231 function graphicsUpdate(currentScene) {
232 var ctx = tracePathCanvas.getContext('2d');
233 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
236 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
237 appendPositions(findPositions(currentScene));
238 for (var id in tracePositions)
241 var points = tracePositions[id];
242 for (var i=0; i < points.length; i++)
246 ctx.moveTo(p.x, p.y);
249 ctx.lineTo(p.x, p.y);
254 ctx.strokeStyle = "black";
260 function clearTraces() {
265 graphicsUpdate: graphicsUpdate,
266 canvas: tracePathCanvas,
267 clearTraces: clearTraces
272function createCanvas() {
273 var c = document.createElement('canvas');
274 c.width = window.innerWidth;
275 c.height = window.innerHeight;
276 c.style.position = "absolute";
282function assert(bool, msg) {
284 throw "Assertion error: " + msg;
288function saveNodeValues(allNodes) {
291 allNodes.forEach(function(node) {
292 nodeValues.push({ value: node.value, id: node.id });
299function restoreNodeValues(allNodes, nodeStates) {
300 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
301 for (var i=0; i < allNodes.length; i++) {
302 var node = allNodes[i];
303 var state = nodeStates[i];
304 assert(node.id == state.id, "the nodes moved position");
306 node.value = state.value;
310function flattenNodes(nodes) {
311 var treeReduce = function(prev, cur, i, arr) {
312 var flatKids = cur.kids.reduce(treeReduce, []);
314 return prev.concat(flatKids);
317 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