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;
26function dispatchElmDebuggerInit() {
28 var dispatch = function() {
29 parent.window.postMessage("elmDebuggerInit", window.location.origin);
31 if (parent.window.document.readyState === "complete") {
35 parent.window.addEventListener("load", dispatch);
40function debugModule(module, runtime) {
41 var programPaused = false;
42 var recordedEvents = [];
43 var asyncCallbacks = [];
45 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
47 var eventsUntilSnapshot = EVENTS_PER_SAVE;
49 // runtime is the prototype of wrappedRuntime
50 // so we can access all runtime properties too
51 var wrappedRuntime = Object.create(runtime);
52 wrappedRuntime.notify = wrapNotify;
53 wrappedRuntime.runDelayed = wrapRunDelayed;
55 // make a copy of the wrappedRuntime
56 var assignedPropTracker = Object.create(wrappedRuntime);
57 var debuggedModule = module.make(assignedPropTracker);
59 // make sure the signal graph is actually a signal & extract the visual model
60 if ( !('recv' in debuggedModule.main) ) {
61 debuggedModule.main = Elm.Signal.make(runtime).constant(debuggedModule.main);
64 // The main module stores imported modules onto the runtime.
65 // To ensure only one instance of each module is created,
66 // we assign them back on the original runtime object.
67 Object.keys(assignedPropTracker).forEach(function(key) {
68 runtime[key] = assignedPropTracker[key];
71 var signalGraphNodes = flattenSignalGraph(wrappedRuntime.inputs);
72 var tracePath = tracePathInit(runtime, debuggedModule.main);
74 snapshots.push(snapshotSignalGraph(signalGraphNodes));
76 function wrapNotify(id, v) {
77 var timestep = runtime.timer.now();
80 // ignore async events generated while playing back
81 // or user events while program is paused
85 recordEvent(id, v, timestep);
86 var changed = runtime.notify(id, v, timestep);
87 snapshotOnCheckpoint();
89 parent.window.postMessage("elmNotify", window.location.origin);
95 function wrapRunDelayed(func, delayMs) {
97 // Don't push timers and such to the callback stack while we're paused.
98 // It causes too many callbacks to be fired during unpausing.
101 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
102 var timerId = setTimeout(function() {
103 cbObj.executed = true;
106 cbObj.timerId = timerId;
107 asyncCallbacks.push(cbObj);
111 function recordEvent(id, v, timestep) {
112 watchTracker.pushFrame();
113 recordedEvents.push({ id:id, value:v, timestep:timestep });
117 function clearAsyncCallbacks() {
118 asyncCallbacks.forEach(function(timer) {
119 if (!timer.executed) {
120 clearTimeout(timer.timerId);
125 function clearRecordedEvents() {
130 function getRecordedEventsLength() {
131 return recordedEvents.length;
134 function getRecordedEventAt(i) {
135 return recordedEvents[i];
138 function copyRecordedEvents() {
139 return recordedEvents.slice();
142 function loadRecordedEvents(events) {
143 recordedEvents = events.slice();
146 function clearSnapshots() {
147 snapshots = [snapshotSignalGraph(signalGraphNodes)];
150 function getSnapshotAt(i) {
151 var snapshotEvent = Math.floor(i / EVENTS_PER_SAVE);
152 assert(snapshotEvent < snapshots.length && snapshotEvent >= 0,
153 "Out of bounds index: " + snapshotEvent);
154 return snapshots[snapshotEvent];
157 function snapshotOnCheckpoint() {
158 if (eventsUntilSnapshot === 1) {
159 snapshots.push(snapshotSignalGraph(signalGraphNodes));
160 eventsUntilSnapshot = EVENTS_PER_SAVE;
162 eventsUntilSnapshot -= 1;
166 function setPaused() {
167 programPaused = true;
168 clearAsyncCallbacks();
169 tracePath.stopRecording();
170 pauseTime = Date.now();
173 function setContinue(position) {
174 var timerDelay = Date.now() - pauseTime;
175 runtime.timer.addDelay(timerDelay);
176 programPaused = false;
178 // If we're not unpausing at the head, then we need to dump the
179 // events that are ahead of where we're continuing.
180 var lastSnapshotPosition = Math.floor(position / EVENTS_PER_SAVE);
181 eventsUntilSnapshot = EVENTS_PER_SAVE - (position % EVENTS_PER_SAVE);
182 snapshots = snapshots.slice(0, lastSnapshotPosition + 1);
183 recordedEvents = recordedEvents.slice(0, position);
184 tracePath.clearTracesAfter(position);
185 eventCounter = position;
186 executeCallbacks(asyncCallbacks, false);
188 tracePath.startRecording();
191 function getPaused() {
192 return programPaused;
196 debuggedModule: debuggedModule,
197 signalGraphNodes: signalGraphNodes,
198 initialSnapshot: snapshotSignalGraph(signalGraphNodes),
199 initialAsyncCallbacks: asyncCallbacks.slice(),
201 clearAsyncCallbacks: clearAsyncCallbacks,
202 clearRecordedEvents: clearRecordedEvents,
203 getRecordedEventsLength: getRecordedEventsLength,
204 getRecordedEventAt: getRecordedEventAt,
205 copyRecordedEvents: copyRecordedEvents,
206 loadRecordedEvents: loadRecordedEvents,
207 clearSnapshots: clearSnapshots,
208 getSnapshotAt: getSnapshotAt,
209 snapshotOnCheckpoint: snapshotOnCheckpoint,
210 getPaused: getPaused,
211 setPaused: setPaused,
212 setContinue: setContinue,
213 tracePath: tracePath,
214 watchTracker: watchTracker
218function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
219 var currentEventIndex = 0;
221 function resetProgram(position) {
222 var closestSnapshot = debugModule.getSnapshotAt(position);
223 debugModule.clearAsyncCallbacks();
224 restoreSnapshot(debugModule.signalGraphNodes, closestSnapshot);
228 function restartProgram() {
231 debugModule.watchTracker.clear();
232 debugModule.tracePath.clearTraces();
233 debugModule.clearRecordedEvents();
234 debugModule.clearSnapshots();
235 debugModule.setContinue(0);
236 executeCallbacks(debugModule.initialAsyncCallbacks, true);
239 function pauseProgram() {
240 debugModule.setPaused();
241 currentEventIndex = debugModule.getRecordedEventsLength();
244 function continueProgram() {
245 if (debugModule.getPaused())
247 if (currentEventIndex === 0) {
251 var closestSnapshotIndex =
252 Math.floor(currentEventIndex / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
253 resetProgram(currentEventIndex);
254 var continueIndex = currentEventIndex;
255 currentEventIndex = closestSnapshotIndex;
256 stepTo(continueIndex);
257 debugModule.setContinue(currentEventIndex);
261 function stepTo(index) {
262 if (!debugModule.getPaused()) {
263 debugModule.setPaused();
267 if (index < 0 || index > getMaxSteps()) {
268 throw "Index out of bounds: " + index;
271 if (index < currentEventIndex) {
272 var closestSnapshotIndex = Math.floor(index / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
274 currentEventIndex = closestSnapshotIndex;
277 while (currentEventIndex < index) {
278 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
279 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
281 currentEventIndex += 1;
285 function getMaxSteps() {
286 return debugModule.getRecordedEventsLength();
289 function redrawGraphics() {
290 var main = debugModule.debuggedModule.main
291 for (var i = main.kids.length ; i-- ; ) {
292 main.kids[i].recv(runtime.timer.now(), true, main.id);
296 function getHotSwapState() {
297 var continueIndex = currentEventIndex;
298 if (!debugModule.getPaused()) {
299 continueIndex = getMaxSteps();
302 recordedEvents: debugModule.copyRecordedEvents(),
303 currentEventIndex: continueIndex
308 var parentNode = runtime.node.parentNode;
309 parentNode.removeChild(debugModule.tracePath.canvas);
310 parentNode.removeChild(runtime.node);
314 // The problem is that we want to previous paused state. But
315 // by the time JS reaches here, the old code has been swapped out
316 // and the new modules are being generated. So we can ask the
317 // debugging console what it thinks the pause state is and go
319 var paused = top.debug.paused;
320 debugModule.setPaused();
321 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
322 var index = getMaxSteps();
324 debugModule.tracePath.clearTraces();
326 // draw new trace path
327 debugModule.tracePath.startRecording();
328 while(currentEventIndex < index) {
329 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
331 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
332 debugModule.snapshotOnCheckpoint();
333 currentEventIndex += 1;
335 debugModule.tracePath.stopRecording();
337 stepTo(hotSwapState.currentEventIndex);
339 debugModule.setContinue(hotSwapState.currentEventIndex);
343 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
346 restart: restartProgram,
348 kontinue: continueProgram,
349 getMaxSteps: getMaxSteps,
351 getPaused: debugModule.getPaused,
352 getHotSwapState: getHotSwapState,
354 allNodes: debugModule.signalGraphNodes,
355 watchTracker: debugModule.watchTracker
361function Point(x, y) {
365 this.translate = function(x, y) {
366 return new Point(this.x + x, this.y + y);
369 this.equals = function(p) {
370 return this.x == p.x && this.y == p.y;
374function tracePathInit(runtime, signalGraphMain) {
375 var List = Elm.List.make(runtime);
376 var Signal = Elm.Signal.make(runtime);
377 var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain);
378 var tracePathCanvas = createCanvas();
379 var tracePositions = {};
380 var recordingTraces = true;
382 function findPositions(currentScene) {
384 function processElement(elem, offset) {
385 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
387 List.map(F2(processForm)(offset))(elem.element.model.forms);
389 if (elem.props.debugTracePathId)
391 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
395 function processForm(offset, form) {
396 if (form.form.ctor == "FElement")
398 processElement(form.form._0, offset.translate(form.x, -form.y));
402 processElement(currentScene, new Point(0, 0));
406 function appendPositions(positions) {
407 for (var id in positions) {
408 var pos = positions[id];
409 if (tracePositions.hasOwnProperty(id)) {
410 tracePositions[id].push(pos);
413 tracePositions[id] = [pos];
415 if (tracePositions[id].length < eventCounter) {
416 var padCount = eventCounter - tracePositions[id].length;
417 var lastTracePosition = tracePositions[id][tracePositions[id].length - 1];
418 for (var i = padCount; i--;) {
419 tracePositions[id].push(lastTracePosition)
422 assert(tracePositions[id].length === eventCounter,
423 "We don't have a 1-1 mapping of trace positions to events");
427 function graphicsUpdate(currentScene) {
428 if (!recordingTraces) {
432 var ctx = tracePathCanvas.getContext('2d');
433 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
436 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
437 appendPositions(findPositions(currentScene));
438 for (var id in tracePositions)
441 var points = tracePositions[id];
442 for (var i=0; i < points.length; i++)
446 ctx.moveTo(p.x, p.y);
449 ctx.lineTo(p.x, p.y);
453 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
460 function clearTraces() {
464 function stopRecording() {
465 recordingTraces = false;
468 function startRecording() {
469 recordingTraces = true;
472 function clearTracesAfter(position) {
474 for (var id in tracePositions) {
475 newTraces[id] = tracePositions[id].slice(0,position);
477 tracePositions = newTraces;
481 graphicsUpdate: graphicsUpdate,
482 canvas: tracePathCanvas,
483 clearTraces: clearTraces,
484 clearTracesAfter: clearTracesAfter,
485 stopRecording: stopRecording,
486 startRecording: startRecording
490function executeCallbacks(callbacks, reexecute) {
491 callbacks.forEach(function(timer) {
492 if (reexecute || !timer.executed) {
493 var func = timer.func;
494 timer.executed = true;
500function createCanvas() {
501 var c = document.createElement('canvas');
502 c.width = window.innerWidth;
503 c.height = window.innerHeight;
504 c.style.position = "absolute";
507 c.style.pointerEvents = "none";
511function assert(bool, msg) {
513 throw "Assertion error: " + msg;
517function snapshotSignalGraph(signalGraphNodes) {
520 signalGraphNodes.forEach(function(node) {
521 nodeValues.push({ value: node.value, id: node.id });
527function restoreSnapshot(signalGraphNodes, snapshot) {
528 assert(signalGraphNodes.length == snapshot.length,
529 "saved program state has wrong length");
530 for (var i=0; i < signalGraphNodes.length; i++) {
531 var node = signalGraphNodes[i];
532 var state = snapshot[i];
533 assert(node.id == state.id, "the nodes moved position");
535 node.value = state.value;
539function flattenSignalGraph(nodes) {
542 function addAllToDict(node) {
543 nodesById[node.id] = node;
544 node.kids.forEach(addAllToDict);
546 nodes.forEach(addAllToDict);
548 var allNodes = Object.keys(nodesById).sort().map(function(key) {
549 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