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 tracePath.stopRecording();
171 pauseTime = Date.now();
174 function setContinue(position) {
175 var pauseDelay = Date.now() - pauseTime;
176 runtime.timer.addDelay(pauseDelay);
177 programPaused = false;
179 // we need to dump the 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);
184 if (position < getRecordedEventsLength()) {
185 var lastEventTime = recordedEvents[position].timestep;
186 var scrubTime = runtime.timer.now() - lastEventTime;
187 runtime.timer.addDelay(scrubTime);
190 recordedEvents = recordedEvents.slice(0, position);
191 tracePath.clearTracesAfter(position);
192 runtime.debuggerStatus.eventCounter = position;
193 executeCallbacks(asyncCallbacks, false);
195 tracePath.startRecording();
198 function getPaused() {
199 return programPaused;
203 debuggedModule: debuggedModule,
204 signalGraphNodes: signalGraphNodes,
205 initialSnapshot: snapshotSignalGraph(signalGraphNodes),
206 initialAsyncCallbacks: asyncCallbacks.slice(),
208 clearAsyncCallbacks: clearAsyncCallbacks,
209 clearRecordedEvents: clearRecordedEvents,
210 getRecordedEventsLength: getRecordedEventsLength,
211 getRecordedEventAt: getRecordedEventAt,
212 copyRecordedEvents: copyRecordedEvents,
213 loadRecordedEvents: loadRecordedEvents,
214 clearSnapshots: clearSnapshots,
215 getSnapshotAt: getSnapshotAt,
216 snapshotOnCheckpoint: snapshotOnCheckpoint,
217 getPaused: getPaused,
218 setPaused: setPaused,
219 setContinue: setContinue,
220 tracePath: tracePath,
221 watchTracker: watchTracker
225function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
226 var currentEventIndex = 0;
228 function resetProgram(position) {
229 var closestSnapshot = debugModule.getSnapshotAt(position);
230 debugModule.clearAsyncCallbacks();
231 restoreSnapshot(debugModule.signalGraphNodes, closestSnapshot);
235 function restartProgram() {
238 debugModule.watchTracker.clear();
239 debugModule.tracePath.clearTraces();
240 debugModule.clearRecordedEvents();
241 debugModule.clearSnapshots();
242 debugModule.setContinue(0);
243 executeCallbacks(debugModule.initialAsyncCallbacks, true);
246 function pauseProgram() {
247 debugModule.setPaused();
248 currentEventIndex = debugModule.getRecordedEventsLength();
251 function continueProgram() {
252 if (debugModule.getPaused())
254 var closestSnapshotIndex =
255 Math.floor(currentEventIndex / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
256 resetProgram(currentEventIndex);
257 var continueIndex = currentEventIndex;
258 currentEventIndex = closestSnapshotIndex;
259 stepTo(continueIndex);
260 debugModule.setContinue(currentEventIndex);
264 function stepTo(index) {
265 if (!debugModule.getPaused()) {
266 debugModule.setPaused();
270 if (index < 0 || index > getMaxSteps()) {
271 throw "Index out of bounds: " + index;
274 if (index < currentEventIndex) {
275 var closestSnapshotIndex = Math.floor(index / EVENTS_PER_SAVE) * EVENTS_PER_SAVE;
277 currentEventIndex = closestSnapshotIndex;
280 while (currentEventIndex < index) {
281 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
282 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
284 currentEventIndex += 1;
288 function getMaxSteps() {
289 return debugModule.getRecordedEventsLength();
292 function redrawGraphics() {
293 var main = debugModule.debuggedModule.main
294 for (var i = main.kids.length ; i-- ; ) {
295 main.kids[i].recv(runtime.timer.now(), true, main.id);
299 function getHotSwapState() {
300 var continueIndex = currentEventIndex;
301 if (!debugModule.getPaused()) {
302 continueIndex = getMaxSteps();
305 recordedEvents: debugModule.copyRecordedEvents(),
306 currentEventIndex: continueIndex
311 var parentNode = runtime.node.parentNode;
312 parentNode.removeChild(debugModule.tracePath.canvas);
313 parentNode.removeChild(runtime.node);
317 // The problem is that we want to previous paused state. But
318 // by the time JS reaches here, the old code has been swapped out
319 // and the new modules are being generated. So we can ask the
320 // debugging console what it thinks the pause state is and go
322 var paused = top.debug.paused;
323 debugModule.setPaused();
324 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
325 var index = getMaxSteps();
326 runtime.debuggerStatus.eventCounter = 0;
327 debugModule.tracePath.clearTraces();
329 // draw new trace path
330 debugModule.tracePath.startRecording();
331 while(currentEventIndex < index) {
332 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
333 runtime.debuggerStatus.eventCounter += 1;
334 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
335 debugModule.snapshotOnCheckpoint();
336 currentEventIndex += 1;
338 debugModule.tracePath.stopRecording();
340 stepTo(hotSwapState.currentEventIndex);
342 debugModule.setContinue(hotSwapState.currentEventIndex);
346 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
349 restart: restartProgram,
351 kontinue: continueProgram,
352 getMaxSteps: getMaxSteps,
354 getPaused: debugModule.getPaused,
355 getHotSwapState: getHotSwapState,
357 allNodes: debugModule.signalGraphNodes,
358 watchTracker: debugModule.watchTracker
364function Point(x, y) {
368 this.translate = function(x, y) {
369 return new Point(this.x + x, this.y + y);
372 this.equals = function(p) {
373 return this.x == p.x && this.y == p.y;
377function tracePathInit(runtime, signalGraphMain) {
378 var List = Elm.List.make(runtime);
379 var Signal = Elm.Signal.make(runtime);
380 var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain);
381 var tracePathCanvas = createCanvas();
382 var tracePositions = {};
383 var recordingTraces = true;
385 function findPositions(currentScene) {
387 function processElement(elem, offset) {
388 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
390 List.map(F2(processForm)(offset))(elem.element.model.forms);
394 function processForm(offset, form) {
395 if (form.form.ctor == "FElement")
397 processElement(form.form._0, offset.translate(form.x, -form.y));
399 if (form.form.ctor == "FGroup")
401 var newOffset = offset.translate(form.x, -form.y);
402 List.map(F2(processForm)(newOffset))(form.form._1);
404 if (form.debugTracePathId)
406 positions[form.debugTracePathId] = new Point(form.x + offset.x, -form.y + offset.y);
410 processElement(currentScene, new Point(0, 0));
414 function appendPositions(positions) {
415 for (var id in positions) {
416 var pos = positions[id];
417 if (tracePositions.hasOwnProperty(id)) {
418 tracePositions[id].push(pos);
421 tracePositions[id] = [pos];
423 if (tracePositions[id].length < runtime.debuggerStatus.eventCounter) {
424 var padCount = runtime.debuggerStatus.eventCounter - tracePositions[id].length;
425 var lastTracePosition = tracePositions[id][tracePositions[id].length - 1];
426 for (var i = padCount; i--;) {
427 tracePositions[id].push(lastTracePosition)
430 assert(tracePositions[id].length === runtime.debuggerStatus.eventCounter,
431 "We don't have a 1-1 mapping of trace positions to events");
435 function graphicsUpdate(currentScene) {
436 if (!recordingTraces) {
440 var ctx = tracePathCanvas.getContext('2d');
441 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
444 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
445 appendPositions(findPositions(currentScene));
446 for (var id in tracePositions)
449 var points = tracePositions[id];
450 for (var i=0; i < points.length; i++)
454 ctx.moveTo(p.x, p.y);
457 ctx.lineTo(p.x, p.y);
461 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
468 function clearTraces() {
472 function stopRecording() {
473 recordingTraces = false;
476 function startRecording() {
477 recordingTraces = true;
480 function clearTracesAfter(position) {
482 for (var id in tracePositions) {
483 newTraces[id] = tracePositions[id].slice(0,position);
485 tracePositions = newTraces;
489 graphicsUpdate: graphicsUpdate,
490 canvas: tracePathCanvas,
491 clearTraces: clearTraces,
492 clearTracesAfter: clearTracesAfter,
493 stopRecording: stopRecording,
494 startRecording: startRecording
498function executeCallbacks(callbacks, reexecute) {
499 callbacks.forEach(function(timer) {
500 if (reexecute || !timer.executed) {
501 var func = timer.func;
502 timer.executed = true;
508function createCanvas() {
509 var c = document.createElement('canvas');
510 c.width = window.innerWidth;
511 c.height = window.innerHeight;
512 c.style.position = "absolute";
515 c.style.pointerEvents = "none";
519function assert(bool, msg) {
521 throw "Assertion error: " + msg;
525function snapshotSignalGraph(signalGraphNodes) {
528 signalGraphNodes.forEach(function(node) {
529 nodeValues.push({ value: node.value, id: node.id });
535function restoreSnapshot(signalGraphNodes, snapshot) {
536 assert(signalGraphNodes.length == snapshot.length,
537 "saved program state has wrong length");
538 for (var i=0; i < signalGraphNodes.length; i++) {
539 var node = signalGraphNodes[i];
540 var state = snapshot[i];
541 assert(node.id == state.id, "the nodes moved position");
543 node.value = state.value;
547function flattenSignalGraph(nodes) {
550 function addAllToDict(node) {
551 nodesById[node.id] = node;
552 node.kids.forEach(addAllToDict);
554 nodes.forEach(addAllToDict);
556 var allNodes = Object.keys(nodesById).sort().map(function(key) {
557 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