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
272function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
273 var currentEventIndex = 0;
275 function resetProgram(position) {
276 var closestSnapshot = debugModule.getSnapshotAt(position);
277 debugModule.clearAsyncCallbacks();
278 restoreSnapshot(debugModule.signalGraphNodes, closestSnapshot);
282 function restartProgram() {
285 debugModule.watchTracker.clear();
286 debugModule.tracePath.clearTraces();
287 debugModule.clearRecordedEvents();
288 debugModule.clearSnapshots();
289 debugModule.setContinue(0);
290 executeCallbacks(debugModule.initialAsyncCallbacks);
293 function pauseProgram() {
294 debugModule.setPaused();
295 currentEventIndex = debugModule.getRecordedEventsLength();
298 function continueProgram() {
299 if (debugModule.getPaused())
301 var closestSnapshotIndex =
302 Math.floor(currentEventIndex / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
303 resetProgram(currentEventIndex);
304 var continueIndex = currentEventIndex;
305 currentEventIndex = closestSnapshotIndex;
306 stepTo(continueIndex);
307 debugModule.setContinue(currentEventIndex);
311 function stepTo(index) {
312 if (!debugModule.getPaused()) {
313 debugModule.setPaused();
317 if (index < 0 || index > getMaxSteps()) {
318 throw "Index out of bounds: " + index;
321 if (index < currentEventIndex) {
322 var closestSnapshotIndex = Math.floor(index / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
324 currentEventIndex = closestSnapshotIndex;
327 while (currentEventIndex < index) {
328 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
329 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
331 currentEventIndex += 1;
335 function getMaxSteps() {
336 return debugModule.getRecordedEventsLength();
339 function redrawGraphics() {
340 var main = debugModule.debuggedModule.main
341 for (var i = main.kids.length ; i-- ; ) {
342 main.kids[i].recv(runtime.timer.now(), true, main.id);
346 function getHotSwapState() {
347 var continueIndex = currentEventIndex;
348 if (!debugModule.getPaused()) {
349 continueIndex = getMaxSteps();
352 recordedEvents: debugModule.copyRecordedEvents(),
353 currentEventIndex: continueIndex
358 var parentNode = runtime.node.parentNode;
359 parentNode.removeChild(debugModule.tracePath.canvas);
360 parentNode.removeChild(runtime.node);
364 // The problem is that we want to previous paused state. But
365 // by the time JS reaches here, the old code has been swapped out
366 // and the new modules are being generated. So we can ask the
367 // debugging console what it thinks the pause state is and go
369 var paused = elmPauseState || false;
370 debugModule.setPaused();
371 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
372 var index = getMaxSteps();
373 runtime.debuggerStatus.eventCounter = 0;
374 debugModule.tracePath.clearTraces();
376 // draw new trace path
377 debugModule.tracePath.startRecording();
378 while(currentEventIndex < index) {
379 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
380 runtime.debuggerStatus.eventCounter += 1;
381 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
382 debugModule.snapshotOnCheckpoint();
383 currentEventIndex += 1;
385 debugModule.tracePath.stopRecording();
387 stepTo(hotSwapState.currentEventIndex);
389 debugModule.setContinue(hotSwapState.currentEventIndex);
393 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
396 restart: restartProgram,
398 kontinue: continueProgram,
399 getMaxSteps: getMaxSteps,
401 getPaused: debugModule.getPaused,
402 getHotSwapState: getHotSwapState,
404 allNodes: debugModule.signalGraphNodes,
405 watchTracker: debugModule.watchTracker
411function Point(x, y) {
415 this.translate = function(x, y) {
416 return new Point(this.x + x, this.y + y);
419 this.equals = function(p) {
420 return this.x == p.x && this.y == p.y;
424function tracePathInit(runtime, signalGraphMain) {
425 var List = Elm.List.make(runtime);
426 var Signal = Elm.Signal.make(runtime);
427 var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain);
428 var tracePathCanvas = createCanvas();
429 var tracePositions = {};
430 var recordingTraces = true;
432 function findPositions(currentScene) {
434 function processElement(elem, offset) {
435 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
437 List.map(F2(processForm)(offset))(elem.element.model.forms);
441 function processForm(offset, form) {
442 if (form.form.ctor == "FElement")
444 processElement(form.form._0, offset.translate(form.x, -form.y));
446 if (form.form.ctor == "FGroup")
448 var newOffset = offset.translate(form.x, -form.y);
449 List.map(F2(processForm)(newOffset))(form.form._1);
451 if (form.debugTracePathId)
453 positions[form.debugTracePathId] = new Point(form.x + offset.x, -form.y + offset.y);
457 processElement(currentScene, new Point(0, 0));
461 function appendPositions(positions) {
462 for (var id in positions) {
463 var pos = positions[id];
464 if (tracePositions.hasOwnProperty(id)) {
465 tracePositions[id].push(pos);
468 tracePositions[id] = [pos];
470 if (tracePositions[id].length < runtime.debuggerStatus.eventCounter) {
471 var padCount = runtime.debuggerStatus.eventCounter - tracePositions[id].length;
472 var lastTracePosition = tracePositions[id][tracePositions[id].length - 1];
473 for (var i = padCount; i--;) {
474 tracePositions[id].push(lastTracePosition)
477 assert(tracePositions[id].length === runtime.debuggerStatus.eventCounter,
478 "We don't have a 1-1 mapping of trace positions to events");
482 function graphicsUpdate(currentScene) {
483 if (!recordingTraces) {
487 var ctx = tracePathCanvas.getContext('2d');
488 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
491 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
492 appendPositions(findPositions(currentScene));
493 for (var id in tracePositions)
496 var points = tracePositions[id];
497 for (var i=0; i < points.length; i++)
501 ctx.moveTo(p.x, p.y);
504 ctx.lineTo(p.x, p.y);
508 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
515 function clearTraces() {
519 function stopRecording() {
520 recordingTraces = false;
523 function startRecording() {
524 recordingTraces = true;
527 function clearTracesAfter(position) {
529 for (var id in tracePositions) {
530 newTraces[id] = tracePositions[id].slice(0,position);
532 tracePositions = newTraces;
536 graphicsUpdate: graphicsUpdate,
537 canvas: tracePathCanvas,
538 clearTraces: clearTraces,
539 clearTracesAfter: clearTracesAfter,
540 stopRecording: stopRecording,
541 startRecording: startRecording
545function executeCallbacks(callbacks) {
546 callbacks.forEach(function(timer) {
547 if (!timer.executed) {
548 var func = timer.func;
549 timer.executed = true;
555function createCanvas() {
556 var c = document.createElement('canvas');
557 c.width = window.innerWidth;
558 c.height = window.innerHeight;
559 c.style.position = "absolute";
562 c.style.pointerEvents = "none";
566function assert(bool, msg) {
568 throw "Assertion error: " + msg;
572function snapshotSignalGraph(signalGraphNodes) {
575 signalGraphNodes.forEach(function(node) {
576 nodeValues.push({ value: node.value, id: node.id });
582function restoreSnapshot(signalGraphNodes, snapshot) {
583 assert(signalGraphNodes.length == snapshot.length,
584 "saved program state has wrong length");
585 for (var i=0; i < signalGraphNodes.length; i++) {
586 var node = signalGraphNodes[i];
587 var state = snapshot[i];
588 assert(node.id == state.id, "the nodes moved position");
590 node.value = state.value;
594function flattenSignalGraph(nodes) {
597 function addAllToDict(node) {
598 nodesById[node.id] = node;
599 node.kids.forEach(addAllToDict);
601 nodes.forEach(addAllToDict);
603 var allNodes = Object.keys(nodesById).sort().map(function(key) {
604 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