5if (!window.location.origin) {
6 window.location.origin =
7 window.location.protocol + "//" +
8 window.location.hostname +
9 (window.location.port ? (':' + window.location.port) : '');
13Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
15 make: function(runtime) {
16 var wrappedModule = debugModule(module, runtime);
17 Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState);
18 dispatchElmDebuggerInit();
19 return wrappedModule.moduleInstance;
24function dispatchElmDebuggerInit() {
26 var dispatch = function(_) {
27 parent.window.postMessage("elmDebuggerInit", window.location.origin);
29 if (parent.window.document.readyState === "complete") {
33 parent.window.addEventListener("load", dispatch);
38function debugModule(module, runtime) {
39 var programPaused = false;
40 var recordedEvents = [];
41 var asyncCallbacks = [];
42 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
44 function wrapNotify(id, v) {
45 var timestep = Date.now();
48 // ignore async events generated while playing back
49 // or user events while program is paused
53 var changed = runtime.notify(id, v, timestep);
54 recordEvent(id, v, timestep);
56 parent.window.postMessage("elmNotify", window.location.origin);
62 function wrapRunDelayed(func, delayMs) {
64 // Don't push timers and such to the callback stack while we're paused.
65 // It causes too many callbacks to be fired during unpausing.
68 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
69 var timerId = setTimeout(function() {
70 cbObj.executed = true;
73 cbObj.timerId = timerId;
74 asyncCallbacks.push(cbObj);
78 function recordEvent(id, v, timestep) {
79 watchTracker.pushFrame();
80 recordedEvents.push({ id:id, value:v, timestep:timestep });
83 function clearAsyncCallbacks() {
84 asyncCallbacks.forEach(function(timer) {
85 if (!timer.executed) {
86 clearTimeout(timer.timerId);
91 function clearRecordedEvents() {
95 function getRecordedEventsLength() {
96 return recordedEvents.length;
99 function getRecordedEventAt(i) {
100 return recordedEvents[i];
103 function copyRecordedEvents() {
104 return recordedEvents.slice();
107 function loadRecordedEvents(events) {
108 recordedEvents = events.slice();
111 function setPaused() {
112 programPaused = true;
113 clearAsyncCallbacks();
114 tracePath.stopRecording();
117 function setContinue(position) {
118 programPaused = false;
120 // If we're not unpausing at the head, then we need to dump the
121 // events that are ahead of where we're continuing.
122 recordedEvents = recordedEvents.slice(0,position);
123 executeCallbacks(asyncCallbacks, false);
125 tracePath.startRecording();
128 function getPaused() {
129 return programPaused;
132 // runtime is the prototype of wrappedRuntime
133 // so we can access all runtime properties too
134 var wrappedRuntime = Object.create(runtime);
135 wrappedRuntime.notify = wrapNotify;
136 wrappedRuntime.runDelayed = wrapRunDelayed;
138 var assignedPropTracker = Object.create(wrappedRuntime);
139 var moduleInstance = module.make(assignedPropTracker);
141 // make sure the signal graph is actually a signal & extract the visual model
142 if ( !('recv' in moduleInstance.main) ) {
143 moduleInstance.main = Elm.Signal.make(runtime).constant(moduleInstance.main);
146 // The main module stores imported modules onto the runtime.
147 // To ensure only one instance of each module is created,
148 // we assign them back on the original runtime object.
149 Object.keys(assignedPropTracker).forEach(function(key) {
150 runtime[key] = assignedPropTracker[key];
153 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
154 var tracePath = tracePathInit(runtime, moduleInstance.main);
157 moduleInstance: moduleInstance,
158 moduleNodes: moduleNodes,
159 initialNodeValues: saveNodeValues(moduleNodes),
160 initialAsyncCallbacks: asyncCallbacks.slice(),
162 clearAsyncCallbacks: clearAsyncCallbacks,
163 clearRecordedEvents: clearRecordedEvents,
164 getRecordedEventsLength: getRecordedEventsLength,
165 getRecordedEventAt: getRecordedEventAt,
166 copyRecordedEvents: copyRecordedEvents,
167 loadRecordedEvents: loadRecordedEvents,
168 getPaused: getPaused,
169 setPaused: setPaused,
170 setContinue: setContinue,
171 tracePath: tracePath,
172 watchTracker: watchTracker,
176function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
177 var eventCounter = 0;
179 function resetProgram() {
180 debugModule.clearAsyncCallbacks();
181 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
185 function restartProgram() {
187 debugModule.watchTracker.clear();
188 debugModule.tracePath.clearTraces();
189 debugModule.clearRecordedEvents();
190 debugModule.setContinue(0);
191 executeCallbacks(debugModule.initialAsyncCallbacks, true);
194 function pauseProgram() {
195 debugModule.setPaused();
196 eventCounter = debugModule.getRecordedEventsLength();
199 function continueProgram() {
200 if (debugModule.getPaused())
202 if(eventCounter === 0) {
207 var index = eventCounter;
209 debugModule.tracePath.clearTraces();
210 debugModule.tracePath.startRecording();
212 debugModule.setContinue(eventCounter);
216 function stepTo(index) {
217 if (!debugModule.getPaused()) {
218 debugModule.setPaused();
222 if (index < 0 || index > getMaxSteps()) {
223 throw "Index out of bounds: " + index;
226 if (index < eventCounter) {
231 assert(index >= eventCounter, "index must be bad");
232 while (eventCounter < index) {
233 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
234 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
238 assert(eventCounter == index, "while loop didn't work");
241 function getMaxSteps() {
242 return debugModule.getRecordedEventsLength();
245 function doPlayback(eventList) {
246 var x = eventList.shift();
248 runtime.notify(x[0], x[1], x[2]);
250 if (eventList.length > 0) {
251 var delta = eventList[0][2] - time;
252 setTimeout(function() { doPlayback(eventList); }, delta);
256 function redrawGraphics() {
257 var main = debugModule.moduleInstance.main
258 for (var i = main.kids.length ; i-- ; ) {
259 main.kids[i].recv(Date.now(), true, main.id);
263 function getHotSwapState() {
264 var counter = eventCounter;
265 if (!debugModule.getPaused()) {
266 counter = getMaxSteps();
269 recordedEvents: debugModule.copyRecordedEvents(),
270 eventCounter: counter
275 var parentNode = runtime.node.parentNode;
276 parentNode.removeChild(debugModule.tracePath.canvas);
277 parentNode.removeChild(runtime.node);
281 // The problem is that we want to previous paused state. But
282 // by the time JS reaches here, the old code has been swapped out
283 // and the new modules are being generated. So we can ask the
284 // debugging console what it thinks the pause state is and go
286 var paused = top.debug.paused;
287 debugModule.setPaused();
288 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
290 // draw new trace path
291 debugModule.tracePath.startRecording();
292 stepTo(getMaxSteps());
293 debugModule.tracePath.stopRecording();
295 stepTo(hotSwapState.eventCounter);
297 debugModule.setPaused();
299 debugModule.setContinue(hotSwapState.eventCounter);
303 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
306 restart: restartProgram,
308 kontinue: continueProgram,
309 getMaxSteps: getMaxSteps,
311 getPaused: debugModule.getPaused,
312 getHotSwapState: getHotSwapState,
314 allNodes: debugModule.moduleNodes,
315 watchTracker: debugModule.watchTracker,
316 mainNode: debugModule.moduleInstance.main
322function Point(x, y) {
326 this.translate = function(x, y) {
327 return new Point(this.x + x, this.y + y);
330 this.equals = function(p) {
331 return this.x == p.x && this.y == p.y;
335function tracePathInit(runtime, mainNode) {
336 var List = Elm.List.make(runtime);
337 var Signal = Elm.Signal.make(runtime);
338 var tracePathNode = A2(Signal.lift, graphicsUpdate, mainNode);
339 var tracePathCanvas = createCanvas();
340 var tracePositions = {};
341 var recordingTraces = true;
343 function findPositions(currentScene) {
345 function processElement(elem, offset) {
346 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
348 List.map(F2(processForm)(offset))(elem.element.model.forms);
350 if (elem.props.debugTracePathId)
352 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
356 function processForm(offset, form) {
357 if (form.form.ctor == "FElement")
359 processElement(form.form._0, offset.translate(form.x, -form.y));
363 processElement(currentScene, new Point(0, 0));
367 function appendPositions(positions) {
368 for (var id in positions) {
369 var pos = positions[id];
370 if (tracePositions.hasOwnProperty(id)) {
371 var points = tracePositions[id];
372 if (!pos.equals(points[points.length-1])) {
377 tracePositions[id] = [pos];
382 function graphicsUpdate(currentScene) {
383 if (!recordingTraces) {
387 var ctx = tracePathCanvas.getContext('2d');
388 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
391 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
392 appendPositions(findPositions(currentScene));
393 for (var id in tracePositions)
396 var points = tracePositions[id];
397 for (var i=0; i < points.length; i++)
401 ctx.moveTo(p.x, p.y);
404 ctx.lineTo(p.x, p.y);
408 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
411 for (var i=0; i < points.length; i++)
415 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
416 ctx.fillStyle = "rgba(50, 50, 50, 0.4)";
424 function clearTraces() {
428 function stopRecording() {
429 recordingTraces = false;
432 function startRecording() {
433 recordingTraces = true;
437 graphicsUpdate: graphicsUpdate,
438 canvas: tracePathCanvas,
439 clearTraces: clearTraces,
440 stopRecording: stopRecording,
441 startRecording: startRecording
446function executeCallbacks(callbacks, reexecute) {
447 callbacks.forEach(function(timer) {
448 if (reexecute || !timer.executed) {
449 var func = timer.func;
450 timer.executed = true;
456function createCanvas() {
457 var c = document.createElement('canvas');
458 c.width = window.innerWidth;
459 c.height = window.innerHeight;
460 c.style.position = "absolute";
463 c.style.pointerEvents = "none";
467function assert(bool, msg) {
469 throw "Assertion error: " + msg;
473function saveNodeValues(allNodes) {
476 allNodes.forEach(function(node) {
477 nodeValues.push({ value: node.value, id: node.id });
484function restoreNodeValues(allNodes, nodeStates) {
485 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
486 for (var i=0; i < allNodes.length; i++) {
487 var node = allNodes[i];
488 var state = nodeStates[i];
489 assert(node.id == state.id, "the nodes moved position");
491 node.value = state.value;
495function flattenNodes(nodes) {
498 function addAllToDict(node) {
499 nodesById[node.id] = node;
500 node.kids.forEach(addAllToDict);
502 nodes.forEach(addAllToDict);
504 var allNodes = Object.keys(nodesById).sort().map(function(key) {
505 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