Skip to content

Commit

Permalink
add update
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 29, 2018
1 parent 6def4af commit 463b553
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
32 changes: 24 additions & 8 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
var parse = function (input) {
var length = input.length;
var dependencies = [];
var locals = ["NaN", "false", "in", "instance", "m", "null", "true", "typeof", "undefined"];
var locals = ["NaN", "false", "in", "m", "null", "true", "typeof", "undefined"];
parseIndex = 0;

var root = {
Expand Down Expand Up @@ -206,16 +206,21 @@
return root;
};

var mapReduce = function (arr, fn) { return arr.reduce(function (result, current) { return result + fn(current); }, ""); };

var generateCreate = function (element) {
switch (element.type) {
case "m-fragment":
return element.children.map(generateCreate).join("");
return mapReduce(element.children, generateCreate);
break;
case "m-expression":
return ("m[" + (element.index) + "] = document.createTextNode(\"\");");
break;
case "m-text":
return ("m[" + (element.index) + "] = document.createTextNode(\"" + (element.content) + "\");");
break;
default:
return element.children.map(generateCreate).join("") + "m[" + (element.index) + "] = document.createElement(\"" + (element.type) + "\");";
return ((mapReduce(element.children, generateCreate)) + "m[" + (element.index) + "] = document.createElement(\"" + (element.type) + "\");");
}
};

Expand All @@ -231,7 +236,7 @@
var childPath = "m[" + (child.index) + "]";

if (child.type !== "m-text") {
generatedMount += child.children.map(function (grandchild) { return generateMount(grandchild, childPath); }).join("");
generatedMount += mapReduce(child.children, function (grandchild) { return generateMount(grandchild, childPath); });
}

generatedMount += parent + ".parentNode.insertBefore(" + childPath + ", " + parent + ");";
Expand All @@ -242,8 +247,8 @@
default:
var elementPath = "m[" + (element.index) + "]";

if (element.type !== "m-text") {
generatedMount += element.children.map(function (child) { return generateMount(child, elementPath); }).join("");
if (element.type !== "m-text" && element.type !== "m-expression") {
generatedMount += mapReduce(element.children, function (child) { return generateMount(child, elementPath); });
}

generatedMount += parent + ".appendChild(" + elementPath + ");";
Expand All @@ -252,10 +257,21 @@
return generatedMount;
};

var generateUpdate = function () {};
var generateUpdate = function (element) {
switch (element.type) {
case "m-expression":
return ("m[" + (element.index) + "].textContent = " + (element.content) + ";");
break;
case "m-text":
return "";
break;
default:
return mapReduce(element.children, generateUpdate);
}
};

var generate = function (tree) {
return new Function(("return [function () {var m = this.m;" + (generateCreate(tree)) + "}, function (root) {var m = this.m;" + (generateMount(tree, "root")) + "}, function () {var m = this.m;" + (generateUpdate(tree)) + "}]"))();
return new Function(("return [function () {var m = this.m;" + (generateCreate(tree)) + "}, function (root) {var m = this.m;" + (generateMount(tree, "root")) + "}, function () {var m = this.m;" + (tree.dependencies.map(function (dependency) { return ("var " + dependency + " = this." + dependency + ";"); })) + (generateUpdate(tree)) + "}]"))();
};

var compile = function (input) {
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions src/compiler/generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const mapReduce = (arr, fn) => arr.reduce((result, current) => result + fn(current), "");

const generateCreate = (element) => {
switch (element.type) {
case "m-fragment":
return element.children.map(generateCreate).join("");
return mapReduce(element.children, generateCreate);
break;
case "m-expression":
return `m[${element.index}] = document.createTextNode("");`;
Expand All @@ -10,7 +12,7 @@ const generateCreate = (element) => {
return `m[${element.index}] = document.createTextNode("${element.content}");`;
break;
default:
return element.children.map(generateCreate).join("") + `m[${element.index}] = document.createElement("${element.type}");`;
return `${mapReduce(element.children, generateCreate)}m[${element.index}] = document.createElement("${element.type}");`;
}
};

Expand All @@ -26,7 +28,7 @@ const generateMount = (element, parent) => {
const childPath = `m[${child.index}]`;

if (child.type !== "m-text") {
generatedMount += child.children.map((grandchild) => generateMount(grandchild, childPath)).join("");
generatedMount += mapReduce(child.children, (grandchild) => generateMount(grandchild, childPath));
}

generatedMount += `${parent}.parentNode.insertBefore(${childPath}, ${parent});`;
Expand All @@ -36,7 +38,7 @@ const generateMount = (element, parent) => {
const elementPath = `m[${element.index}]`;

if (element.type !== "m-text" && element.type !== "m-expression") {
generatedMount += element.children.map((child) => generateMount(child, elementPath)).join("");
generatedMount += mapReduce(element.children, (child) => generateMount(child, elementPath));
}

generatedMount += `${parent}.appendChild(${elementPath});`;
Expand All @@ -45,7 +47,18 @@ const generateMount = (element, parent) => {
return generatedMount;
};

const generateUpdate = () => {};
const generateUpdate = (element) => {
switch (element.type) {
case "m-expression":
return `m[${element.index}].textContent = ${element.content};`;
break;
case "m-text":
return "";
break;
default:
return mapReduce(element.children, generateUpdate);
}
};

export const generate = (tree) => {
return new Function(`return [function () {var m = this.m;${generateCreate(tree)}}, function (root) {var m = this.m;${generateMount(tree, "root")}}, function () {var m = this.m;${tree.dependencies.map((dependency) => `var ${dependency} = this.${dependency};`)}${generateUpdate(tree)}}]`)();
Expand Down

0 comments on commit 463b553

Please sign in to comment.