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.moduleInstance;
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;
44 function wrapNotify(id, v) {
45 var timestep = Date.now();
48 // ignore async events generated while playing back
49 // or user events while program is paused
53 var changed = runtime.notify(id, v, timestep);
54 recordEvent(id, v, timestep);
56 parent.window.postMessage("elmNotify", window.location.origin);
62 function wrapRunDelayed(func, delayMs) {
63 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
64 var timerId = setTimeout(function() {
65 cbObj.executed = true;
68 cbObj.timerId = timerId;
69 asyncCallbacks.push(cbObj);
73 function recordEvent(id, v, timestep) {
74 watchTracker.pushFrame();
75 recordedEvents.push({ id:id, value:v, timestep:timestep });
78 function clearAsyncCallbacks() {
79 asyncCallbacks.forEach(function(timer) {
80 if (!timer.executed) {
81 clearTimeout(timer.timerId);
86 function clearRecordedEvents() {
90 function getRecordedEventsLength() {
91 return recordedEvents.length;
94 function getRecordedEventAt(i) {
95 return recordedEvents[i];
98 function copyRecordedEvents() {
99 return recordedEvents.slice();
102 function loadRecordedEvents(events) {
103 recordedEvents = events.slice();
106 function setPaused(v) {
109 clearAsyncCallbacks();
110 tracePath.stopRecording();
113 // executeCallbacks should only be called when continuing, not when restarting.
114 //executeCallbacks(asyncCallbacks, false);
115 tracePath.startRecording();
119 function getPaused() {
120 return programPaused;
123 // runtime is the prototype of wrappedRuntime
124 // so we can access all runtime properties too
125 var wrappedRuntime = Object.create(runtime);
126 wrappedRuntime.notify = wrapNotify;
127 wrappedRuntime.runDelayed = wrapRunDelayed;
129 var assignedPropTracker = Object.create(wrappedRuntime);
130 var moduleInstance = module.make(assignedPropTracker);
132 // make sure the signal graph is actually a signal & extract the visual model
133 if ( !('recv' in moduleInstance.main) ) {
134 moduleInstance.main = Elm.Signal.make(runtime).constant(moduleInstance.main);
137 // The main module stores imported modules onto the runtime.
138 // To ensure only one instance of each module is created,
139 // we assign them back on the original runtime object.
140 Object.keys(assignedPropTracker).forEach(function(key) {
141 runtime[key] = assignedPropTracker[key];
144 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
145 var tracePath = tracePathInit(runtime, moduleInstance.main);
148 moduleInstance: moduleInstance,
149 moduleNodes: moduleNodes,
150 initialNodeValues: saveNodeValues(moduleNodes),
151 initialAsyncCallbacks: asyncCallbacks.slice(),
153 clearAsyncCallbacks: clearAsyncCallbacks,
154 clearRecordedEvents: clearRecordedEvents,
155 getRecordedEventsLength: getRecordedEventsLength,
156 getRecordedEventAt: getRecordedEventAt,
157 copyRecordedEvents: copyRecordedEvents,
158 loadRecordedEvents: loadRecordedEvents,
159 getPaused: getPaused,
160 setPaused: setPaused,
161 tracePath: tracePath,
162 watchTracker: watchTracker,
166function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
167 var eventCounter = 0;
169 function resetProgram() {
170 debugModule.clearAsyncCallbacks();
171 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
175 function restartProgram() {
177 debugModule.watchTracker.clear();
178 debugModule.tracePath.clearTraces();
179 debugModule.clearRecordedEvents();
180 debugModule.setPaused(false);
181 executeCallbacks(debugModule.initialAsyncCallbacks, true);
184 function pauseProgram() {
185 debugModule.setPaused(true);
186 eventCounter = debugModule.getRecordedEventsLength();
189 function continueProgram() {
190 if (debugModule.getPaused())
192 debugModule.setPaused(false);
196 function stepTo(index) {
197 if (!debugModule.getPaused()) {
198 debugModule.setPaused(true);
202 if (index < 0 || index > getMaxSteps()) {
203 throw "Index out of bounds: " + index;
206 if (index < eventCounter) {
211 assert(index >= eventCounter, "index must be bad");
212 while (eventCounter < index) {
213 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
214 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
218 assert(eventCounter == index, "while loop didn't work");
221 function getMaxSteps() {
222 return debugModule.getRecordedEventsLength();
225 function doPlayback(eventList) {
226 var x = eventList.shift();
228 runtime.notify(x[0], x[1], x[2]);
230 if (eventList.length > 0) {
231 var delta = eventList[0][2] - time;
232 setTimeout(function() { doPlayback(eventList); }, delta);
236 function redrawGraphics() {
237 var main = debugModule.moduleInstance.main
238 for (var i = main.kids.length ; i-- ; ) {
239 main.kids[i].recv(Date.now(), true, main.id);
243 function getHotSwapState() {
244 var counter = eventCounter;
245 if (!debugModule.getPaused()) {
246 counter = getMaxSteps();
249 recordedEvents: debugModule.copyRecordedEvents(),
250 eventCounter: counter
255 var parentNode = runtime.node.parentNode;
256 parentNode.removeChild(debugModule.tracePath.canvas);
257 parentNode.removeChild(runtime.node);
261 debugModule.setPaused(true);
262 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
264 // draw new trace path
265 debugModule.tracePath.startRecording();
266 stepTo(getMaxSteps());
267 debugModule.tracePath.stopRecording();
269 stepTo(hotSwapState.eventCounter);
272 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
275 restart: restartProgram,
277 kontinue: continueProgram,
278 getMaxSteps: getMaxSteps,
280 getPaused: debugModule.getPaused,
281 getHotSwapState: getHotSwapState,
283 allNodes: debugModule.moduleNodes,
284 watchTracker: debugModule.watchTracker,
285 mainNode: debugModule.moduleInstance.main
291function Point(x, y) {
295 this.translate = function(x, y) {
296 return new Point(this.x + x, this.y + y);
299 this.equals = function(p) {
300 return this.x == p.x && this.y == p.y;
304function tracePathInit(runtime, mainNode) {
305 var List = Elm.List.make(runtime);
306 var Signal = Elm.Signal.make(runtime);
307 var tracePathNode = A2(Signal.lift, graphicsUpdate, mainNode);
308 var tracePathCanvas = createCanvas();
309 var tracePositions = {};
310 var recordingTraces = true;
312 function findPositions(currentScene) {
314 function processElement(elem, offset) {
315 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
317 List.map(F2(processForm)(offset))(elem.element.model.forms);
319 if (elem.props.debugTracePathId)
321 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
325 function processForm(offset, form) {
326 if (form.form.ctor == "FElement")
328 processElement(form.form._0, offset.translate(form.x, -form.y));
332 processElement(currentScene, new Point(0, 0));
336 function appendPositions(positions) {
337 for (var id in positions) {
338 var pos = positions[id];
339 if (tracePositions.hasOwnProperty(id)) {
340 var points = tracePositions[id];
341 if (!pos.equals(points[points.length-1])) {
346 tracePositions[id] = [pos];
351 function graphicsUpdate(currentScene) {
352 if (!recordingTraces) {
356 var ctx = tracePathCanvas.getContext('2d');
357 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
360 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
361 appendPositions(findPositions(currentScene));
362 for (var id in tracePositions)
365 var points = tracePositions[id];
366 for (var i=0; i < points.length; i++)
370 ctx.moveTo(p.x, p.y);
373 ctx.lineTo(p.x, p.y);
377 ctx.strokeStyle = "rgba(50, 50, 50, 0.4)";
380 for (var i=0; i < points.length; i++)
384 ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
385 ctx.fillStyle = "rgba(50, 50, 50, 0.4)";
393 function clearTraces() {
397 function stopRecording() {
398 recordingTraces = false;
401 function startRecording() {
402 recordingTraces = true;
406 graphicsUpdate: graphicsUpdate,
407 canvas: tracePathCanvas,
408 clearTraces: clearTraces,
409 stopRecording: stopRecording,
410 startRecording: startRecording
415function executeCallbacks(callbacks, reexecute) {
416 callbacks.forEach(function(timer) {
417 if (reexecute || !timer.executed) {
418 var func = timer.func;
424function createCanvas() {
425 var c = document.createElement('canvas');
426 c.width = window.innerWidth;
427 c.height = window.innerHeight;
428 c.style.position = "absolute";
431 c.style.pointerEvents = "none";
435function assert(bool, msg) {
437 throw "Assertion error: " + msg;
441function saveNodeValues(allNodes) {
444 allNodes.forEach(function(node) {
445 nodeValues.push({ value: node.value, id: node.id });
452function restoreNodeValues(allNodes, nodeStates) {
453 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
454 for (var i=0; i < allNodes.length; i++) {
455 var node = allNodes[i];
456 var state = nodeStates[i];
457 assert(node.id == state.id, "the nodes moved position");
459 node.value = state.value;
463function flattenNodes(nodes) {
466 function addAllToDict(node) {
467 nodesById[node.id] = node;
468 node.kids.forEach(addAllToDict);
470 nodes.forEach(addAllToDict);
472 var allNodes = Object.keys(nodesById).sort().map(function(key) {
473 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