Skip to content

Instantly share code, notes, and snippets.

@ondras
Last active April 4, 2016 12:55
Show Gist options
  • Save ondras/0eb8ca8c599a4dc411d115aabc2b0606 to your computer and use it in GitHub Desktop.
Save ondras/0eb8ca8c599a4dc411d115aabc2b0606 to your computer and use it in GitHub Desktop.
Elm graphviz
// javascript:document.body.appendChild(document.createElement("script")).src="http://bit.ly/1RT8h3o";undefined
function trimDeadNodes(node) {
if (node.isOutput) { return true; }
var liveKids = [];
for (var i = node.kids.length; i--; ) {
var kid = node.kids[i];
if (trimDeadNodes(kid)) { liveKids.push(kid); }
}
node.kids = liveKids;
return liveKids.length > 0;
}
function printGraph(queue) {
var str = "";
queue = queue.slice(0);
str += 'digraph { \n';
var seen = [];
while (queue.length > 0) {
var node = queue.pop();
var id = node.id;
if (seen.indexOf(id) < 0) {
str += id + ' [label="' + node.name + '"];\n';
var kids = node.kids || [];
for (var i = kids.length; i--; ) {
str += id + ' -> ' + kids[i].id + ';\n';
}
queue = queue.concat(kids);
seen.push(id);
}
}
str += '}\n';
console.log(str);
return str;
}
if (!window.oldMake) {
window.oldMake = Elm.Main.make;
}
Elm.Main.make = function(elm) {
var result = oldMake.apply(this, arguments);
setTimeout(function() {
var root = {kids:elm.inputs};
trimDeadNodes(root);
var dot = printGraph(root.kids);
window.open("https://stamm-wilbrandt.de/GraphvizFiddle/#" + encodeURIComponent(dot));
}, 100);
return result;
}
Elm.fullscreen(Elm.Main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment