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 /* =undefined */) {
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 /* =undefined */) {
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 assert(index >= currentEventIndex, "index must be bad");
236 while (currentEventIndex < index) {
237 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
238 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
240 currentEventIndex += 1;
242 assert(currentEventIndex == index, "while loop didn't work");
245 function getMaxSteps() {
246 return debugModule.getRecordedEventsLength();
249 function doPlayback(eventList) {
250 var x = eventList.shift();
252 runtime.notify(x[0], x[1], x[2]);
254 if (eventList.length > 0) {
255 var delta = eventList[0][2] - time;
256 setTimeout(function() { doPlayback(eventList); }, delta);
260 function redrawGraphics() {
261 var main = debugModule.debuggedModule.main
262 for (var i = main.kids.length ; i-- ; ) {
263 main.kids[i].recv(runtime.timer.now(), true, main.id);
267 function getHotSwapState() {
268 var continueIndex = currentEventIndex;
269 if (!debugModule.getPaused()) {
270 continueIndex = getMaxSteps();
273 recordedEvents: debugModule.copyRecordedEvents(),
274 currentEventIndex: continueIndex
279 var parentNode = runtime.node.parentNode;
280 parentNode.removeChild(debugModule.tracePath.canvas);
281 parentNode.removeChild(runtime.node);
285 // The problem is that we want to previous paused state. But
286 // by the time JS reaches here, the old code has been swapped out
287 // and the new modules are being generated. So we can ask the
288 // debugging console what it thinks the pause state is and go
290 var paused = top.debug.paused;
291 debugModule.setPaused();
292 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
294 // draw new trace path
295 debugModule.tracePath.startRecording();
296 stepTo(getMaxSteps());
297 debugModule.tracePath.stopRecording();
299 stepTo(hotSwapState.currentEventIndex);
301 debugModule.setPaused();
303 debugModule.setContinue(hotSwapState.currentEventIndex);
307 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
310 restart: restartProgram,
312 kontinue: continueProgram,
313 getMaxSteps: getMaxSteps,
315 getPaused: debugModule.getPaused,
316 getHotSwapState: getHotSwapState,
318 allNodes: debugModule.signalGraphNodes,
319 watchTracker: debugModule.watchTracker,
320 signalGraphMain: debugModule.debuggedModule.main
326function Point(x, y) {
330 this.translate = function(x, y) {
331 return new Point(this.x + x, this.y + y);
334 this.equals = function(p) {
335 return this.x == p.x && this.y == p.y;
339function tracePathInit(runtime, signalGraphMain) {
340 var List = Elm.List.make(runtime);
341 var Signal = Elm.Signal.make(runtime);
342 var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain);
343 var tracePathCanvas = createCanvas();
344 var tracePositions = {};
345 var recordingTraces = true;
347 function findPositions(currentScene) {
349 function processElement(elem, offset) {
350 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
352 List.map(F2(processForm)(offset))(elem.element.model.forms);
354 if (elem.props.debugTracePathId)
356 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
360 function processForm(offset, form) {
361 if (form.form.ctor == "FElement")
363 processElement(form.form._0, offset.translate(form.x, -form.y));
367 processElement(currentScene, new Point(0, 0));
371 function appendPositions(positions) {
372 for (var id in positions) {
373 var pos = positions[id];
374 if (tracePositions.hasOwnProperty(id)) {
375 var points = tracePositions[id];
376 if (!pos.equals(points[points.length-1])) {
381 tracePositions[id] = [pos];
386 function graphicsUpdate(currentScene) {
387 if (!recordingTraces) {
391 var ctx = tracePathCanvas.getContext('2d');
392 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
395 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
396 appendPositions(findPositions(currentScene));
397 for (var id in tracePositions)
400 var points = tracePositions[id];
401 for (var i=0; i < points.length; i++)
405 ctx.moveTo(p.x, p.y);
408 ctx.lineTo(p.x, p.y);
412 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
415 for (var i=0; i < points.length; i++)
419 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
420 ctx.fillStyle = "rgba(50, 50, 50, 0.4)";
428 function clearTraces() {
432 function stopRecording() {
433 recordingTraces = false;
436 function startRecording() {
437 recordingTraces = true;
441 graphicsUpdate: graphicsUpdate,
442 canvas: tracePathCanvas,
443 clearTraces: clearTraces,
444 stopRecording: stopRecording,
445 startRecording: startRecording
450function executeCallbacks(callbacks, reexecute) {
451 callbacks.forEach(function(timer) {
452 if (reexecute || !timer.executed) {
453 var func = timer.func;
454 timer.executed = true;
460function createCanvas() {
461 var c = document.createElement('canvas');
462 c.width = window.innerWidth;
463 c.height = window.innerHeight;
464 c.style.position = "absolute";
467 c.style.pointerEvents = "none";
471function assert(bool, msg) {
473 throw "Assertion error: " + msg;
477function snapshotSignalGraph(signalGraphNodes) {
480 signalGraphNodes.forEach(function(node) {
481 nodeValues.push({ value: node.value, id: node.id });
488function restoreSnapshot(signalGraphNodes, snapshot) {
489 assert(signalGraphNodes.length == snapshot.length, "saved program state has wrong length");
490 for (var i=0; i < signalGraphNodes.length; i++) {
491 var node = signalGraphNodes[i];
492 var state = snapshot[i];
493 assert(node.id == state.id, "the nodes moved position");
495 node.value = state.value;
499function flattenSignalGraph(nodes) {
502 function addAllToDict(node) {
503 nodesById[node.id] = node;
504 node.kids.forEach(addAllToDict);
506 nodes.forEach(addAllToDict);
508 var allNodes = Object.keys(nodesById).sort().map(function(key) {
509 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