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) {
15 make: function(runtime) {
16 var wrappedModule = debugModule(module, runtime);
17 Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState);
18 dispatchElmDebuggerInit();
19 return wrappedModule.debuggedModule;
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;
45 function wrapNotify(id, v) {
46 var timestep = runtime.timer.now();
49 // ignore async events generated while playing back
50 // or user events while program is paused
54 var changed = runtime.notify(id, v, timestep);
55 recordEvent(id, v, timestep);
57 parent.window.postMessage("elmNotify", window.location.origin);
63 function wrapRunDelayed(func, delayMs) {
65 // Don't push timers and such to the callback stack while we're paused.
66 // It causes too many callbacks to be fired during unpausing.
69 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
70 var timerId = setTimeout(function() {
71 cbObj.executed = true;
74 cbObj.timerId = timerId;
75 asyncCallbacks.push(cbObj);
79 function recordEvent(id, v, timestep) {
80 watchTracker.pushFrame();
81 recordedEvents.push({ id:id, value:v, timestep:timestep });
84 function clearAsyncCallbacks() {
85 asyncCallbacks.forEach(function(timer) {
86 if (!timer.executed) {
87 clearTimeout(timer.timerId);
92 function clearRecordedEvents() {
96 function getRecordedEventsLength() {
97 return recordedEvents.length;
100 function getRecordedEventAt(i) {
101 return recordedEvents[i];
104 function copyRecordedEvents() {
105 return recordedEvents.slice();
108 function loadRecordedEvents(events) {
109 recordedEvents = events.slice();
112 function setPaused() {
113 programPaused = true;
114 clearAsyncCallbacks();
115 tracePath.stopRecording();
116 pauseTime = Date.now();
119 function setContinue(position) {
120 var timerDelay = Date.now() - pauseTime;
121 runtime.timer.addDelay(timerDelay);
122 programPaused = false;
124 // If we're not unpausing at the head, then we need to dump the
125 // events that are ahead of where we're continuing.
126 recordedEvents = recordedEvents.slice(0, position);
127 executeCallbacks(asyncCallbacks, false);
129 tracePath.startRecording();
132 function getPaused() {
133 return programPaused;
136 // runtime is the prototype of wrappedRuntime
137 // so we can access all runtime properties too
138 var wrappedRuntime = Object.create(runtime);
139 wrappedRuntime.notify = wrapNotify;
140 wrappedRuntime.runDelayed = wrapRunDelayed;
142 var assignedPropTracker = Object.create(wrappedRuntime);
143 var debuggedModule = module.make(assignedPropTracker);
145 // make sure the signal graph is actually a signal & extract the visual model
146 if ( !('recv' in debuggedModule.main) ) {
147 debuggedModule.main = Elm.Signal.make(runtime).constant(debuggedModule.main);
150 // The main module stores imported modules onto the runtime.
151 // To ensure only one instance of each module is created,
152 // we assign them back on the original runtime object.
153 Object.keys(assignedPropTracker).forEach(function(key) {
154 runtime[key] = assignedPropTracker[key];
157 var signalGraphNodes = flattenSignalGraph(wrappedRuntime.inputs);
158 var tracePath = tracePathInit(runtime, debuggedModule.main);
161 debuggedModule: debuggedModule,
162 signalGraphNodes: signalGraphNodes,
163 initialSnapshot: snapshotSignalGraph(signalGraphNodes),
164 initialAsyncCallbacks: asyncCallbacks.slice(),
166 clearAsyncCallbacks: clearAsyncCallbacks,
167 clearRecordedEvents: clearRecordedEvents,
168 getRecordedEventsLength: getRecordedEventsLength,
169 getRecordedEventAt: getRecordedEventAt,
170 copyRecordedEvents: copyRecordedEvents,
171 loadRecordedEvents: loadRecordedEvents,
172 getPaused: getPaused,
173 setPaused: setPaused,
174 setContinue: setContinue,
175 tracePath: tracePath,
176 watchTracker: watchTracker
180function debuggerInit(debugModule, runtime, hotSwapState) {
181 var currentEventIndex = 0;
183 function resetProgram() {
184 debugModule.clearAsyncCallbacks();
185 restoreSnapshot(debugModule.signalGraphNodes, debugModule.initialSnapshot);
189 function restartProgram() {
191 debugModule.watchTracker.clear();
192 debugModule.tracePath.clearTraces();
193 debugModule.clearRecordedEvents();
194 debugModule.setContinue(0);
195 executeCallbacks(debugModule.initialAsyncCallbacks, true);
198 function pauseProgram() {
199 debugModule.setPaused();
200 currentEventIndex = debugModule.getRecordedEventsLength();
203 function continueProgram() {
204 if (debugModule.getPaused())
206 if (currentEventIndex === 0) {
211 var continueIndex = currentEventIndex;
212 currentEventIndex = 0;
213 debugModule.tracePath.clearTraces();
214 debugModule.tracePath.startRecording();
215 stepTo(continueIndex);
216 debugModule.setContinue(currentEventIndex);
220 function stepTo(index) {
221 if (!debugModule.getPaused()) {
222 debugModule.setPaused();
226 if (index < 0 || index > getMaxSteps()) {
227 throw "Index out of bounds: " + index;
230 if (index < currentEventIndex) {
232 currentEventIndex = 0;
235 while (currentEventIndex < index) {
236 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
237 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
239 currentEventIndex += 1;
243 function getMaxSteps() {
244 return debugModule.getRecordedEventsLength();
247 function redrawGraphics() {
248 var main = debugModule.debuggedModule.main
249 for (var i = main.kids.length ; i-- ; ) {
250 main.kids[i].recv(runtime.timer.now(), true, main.id);
254 function getHotSwapState() {
255 var continueIndex = currentEventIndex;
256 if (!debugModule.getPaused()) {
257 continueIndex = getMaxSteps();
260 recordedEvents: debugModule.copyRecordedEvents(),
261 currentEventIndex: continueIndex
266 var parentNode = runtime.node.parentNode;
267 parentNode.removeChild(debugModule.tracePath.canvas);
268 parentNode.removeChild(runtime.node);
272 // The problem is that we want to previous paused state. But
273 // by the time JS reaches here, the old code has been swapped out
274 // and the new modules are being generated. So we can ask the
275 // debugging console what it thinks the pause state is and go
277 var paused = top.debug.paused;
278 debugModule.setPaused();
279 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
281 // draw new trace path
282 debugModule.tracePath.startRecording();
283 stepTo(getMaxSteps());
284 debugModule.tracePath.stopRecording();
286 stepTo(hotSwapState.currentEventIndex);
288 debugModule.setPaused();
290 debugModule.setContinue(hotSwapState.currentEventIndex);
294 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
297 restart: restartProgram,
299 kontinue: continueProgram,
300 getMaxSteps: getMaxSteps,
302 getPaused: debugModule.getPaused,
303 getHotSwapState: getHotSwapState,
305 allNodes: debugModule.signalGraphNodes,
306 watchTracker: debugModule.watchTracker,
307 signalGraphMain: debugModule.debuggedModule.main
313function Point(x, y) {
317 this.translate = function(x, y) {
318 return new Point(this.x + x, this.y + y);
321 this.equals = function(p) {
322 return this.x == p.x && this.y == p.y;
326function tracePathInit(runtime, signalGraphMain) {
327 var List = Elm.List.make(runtime);
328 var Signal = Elm.Signal.make(runtime);
329 var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain);
330 var tracePathCanvas = createCanvas();
331 var tracePositions = {};
332 var recordingTraces = true;
334 function findPositions(currentScene) {
336 function processElement(elem, offset) {
337 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
339 List.map(F2(processForm)(offset))(elem.element.model.forms);
341 if (elem.props.debugTracePathId)
343 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
347 function processForm(offset, form) {
348 if (form.form.ctor == "FElement")
350 processElement(form.form._0, offset.translate(form.x, -form.y));
354 processElement(currentScene, new Point(0, 0));
358 function appendPositions(positions) {
359 for (var id in positions) {
360 var pos = positions[id];
361 if (tracePositions.hasOwnProperty(id)) {
362 var points = tracePositions[id];
363 if (!pos.equals(points[points.length-1])) {
368 tracePositions[id] = [pos];
373 function graphicsUpdate(currentScene) {
374 if (!recordingTraces) {
378 var ctx = tracePathCanvas.getContext('2d');
379 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
382 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
383 appendPositions(findPositions(currentScene));
384 for (var id in tracePositions)
387 var points = tracePositions[id];
388 for (var i=0; i < points.length; i++)
392 ctx.moveTo(p.x, p.y);
395 ctx.lineTo(p.x, p.y);
399 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
402 for (var i=0; i < points.length; i++)
406 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
407 ctx.fillStyle = "rgba(50, 50, 50, 0.4)";
415 function clearTraces() {
419 function stopRecording() {
420 recordingTraces = false;
423 function startRecording() {
424 recordingTraces = true;
428 graphicsUpdate: graphicsUpdate,
429 canvas: tracePathCanvas,
430 clearTraces: clearTraces,
431 stopRecording: stopRecording,
432 startRecording: startRecording
437function executeCallbacks(callbacks, reexecute) {
438 callbacks.forEach(function(timer) {
439 if (reexecute || !timer.executed) {
440 var func = timer.func;
441 timer.executed = true;
447function createCanvas() {
448 var c = document.createElement('canvas');
449 c.width = window.innerWidth;
450 c.height = window.innerHeight;
451 c.style.position = "absolute";
454 c.style.pointerEvents = "none";
458function assert(bool, msg) {
460 throw "Assertion error: " + msg;
464function snapshotSignalGraph(signalGraphNodes) {
467 signalGraphNodes.forEach(function(node) {
468 nodeValues.push({ value: node.value, id: node.id });
475function restoreSnapshot(signalGraphNodes, snapshot) {
476 assert(signalGraphNodes.length == snapshot.length, "saved program state has wrong length");
477 for (var i=0; i < signalGraphNodes.length; i++) {
478 var node = signalGraphNodes[i];
479 var state = snapshot[i];
480 assert(node.id == state.id, "the nodes moved position");
482 node.value = state.value;
486function flattenSignalGraph(nodes) {
489 function addAllToDict(node) {
490 nodesById[node.id] = node;
491 node.kids.forEach(addAllToDict);
493 nodes.forEach(addAllToDict);
495 var allNodes = Object.keys(nodesById).sort().map(function(key) {
496 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