6Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
8 make: function(runtime) {
9 var wrappedModule = debugModule(module, runtime);
10 Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState);
11 dispatchElmDebuggerInit();
12 return wrappedModule.moduleInstance;
17function dispatchElmDebuggerInit() {
19 var dispatch = function(_) {
20 parent.window.postMessage("elmDebuggerInit", window.location.origin);
22 if (parent.window.document.readyState === "complete") {
26 parent.window.addEventListener("load", dispatch);
31function debugModule(module, runtime) {
32 var programPaused = false;
33 var recordedEvents = [];
34 var asyncCallbacks = [];
35 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
37 function wrapNotify(id, v) {
38 var timestep = Date.now();
41 // ignore async events generated while playing back
42 // or user events while program is paused
46 var changed = runtime.notify(id, v, timestep);
47 recordEvent(id, v, timestep);
49 parent.window.postMessage("elmNotify", window.location.origin);
55 function wrapRunDelayed(func, delayMs) {
56 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
57 var timerId = setTimeout(function() {
58 cbObj.executed = true;
61 cbObj.timerId = timerId;
62 asyncCallbacks.push(cbObj);
66 function recordEvent(id, v, timestep) {
67 watchTracker.pushFrame();
68 recordedEvents.push({ id:id, value:v, timestep:timestep });
71 function clearAsyncCallbacks() {
72 asyncCallbacks.forEach(function(timer) {
73 if (!timer.executed) {
74 clearTimeout(timer.timerId);
79 function clearRecordedEvents() {
83 function getRecordedEventsLength() {
84 return recordedEvents.length;
87 function getRecordedEventAt(i) {
88 return recordedEvents[i];
91 function copyRecordedEvents() {
92 return recordedEvents.slice();
95 function loadRecordedEvents(events) {
96 recordedEvents = events.slice();
99 function setPaused(v) {
102 clearAsyncCallbacks();
103 tracePath.stopRecording();
106 // executeCallbacks should only be called when continuing, not when restarting.
107 //executeCallbacks(asyncCallbacks, false);
108 tracePath.startRecording();
112 function getPaused() {
113 return programPaused;
116 // runtime is the prototype of wrappedRuntime
117 // so we can access all runtime properties too
118 var wrappedRuntime = Object.create(runtime);
119 wrappedRuntime.notify = wrapNotify;
120 wrappedRuntime.runDelayed = wrapRunDelayed;
122 var assignedPropTracker = Object.create(wrappedRuntime);
123 var moduleInstance = module.make(assignedPropTracker);
125 // make sure the signal graph is actually a signal & extract the visual model
126 if ( !('recv' in moduleInstance.main) ) {
127 moduleInstance.main = Elm.Signal.make(runtime).constant(moduleInstance.main);
130 // The main module stores imported modules onto the runtime.
131 // To ensure only one instance of each module is created,
132 // we assign them back on the original runtime object.
133 Object.keys(assignedPropTracker).forEach(function(key) {
134 runtime[key] = assignedPropTracker[key];
137 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
138 var tracePath = tracePathInit(runtime, moduleInstance.main);
141 moduleInstance: moduleInstance,
142 moduleNodes: moduleNodes,
143 initialNodeValues: saveNodeValues(moduleNodes),
144 initialAsyncCallbacks: asyncCallbacks.slice(),
146 clearAsyncCallbacks: clearAsyncCallbacks,
147 clearRecordedEvents: clearRecordedEvents,
148 getRecordedEventsLength: getRecordedEventsLength,
149 getRecordedEventAt: getRecordedEventAt,
150 copyRecordedEvents: copyRecordedEvents,
151 loadRecordedEvents: loadRecordedEvents,
152 getPaused: getPaused,
153 setPaused: setPaused,
154 tracePath: tracePath,
155 watchTracker: watchTracker,
159function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
160 var eventCounter = 0;
162 function resetProgram() {
163 debugModule.clearAsyncCallbacks();
164 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
168 function restartProgram() {
170 debugModule.watchTracker.clear();
171 debugModule.tracePath.clearTraces();
172 debugModule.clearRecordedEvents();
173 debugModule.setPaused(false);
174 executeCallbacks(debugModule.initialAsyncCallbacks, true);
177 function pauseProgram() {
178 debugModule.setPaused(true);
179 eventCounter = debugModule.getRecordedEventsLength();
182 function continueProgram() {
183 if (debugModule.getPaused())
185 debugModule.setPaused(false);
189 function stepTo(index) {
190 if (!debugModule.getPaused()) {
191 debugModule.setPaused(true);
195 if (index < 0 || index > getMaxSteps()) {
196 throw "Index out of bounds: " + index;
199 if (index < eventCounter) {
204 assert(index >= eventCounter, "index must be bad");
205 while (eventCounter < index) {
206 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
207 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
211 assert(eventCounter == index, "while loop didn't work");
214 function getMaxSteps() {
215 return debugModule.getRecordedEventsLength();
218 function doPlayback(eventList) {
219 var x = eventList.shift();
221 runtime.notify(x[0], x[1], x[2]);
223 if (eventList.length > 0) {
224 var delta = eventList[0][2] - time;
225 setTimeout(function() { doPlayback(eventList); }, delta);
229 function redrawGraphics() {
230 var main = debugModule.moduleInstance.main
231 for (var i = main.kids.length ; i-- ; ) {
232 main.kids[i].recv(Date.now(), true, main.id);
236 function getHotSwapState() {
237 var counter = eventCounter;
238 if (!debugModule.getPaused()) {
239 counter = getMaxSteps();
242 recordedEvents: debugModule.copyRecordedEvents(),
243 eventCounter: counter
248 var parentNode = runtime.node.parentNode;
249 parentNode.removeChild(debugModule.tracePath.canvas);
250 parentNode.removeChild(runtime.node);
254 debugModule.setPaused(true);
255 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
257 // draw new trace path
258 debugModule.tracePath.startRecording();
259 stepTo(getMaxSteps());
260 debugModule.tracePath.stopRecording();
262 stepTo(hotSwapState.eventCounter);
265 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
268 restart: restartProgram,
270 kontinue: continueProgram,
271 getMaxSteps: getMaxSteps,
273 getPaused: debugModule.getPaused,
274 getHotSwapState: getHotSwapState,
276 allNodes: debugModule.moduleNodes,
277 watchTracker: debugModule.watchTracker,
278 mainNode: debugModule.moduleInstance.main
284function Point(x, y) {
288 this.translate = function(x, y) {
289 return new Point(this.x + x, this.y + y);
292 this.equals = function(p) {
293 return this.x == p.x && this.y == p.y;
297function tracePathInit(runtime, mainNode) {
298 var List = Elm.List.make(runtime);
299 var Signal = Elm.Signal.make(runtime);
300 var tracePathNode = A2(Signal.lift, graphicsUpdate, mainNode);
301 var tracePathCanvas = createCanvas();
302 var tracePositions = {};
303 var recordingTraces = true;
305 function findPositions(currentScene) {
307 function processElement(elem, offset) {
308 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
310 List.map(F2(processForm)(offset))(elem.element.model.forms);
312 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
314 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
318 function processForm(offset, form) {
319 if (form.form.ctor == "FElement")
321 processElement(form.form._0, offset.translate(form.x, -form.y));
325 processElement(currentScene, new Point(0, 0));
329 function appendPositions(positions) {
330 for (var id in positions) {
331 var pos = positions[id];
332 if (tracePositions.hasOwnProperty(id)) {
333 var points = tracePositions[id];
334 if (!pos.equals(points[points.length-1])) {
339 tracePositions[id] = [pos];
344 function graphicsUpdate(currentScene) {
345 if (!recordingTraces) {
349 var ctx = tracePathCanvas.getContext('2d');
350 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
353 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
354 appendPositions(findPositions(currentScene));
355 for (var id in tracePositions)
358 var points = tracePositions[id];
359 for (var i=0; i < points.length; i++)
363 ctx.moveTo(p.x, p.y);
366 ctx.lineTo(p.x, p.y);
370 ctx.strokeStyle = "#888a85";
373 for (var i=0; i < points.length; i++)
377 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
378 ctx.fillStyle = "#2e3436";
386 function clearTraces() {
390 function stopRecording() {
391 recordingTraces = false;
394 function startRecording() {
395 recordingTraces = true;
399 graphicsUpdate: graphicsUpdate,
400 canvas: tracePathCanvas,
401 clearTraces: clearTraces,
402 stopRecording: stopRecording,
403 startRecording: startRecording
408function executeCallbacks(callbacks, reexecute) {
409 callbacks.forEach(function(timer) {
410 if (reexecute || !timer.executed) {
411 var func = timer.func;
417function createCanvas() {
418 var c = document.createElement('canvas');
419 c.width = window.innerWidth;
420 c.height = window.innerHeight;
421 c.style.position = "absolute";
424 c.style.pointerEvents = "none";
428function assert(bool, msg) {
430 throw "Assertion error: " + msg;
434function saveNodeValues(allNodes) {
437 allNodes.forEach(function(node) {
438 nodeValues.push({ value: node.value, id: node.id });
445function restoreNodeValues(allNodes, nodeStates) {
446 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
447 for (var i=0; i < allNodes.length; i++) {
448 var node = allNodes[i];
449 var state = nodeStates[i];
450 assert(node.id == state.id, "the nodes moved position");
452 node.value = state.value;
456function flattenNodes(nodes) {
459 function addAllToDict(node) {
460 nodesById[node.id] = node;
461 node.kids.forEach(addAllToDict);
463 nodes.forEach(addAllToDict);
465 var allNodes = Object.keys(nodesById).sort().map(function(key) {
466 return nodesById[key];
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