(function() { 'use strict'; if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? (':' + window.location.port) : ''); } Elm.Debugger = null; Elm.debuggerAttach = function(module, hotSwapState) { return { make: function(runtime) { var wrappedModule = debugModule(module, runtime); Elm.Debugger = debuggerInit(wrappedModule, runtime, hotSwapState); dispatchElmDebuggerInit(); return wrappedModule.debuggedModule; } }; }; function dispatchElmDebuggerInit() { if (parent.window) { var dispatch = function() { parent.window.postMessage("elmDebuggerInit", window.location.origin); }; if (parent.window.document.readyState === "complete") { dispatch(); } else { parent.window.addEventListener("load", dispatch); } } } function debugModule(module, runtime) { var programPaused = false; var recordedEvents = []; var asyncCallbacks = []; var watchTracker = Elm.Native.Debug.make(runtime).watchTracker; var pauseTime = 0; function wrapNotify(id, v) { var timestep = runtime.timer.now(); if (programPaused) { // ignore async events generated while playing back // or user events while program is paused return false; } else { var changed = runtime.notify(id, v, timestep); recordEvent(id, v, timestep); if (parent.window) { parent.window.postMessage("elmNotify", window.location.origin); } return changed; } }; function wrapRunDelayed(func, delayMs) { if (programPaused) { // Don't push timers and such to the callback stack while we're paused. // It causes too many callbacks to be fired during unpausing. return 0; } var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false }; var timerId = setTimeout(function() { cbObj.executed = true; func(); }, delayMs); cbObj.timerId = timerId; asyncCallbacks.push(cbObj); return timerId; } function recordEvent(id, v, timestep) { watchTracker.pushFrame(); recordedEvents.push({ id:id, value:v, timestep:timestep }); } function clearAsyncCallbacks() { asyncCallbacks.forEach(function(timer) { if (!timer.executed) { clearTimeout(timer.timerId); } }); } function clearRecordedEvents() { recordedEvents = []; } function getRecordedEventsLength() { return recordedEvents.length; } function getRecordedEventAt(i) { return recordedEvents[i]; } function copyRecordedEvents() { return recordedEvents.slice(); } function loadRecordedEvents(events) { recordedEvents = events.slice(); } function setPaused() { programPaused = true; clearAsyncCallbacks(); tracePath.stopRecording(); pauseTime = Date.now(); } function setContinue(position) { var timerDelay = Date.now() - pauseTime; runtime.timer.addDelay(timerDelay); programPaused = false; if (position > 0) { // If we're not unpausing at the head, then we need to dump the // events that are ahead of where we're continuing. recordedEvents = recordedEvents.slice(0, position); executeCallbacks(asyncCallbacks, false); } tracePath.startRecording(); } function getPaused() { return programPaused; } // runtime is the prototype of wrappedRuntime // so we can access all runtime properties too var wrappedRuntime = Object.create(runtime); wrappedRuntime.notify = wrapNotify; wrappedRuntime.runDelayed = wrapRunDelayed; var assignedPropTracker = Object.create(wrappedRuntime); var debuggedModule = module.make(assignedPropTracker); // make sure the signal graph is actually a signal & extract the visual model if ( !('recv' in debuggedModule.main) ) { debuggedModule.main = Elm.Signal.make(runtime).constant(debuggedModule.main); } // The main module stores imported modules onto the runtime. // To ensure only one instance of each module is created, // we assign them back on the original runtime object. Object.keys(assignedPropTracker).forEach(function(key) { runtime[key] = assignedPropTracker[key]; }); var signalGraphNodes = flattenSignalGraph(wrappedRuntime.inputs); var tracePath = tracePathInit(runtime, debuggedModule.main); return { debuggedModule: debuggedModule, signalGraphNodes: signalGraphNodes, initialSnapshot: snapshotSignalGraph(signalGraphNodes), initialAsyncCallbacks: asyncCallbacks.slice(), // API functions clearAsyncCallbacks: clearAsyncCallbacks, clearRecordedEvents: clearRecordedEvents, getRecordedEventsLength: getRecordedEventsLength, getRecordedEventAt: getRecordedEventAt, copyRecordedEvents: copyRecordedEvents, loadRecordedEvents: loadRecordedEvents, getPaused: getPaused, setPaused: setPaused, setContinue: setContinue, tracePath: tracePath, watchTracker: watchTracker, }; } function debuggerInit(debugModule, runtime, hotSwapState) { var currentEventIndex = 0; function resetProgram() { debugModule.clearAsyncCallbacks(); restoreSnapshot(debugModule.signalGraphNodes, debugModule.initialSnapshot); redrawGraphics(); } function restartProgram() { resetProgram(); debugModule.watchTracker.clear(); debugModule.tracePath.clearTraces(); debugModule.clearRecordedEvents(); debugModule.setContinue(0); executeCallbacks(debugModule.initialAsyncCallbacks, true); } function pauseProgram() { debugModule.setPaused(); currentEventIndex = debugModule.getRecordedEventsLength(); } function continueProgram() { if (debugModule.getPaused()) { if (currentEventIndex === 0) { restartProgram(); return; } resetProgram(); var continueIndex = currentEventIndex; currentEventIndex = 0; debugModule.tracePath.clearTraces(); debugModule.tracePath.startRecording(); stepTo(continueIndex); debugModule.setContinue(currentEventIndex); } } function stepTo(index) { if (!debugModule.getPaused()) { debugModule.setPaused(); resetProgram(); } if (index < 0 || index > getMaxSteps()) { throw "Index out of bounds: " + index; } if (index < currentEventIndex) { resetProgram(); currentEventIndex = 0; } while (currentEventIndex < index) { var nextEvent = debugModule.getRecordedEventAt(currentEventIndex); runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep); currentEventIndex += 1; } } function getMaxSteps() { return debugModule.getRecordedEventsLength(); } function redrawGraphics() { var main = debugModule.debuggedModule.main for (var i = main.kids.length ; i-- ; ) { main.kids[i].recv(runtime.timer.now(), true, main.id); } } function getHotSwapState() { var continueIndex = currentEventIndex; if (!debugModule.getPaused()) { continueIndex = getMaxSteps(); } return { recordedEvents: debugModule.copyRecordedEvents(), currentEventIndex: continueIndex }; } function dispose() { var parentNode = runtime.node.parentNode; parentNode.removeChild(debugModule.tracePath.canvas); parentNode.removeChild(runtime.node); } if (hotSwapState) { // The problem is that we want to previous paused state. But // by the time JS reaches here, the old code has been swapped out // and the new modules are being generated. So we can ask the // debugging console what it thinks the pause state is and go // from there. var paused = top.debug.paused; debugModule.setPaused(); debugModule.loadRecordedEvents(hotSwapState.recordedEvents); // draw new trace path debugModule.tracePath.startRecording(); stepTo(getMaxSteps()); debugModule.tracePath.stopRecording(); stepTo(hotSwapState.currentEventIndex); if (paused) { debugModule.setPaused(); } else { debugModule.setContinue(hotSwapState.currentEventIndex); } } runtime.node.parentNode.appendChild(debugModule.tracePath.canvas); var elmDebugger = { restart: restartProgram, pause: pauseProgram, kontinue: continueProgram, getMaxSteps: getMaxSteps, stepTo: stepTo, getPaused: debugModule.getPaused, getHotSwapState: getHotSwapState, dispose: dispose, allNodes: debugModule.signalGraphNodes, watchTracker: debugModule.watchTracker, signalGraphMain: debugModule.debuggedModule.main }; return elmDebugger; } function Point(x, y) { this.x = x; this.y = y; this.translate = function(x, y) { return new Point(this.x + x, this.y + y); } this.equals = function(p) { return this.x == p.x && this.y == p.y; } } function tracePathInit(runtime, signalGraphMain) { var List = Elm.List.make(runtime); var Signal = Elm.Signal.make(runtime); var tracePathNode = A2(Signal.lift, graphicsUpdate, signalGraphMain); var tracePathCanvas = createCanvas(); var tracePositions = {}; var recordingTraces = true; function findPositions(currentScene) { var positions = {}; function processElement(elem, offset) { if (elem.element.ctor == "Custom" && elem.element.type == "Collage") { List.map(F2(processForm)(offset))(elem.element.model.forms); } if (elem.props.debugTracePathId) { positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y); } } function processForm(offset, form) { if (form.form.ctor == "FElement") { processElement(form.form._0, offset.translate(form.x, -form.y)); } } processElement(currentScene, new Point(0, 0)); return positions; } function appendPositions(positions) { for (var id in positions) { var pos = positions[id]; if (tracePositions.hasOwnProperty(id)) { var points = tracePositions[id]; if (!pos.equals(points[points.length-1])) { points.push(pos); } } else { tracePositions[id] = [pos]; } } } function graphicsUpdate(currentScene) { if (!recordingTraces) { return; } var ctx = tracePathCanvas.getContext('2d'); ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height); ctx.save(); ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2); appendPositions(findPositions(currentScene)); for (var id in tracePositions) { ctx.beginPath(); var points = tracePositions[id]; for (var i=0; i < points.length; i++) { var p = points[i]; if (i == 0) { ctx.moveTo(p.x, p.y); } else { ctx.lineTo(p.x, p.y); } } ctx.lineWidth = 1; ctx.strokeStyle = "rgba(50, 50, 50, 0.4)"; ctx.stroke(); for (var i=0; i < points.length; i++) { var p = points[i]; ctx.beginPath(); ctx.arc(p.x, p.y, 2, 0, Math.PI * 2); ctx.fillStyle = "rgba(50, 50, 50, 0.4)"; ctx.fill(); } } ctx.restore(); } function clearTraces() { tracePositions = {}; } function stopRecording() { recordingTraces = false; } function startRecording() { recordingTraces = true; } return { graphicsUpdate: graphicsUpdate, canvas: tracePathCanvas, clearTraces: clearTraces, stopRecording: stopRecording, startRecording: startRecording } } function executeCallbacks(callbacks, reexecute) { callbacks.forEach(function(timer) { if (reexecute || !timer.executed) { var func = timer.func; timer.executed = true; func(); } }); } function createCanvas() { var c = document.createElement('canvas'); c.width = window.innerWidth; c.height = window.innerHeight; c.style.position = "absolute"; c.style.top = "0"; c.style.left = "0"; c.style.pointerEvents = "none"; return c; } function assert(bool, msg) { if (!bool) { throw "Assertion error: " + msg; } } function snapshotSignalGraph(signalGraphNodes) { var nodeValues = []; signalGraphNodes.forEach(function(node) { nodeValues.push({ value: node.value, id: node.id }); }); return nodeValues; }; function restoreSnapshot(signalGraphNodes, snapshot) { assert(signalGraphNodes.length == snapshot.length, "saved program state has wrong length"); for (var i=0; i < signalGraphNodes.length; i++) { var node = signalGraphNodes[i]; var state = snapshot[i]; assert(node.id == state.id, "the nodes moved position"); node.value = state.value; } } function flattenSignalGraph(nodes) { var nodesById = {}; function addAllToDict(node) { nodesById[node.id] = node; node.kids.forEach(addAllToDict); } nodes.forEach(addAllToDict); var allNodes = Object.keys(nodesById).sort().map(function(key) { return nodesById[key]; }); return allNodes; }; }());