4if (!window.location.origin) {
5 window.location.origin =
6 window.location.protocol + "//" +
7 window.location.hostname +
8 (window.location.port ? (':' + window.location.port) : '');
12Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
14 make: function(runtime) {
15 var wrappedModule = debugModule(module, runtime);
16 Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState);
17 dispatchElmDebuggerInit();
18 return wrappedModule.debuggedModule;
23var EVENTS_PER_SAVE = 100;
25function dispatchElmDebuggerInit() {
27 var dispatch = function() {
28 parent.window.postMessage("elmDebuggerInit", window.location.origin);
30 if (parent.window.document.readyState === "complete") {
34 parent.window.addEventListener("load", dispatch);
39function debugModule(module, runtime) {
40 var programPaused = false;
41 var recordedEvents = [];
42 var asyncCallbacks = [];
44 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
46 var eventsUntilSnapshot = EVENTS_PER_SAVE;
47 runtime.debuggerStatus = runtime.debuggerStatus || {};
48 runtime.debuggerStatus.eventCounter = runtime.debuggerStatus.eventCounter || 0;
50 // runtime is the prototype of wrappedRuntime
51 // so we can access all runtime properties too
52 var wrappedRuntime = Object.create(runtime);
53 wrappedRuntime.notify = wrapNotify;
54 wrappedRuntime.runDelayed = wrapRunDelayed;
56 // make a copy of the wrappedRuntime
57 var assignedPropTracker = Object.create(wrappedRuntime);
58 var debuggedModule = module.make(assignedPropTracker);
60 // make sure the signal graph is actually a signal & extract the visual model
61 if ( !('recv' in debuggedModule.main) ) {
62 debuggedModule.main = Elm.Signal.make(runtime).constant(debuggedModule.main);
65 // The main module stores imported modules onto the runtime.
66 // To ensure only one instance of each module is created,
67 // we assign them back on the original runtime object.
68 Object.keys(assignedPropTracker).forEach(function(key) {
69 runtime[key] = assignedPropTracker[key];
72 var signalGraphNodes = flattenSignalGraph(wrappedRuntime.inputs);
73 var tracePath = tracePathInit(runtime, debuggedModule.main);
75 snapshots.push(snapshotSignalGraph(signalGraphNodes));
77 function wrapNotify(id, v) {
78 var timestep = runtime.timer.now();
81 // ignore async events generated while playing back
82 // or user events while program is paused
86 recordEvent(id, v, timestep);
87 var changed = runtime.notify(id, v, timestep);
88 snapshotOnCheckpoint();
90 parent.window.postMessage("elmNotify", window.location.origin);
96 function wrapRunDelayed(func, delayMs) {
98 // Don't push timers and such to the callback stack while we're paused.
99 // It causes too many callbacks to be fired during unpausing.
102 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
103 var timerId = setTimeout(function() {
104 cbObj.executed = true;
107 cbObj.timerId = timerId;
108 asyncCallbacks.push(cbObj);
112 function recordEvent(id, v, timestep) {
113 watchTracker.pushFrame();
114 recordedEvents.push({ id:id, value:v, timestep:timestep });
115 runtime.debuggerStatus.eventCounter += 1;
118 function clearAsyncCallbacks() {
119 asyncCallbacks.forEach(function(timer) {
120 if (!timer.executed) {
121 clearTimeout(timer.timerId);
126 function clearRecordedEvents() {
128 runtime.debuggerStatus.eventCounter = 0;
131 function getRecordedEventsLength() {
132 return recordedEvents.length;
135 function getRecordedEventAt(i) {
136 return recordedEvents[i];
139 function copyRecordedEvents() {
140 return recordedEvents.slice();
143 function loadRecordedEvents(events) {
144 recordedEvents = events.slice();
147 function clearSnapshots() {
148 snapshots = [snapshotSignalGraph(signalGraphNodes)];
151 function getSnapshotAt(i) {
152 var snapshotEvent = Math.floor(i / EVENTS_PER_SAVE);
153 assert(snapshotEvent < snapshots.length && snapshotEvent >= 0,
154 "Out of bounds index: " + snapshotEvent);
155 return snapshots[snapshotEvent];
158 function snapshotOnCheckpoint() {
159 if (eventsUntilSnapshot === 1) {
160 snapshots.push(snapshotSignalGraph(signalGraphNodes));
161 eventsUntilSnapshot = EVENTS_PER_SAVE;
163 eventsUntilSnapshot -= 1;
167 function setPaused() {
168 programPaused = true;
169 clearAsyncCallbacks();
170 pauseTime = Date.now();
171 tracePath.stopRecording();
172 preventInputEvents();
175 function setContinue(position) {
176 var pauseDelay = Date.now() - pauseTime;
177 runtime.timer.addDelay(pauseDelay);
178 programPaused = false;
180 // we need to dump the events that are ahead of where we're continuing.
181 var lastSnapshotPosition = Math.floor(position / EVENTS_PER_SAVE);
182 eventsUntilSnapshot = EVENTS_PER_SAVE - (position % EVENTS_PER_SAVE);
183 snapshots = snapshots.slice(0, lastSnapshotPosition + 1);
185 if (position < getRecordedEventsLength()) {
186 var lastEventTime = recordedEvents[position].timestep;
187 var scrubTime = runtime.timer.now() - lastEventTime;
188 runtime.timer.addDelay(scrubTime);
191 recordedEvents = recordedEvents.slice(0, position);
192 tracePath.clearTracesAfter(position);
193 runtime.debuggerStatus.eventCounter = position;
194 executeCallbacks(asyncCallbacks);
197 tracePath.startRecording();
200 function getPaused() {
201 return programPaused;
204 function preventInputEvents(){
206 [ "click", "mousemove", "mouseup", "mousedown", "mouseclick"
207 , "keydown", "keypress", "keyup", "touchstart", "touchend"
208 , "touchcancel", "touchleave", "touchmove", "pointermove"
209 , "pointerdown", "pointerup", "pointerover", "pointerout"
210 , "pointerenter", "pointerleave", "pointercancel"
213 var ignore = function(e) {
214 var evt = e ? e : window.event;
215 if (evt.stopPropagation) {
216 evt.stopPropagation();
218 if (evt.cabcelBubble !== null) {
219 evt.cabcelBubble = true;
221 if (evt.preventDefault) {
222 evt.preventDefault();
227 var ignoringDiv = document.getElementById("elmEventIgnorer");
229 ignoringDiv = document.createElement("div");
230 ignoringDiv.id = "elmEventIgnorer";
231 ignoringDiv.style.position = "absolute";
232 ignoringDiv.style.top = "0px";
233 ignoringDiv.style.left = "0px";
234 ignoringDiv.style.width = "100%";
235 ignoringDiv.style.height = "100%";
237 for (var i = events.length; i-- ;) {
238 ignoringDiv.addEventListener(events[i], ignore, true);
240 runtime.node.appendChild(ignoringDiv);
244 function permitInputEvents(){
245 var ignoringDiv = document.getElementById("elmEventIgnorer");
246 ignoringDiv.parentNode.removeChild(ignoringDiv);
250 debuggedModule: debuggedModule,
251 signalGraphNodes: signalGraphNodes,
252 initialSnapshot: snapshotSignalGraph(signalGraphNodes),
253 initialAsyncCallbacks: asyncCallbacks.slice(),
255 clearAsyncCallbacks: clearAsyncCallbacks,
256 clearRecordedEvents: clearRecordedEvents,
257 getRecordedEventsLength: getRecordedEventsLength,
258 getRecordedEventAt: getRecordedEventAt,
259 copyRecordedEvents: copyRecordedEvents,
260 loadRecordedEvents: loadRecordedEvents,
261 clearSnapshots: clearSnapshots,
262 getSnapshotAt: getSnapshotAt,
263 snapshotOnCheckpoint: snapshotOnCheckpoint,
264 getPaused: getPaused,
265 setPaused: setPaused,
266 setContinue: setContinue,
267 tracePath: tracePath,
268 watchTracker: watchTracker
272// The debuggerHistory variable is passed in on hotswap. It represents
273// the a state of the debugger for it to assume during init. It contains
274// the paused state of the debugger, the recorded events, and the current
275// event being processed.
276function debuggerInit(debugModule, runtime, debuggerHistory /* =undefined */) {
277 var currentEventIndex = 0;
279 function resetProgram(position) {
280 var closestSnapshot = debugModule.getSnapshotAt(position);
281 debugModule.clearAsyncCallbacks();
282 restoreSnapshot(debugModule.signalGraphNodes, closestSnapshot);
286 function restartProgram() {
289 debugModule.watchTracker.clear();
290 debugModule.tracePath.clearTraces();
291 debugModule.clearRecordedEvents();
292 debugModule.clearSnapshots();
293 debugModule.setContinue(0);
294 executeCallbacks(debugModule.initialAsyncCallbacks);
297 function pauseProgram() {
298 debugModule.setPaused();
299 currentEventIndex = debugModule.getRecordedEventsLength();
302 function continueProgram() {
303 if (debugModule.getPaused())
305 var closestSnapshotIndex =
306 Math.floor(currentEventIndex / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
307 resetProgram(currentEventIndex);
308 var continueIndex = currentEventIndex;
309 currentEventIndex = closestSnapshotIndex;
310 stepTo(continueIndex);
311 debugModule.setContinue(currentEventIndex);
315 function stepTo(index) {
316 if (!debugModule.getPaused()) {
317 debugModule.setPaused();
321 if (index < 0 || index > getMaxSteps()) {
322 throw "Index out of bounds: " + index;
325 if (index < currentEventIndex) {
326 var closestSnapshotIndex = Math.floor(index / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
328 currentEventIndex = closestSnapshotIndex;
331 while (currentEventIndex < index) {
332 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
333 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
335 currentEventIndex += 1;
339 function getMaxSteps() {
340 return debugModule.getRecordedEventsLength();
343 function redrawGraphics() {
344 var main = debugModule.debuggedModule.main
345 for (var i = main.kids.length ; i-- ; ) {
346 main.kids[i].recv(runtime.timer.now(), true, main.id);
350 function getHotSwapState() {
351 var continueIndex = currentEventIndex;
352 if (!debugModule.getPaused()) {
353 continueIndex = getMaxSteps();
356 paused: debugModule.getPaused(),
357 recordedEvents: debugModule.copyRecordedEvents(),
358 currentEventIndex: continueIndex
363 var parentNode = runtime.node.parentNode;
364 parentNode.removeChild(debugModule.tracePath.canvas);
365 parentNode.removeChild(runtime.node);
368 if (debuggerHistory) {
369 // The problem is that we want to previous paused state. But
370 // by the time JS reaches here, the old code has been swapped out
371 // and the new modules are being generated. So we can ask the
372 // debugging console what it thinks the pause state is and go
374 var paused = debuggerHistory.paused;
375 debugModule.setPaused();
376 debugModule.loadRecordedEvents(debuggerHistory.recordedEvents);
377 var index = getMaxSteps();
378 runtime.debuggerStatus.eventCounter = 0;
379 debugModule.tracePath.clearTraces();
381 // draw new trace path
382 debugModule.tracePath.startRecording();
383 while(currentEventIndex < index) {
384 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
385 runtime.debuggerStatus.eventCounter += 1;
386 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
387 debugModule.snapshotOnCheckpoint();
388 currentEventIndex += 1;
390 debugModule.tracePath.stopRecording();
392 stepTo(debuggerHistory.currentEventIndex);
394 debugModule.setContinue(debuggerHistory.currentEventIndex);
398 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
401 restart: restartProgram,
403 kontinue: continueProgram,
404 getMaxSteps: getMaxSteps,
406 getPaused: debugModule.getPaused,
407 getHotSwapState: getHotSwapState,
409 allNodes: debugModule.signalGraphNodes,
410 watchTracker: debugModule.watchTracker
416function Point(x, y) {
420 this.translate = function(x, y) {
421 return new Point(this.x + x, this.y + y);
424 this.equals = function(p) {
425 return this.x == p.x && this.y == p.y;
429function tracePathInit(runtime, signalGraphMain) {
430 var List = Elm.List.make(runtime);
431 var Signal = Elm.Signal.make(runtime);
432 var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain);
433 var tracePathCanvas = createCanvas();
434 var tracePositions = {};
435 var recordingTraces = true;
437 function findPositions(currentScene) {
439 function processElement(elem, offset) {
440 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
442 List.map(F2(processForm)(offset))(elem.element.model.forms);
446 function processForm(offset, form) {
447 if (form.form.ctor == "FElement")
449 processElement(form.form._0, offset.translate(form.x, -form.y));
451 if (form.form.ctor == "FGroup")
453 var newOffset = offset.translate(form.x, -form.y);
454 List.map(F2(processForm)(newOffset))(form.form._1);
456 if (form.debugTracePathId)
458 positions[form.debugTracePathId] = new Point(form.x + offset.x, -form.y + offset.y);
462 processElement(currentScene, new Point(0, 0));
466 function appendPositions(positions) {
467 for (var id in positions) {
468 var pos = positions[id];
469 if (tracePositions.hasOwnProperty(id)) {
470 tracePositions[id].push(pos);
473 tracePositions[id] = [pos];
475 if (tracePositions[id].length < runtime.debuggerStatus.eventCounter) {
476 var padCount = runtime.debuggerStatus.eventCounter - tracePositions[id].length;
477 var lastTracePosition = tracePositions[id][tracePositions[id].length - 1];
478 for (var i = padCount; i--;) {
479 tracePositions[id].push(lastTracePosition)
482 assert(tracePositions[id].length === runtime.debuggerStatus.eventCounter,
483 "We don't have a 1-1 mapping of trace positions to events");
487 function graphicsUpdate(currentScene) {
488 if (!recordingTraces) {
492 var ctx = tracePathCanvas.getContext('2d');
493 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
496 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
497 appendPositions(findPositions(currentScene));
498 for (var id in tracePositions)
501 var points = tracePositions[id];
502 for (var i=0; i < points.length; i++)
506 ctx.moveTo(p.x, p.y);
509 ctx.lineTo(p.x, p.y);
513 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
520 function clearTraces() {
524 function stopRecording() {
525 recordingTraces = false;
528 function startRecording() {
529 recordingTraces = true;
532 function clearTracesAfter(position) {
534 for (var id in tracePositions) {
535 newTraces[id] = tracePositions[id].slice(0,position);
537 tracePositions = newTraces;
541 graphicsUpdate: graphicsUpdate,
542 canvas: tracePathCanvas,
543 clearTraces: clearTraces,
544 clearTracesAfter: clearTracesAfter,
545 stopRecording: stopRecording,
546 startRecording: startRecording
550function executeCallbacks(callbacks) {
551 callbacks.forEach(function(timer) {
552 if (!timer.executed) {
553 var func = timer.func;
554 timer.executed = true;
560function createCanvas() {
561 var c = document.createElement('canvas');
562 c.width = window.innerWidth;
563 c.height = window.innerHeight;
564 c.style.position = "absolute";
567 c.style.pointerEvents = "none";
571function assert(bool, msg) {
573 throw "Assertion error: " + msg;
577function snapshotSignalGraph(signalGraphNodes) {
580 signalGraphNodes.forEach(function(node) {
581 nodeValues.push({ value: node.value, id: node.id });
587function restoreSnapshot(signalGraphNodes, snapshot) {
588 assert(signalGraphNodes.length == snapshot.length,
589 "saved program state has wrong length");
590 for (var i=0; i < signalGraphNodes.length; i++) {
591 var node = signalGraphNodes[i];
592 var state = snapshot[i];
593 assert(node.id == state.id, "the nodes moved position");
595 node.value = state.value;
599function flattenSignalGraph(nodes) {
602 function addAllToDict(node) {
603 nodesById[node.id] = node;
604 node.kids.forEach(addAllToDict);
606 nodes.forEach(addAllToDict);
608 var allNodes = Object.keys(nodesById).sort().map(function(key) {
609 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