Skip to content

Commit

Permalink
consistent modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jul 19, 2019
1 parent c7f2afc commit 8165998
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 145 deletions.
121 changes: 63 additions & 58 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
* should be fast, pure, functions that are cheap to call and easy to optimize
* through caching and memoization.
*/
function driver(data) {
return {
input: function input() {
// Return the stored data as input.
return data;
},
output: function output(dataNew) {
// Update the stored data when it is an output.
data = dataNew;
}
};
}

var data = {
driver: function driver(data) {
return {
input: function input() {
// Return the stored data as input.
return data;
},
output: function output(dataNew) {
// Update the stored data when it is an output.
data = dataNew;
}
};
}
driver: driver
};

/**
Expand Down Expand Up @@ -144,41 +146,29 @@
}
}

/**
* Cache for default property values
*/
var removeDataPropertyCache = {};
/**
* Old Node Constructor
*/

function NodeOld(node, element, children) {
this.node = node;
this.element = element;
this.children = children;
}

/**
* New Node Constructor
*/

function NodeNew(type, name, data, children) {
this.type = type;
this.name = name;
this.data = data;
this.children = children;
}

/**
* Returns a new node.
*
* @param {number} type
* @param {string} name
* @param {Object} data
* @param {Array} children
* Cache for default property values
*/

function m(type, name, data, children) {
return new NodeNew(type, name, data, children);
}
var removeDataPropertyCache = {};
/**
* Update an ariaset, dataset, or style property.
*
Expand Down Expand Up @@ -490,37 +480,52 @@
*/


function driver$1(root) {
// Accept query strings as well as DOM elements.
if (typeof root === "string") {
root = document.querySelector(root);
} // Capture old data from the root element's attributes.


var rootAttributes = root.attributes;
var dataOld = {};

for (var i = 0; i < rootAttributes.length; i++) {
var rootAttribute = rootAttributes[i];
dataOld[rootAttribute.name] = rootAttribute.value;
} // Create an old node from the root element.


var viewOld = new NodeOld(new NodeNew(types.element, root.tagName.toLowerCase(), dataOld, []), root, []);
return {
input: function input() {
// Return the current event data as input.
return viewEvent;
},
output: function output(viewNew) {
// When given a new view, patch the old view into the new one,
// updating the DOM in the process.
viewPatch(viewOld, viewNew);
}
};
}

/**
* Returns a new node.
*
* @param {number} type
* @param {string} name
* @param {Object} data
* @param {Array} children
*/

function m(type, name, data, children) {
return new NodeNew(type, name, data, children);
}

var view = {
m: m,
driver: function driver(root) {
// Accept query strings as well as DOM elements.
if (typeof root === "string") {
root = document.querySelector(root);
} // Capture old data from the root element's attributes.


var rootAttributes = root.attributes;
var dataOld = {};

for (var i = 0; i < rootAttributes.length; i++) {
var rootAttribute = rootAttributes[i];
dataOld[rootAttribute.name] = rootAttribute.value;
} // Create an old node from the root element.


var viewOld = new NodeOld(new NodeNew(types.element, root.tagName.toLowerCase(), dataOld, []), root, []);
return {
input: function input() {
// Return the current event data as input.
return viewEvent;
},
output: function output(viewNew) {
// When given a new view, patch the old view into the new one,
// updating the DOM in the process.
viewPatch(viewOld, viewNew);
}
};
}
driver: driver$1,
m: m
};

var index = {
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

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

26 changes: 12 additions & 14 deletions packages/moon/src/data/data.js → packages/moon/src/data/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
* should be fast, pure, functions that are cheap to call and easy to optimize
* through caching and memoization.
*/
export default {
driver(data) {
return {
input() {
// Return the stored data as input.
return data;
},
output(dataNew) {
// Update the stored data when it is an output.
data = dataNew;
}
};
}
};
export default function driver(data) {
return {
input() {
// Return the stored data as input.
return data;
},
output(dataNew) {
// Update the stored data when it is an output.
data = dataNew;
}
};
}
5 changes: 5 additions & 0 deletions packages/moon/src/data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import driver from "moon/src/data/driver";

export default {
driver
};
4 changes: 2 additions & 2 deletions packages/moon/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import data from "moon/src/data/data";
import view from "moon/src/view/view";
import data from "moon/src/data/index";
import view from "moon/src/view/index";
import { use } from "moon/src/use";
import run from "moon/src/run";

Expand Down
9 changes: 9 additions & 0 deletions packages/moon/src/view/NodeNew.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* New Node Constructor
*/
export default function NodeNew(type, name, data, children) {
this.type = type;
this.name = name;
this.data = data;
this.children = children;
}
8 changes: 8 additions & 0 deletions packages/moon/src/view/NodeOld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Old Node Constructor
*/
export default function NodeOld(node, element, children) {
this.node = node;
this.element = element;
this.children = children;
}
77 changes: 38 additions & 39 deletions packages/moon/src/view/view.js → packages/moon/src/view/driver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import run from "moon/src/run";
import { m, NodeNew, NodeOld, removeDataProperty, removeDataSet, updateDataSet } from "moon/src/view/util/util";
import NodeOld from "moon/src/view/NodeOld";
import NodeNew from "moon/src/view/NodeNew";
import { removeDataProperty, removeDataSet, updateDataSet } from "moon/src/view/util";
import { types } from "util/util";

/**
Expand Down Expand Up @@ -266,45 +268,42 @@ function viewPatch(nodeOld, nodeNew) {
* JavaScript engines, and immutability opens up the opportunity to use
* standard functional techniques for caching.
*/
export default {
m,
driver(root) {
// Accept query strings as well as DOM elements.
if (typeof root === "string") {
root = document.querySelector(root);
}
export default function driver(root) {
// Accept query strings as well as DOM elements.
if (typeof root === "string") {
root = document.querySelector(root);
}

// Capture old data from the root element's attributes.
const rootAttributes = root.attributes;
const dataOld = {};
// Capture old data from the root element's attributes.
const rootAttributes = root.attributes;
const dataOld = {};

for (let i = 0; i < rootAttributes.length; i++) {
const rootAttribute = rootAttributes[i];
dataOld[rootAttribute.name] = rootAttribute.value;
}
for (let i = 0; i < rootAttributes.length; i++) {
const rootAttribute = rootAttributes[i];
dataOld[rootAttribute.name] = rootAttribute.value;
}

// Create an old node from the root element.
const viewOld = new NodeOld(
new NodeNew(
types.element,
root.tagName.toLowerCase(),
dataOld,
[]
),
root,
// Create an old node from the root element.
const viewOld = new NodeOld(
new NodeNew(
types.element,
root.tagName.toLowerCase(),
dataOld,
[]
);

return {
input() {
// Return the current event data as input.
return viewEvent;
},
output(viewNew) {
// When given a new view, patch the old view into the new one,
// updating the DOM in the process.
viewPatch(viewOld, viewNew);
}
};
}
};
),
root,
[]
);

return {
input() {
// Return the current event data as input.
return viewEvent;
},
output(viewNew) {
// When given a new view, patch the old view into the new one,
// updating the DOM in the process.
viewPatch(viewOld, viewNew);
}
};
}
7 changes: 7 additions & 0 deletions packages/moon/src/view/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import driver from "moon/src/view/driver";
import m from "moon/src/view/m";

export default {
driver,
m
};
13 changes: 13 additions & 0 deletions packages/moon/src/view/m.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import NodeNew from "moon/src/view/NodeNew";

/**
* Returns a new node.
*
* @param {number} type
* @param {string} name
* @param {Object} data
* @param {Array} children
*/
export default function m(type, name, data, children) {
return new NodeNew(type, name, data, children);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,6 @@
*/
const removeDataPropertyCache = {};

/**
* Old Node Constructor
*/
export function NodeOld(node, element, children) {
this.node = node;
this.element = element;
this.children = children;
}

/**
* New Node Constructor
*/
export function NodeNew(type, name, data, children) {
this.type = type;
this.name = name;
this.data = data;
this.children = children;
}

/**
* Returns a new node.
*
* @param {number} type
* @param {string} name
* @param {Object} data
* @param {Array} children
*/
export function m(type, name, data, children) {
return new NodeNew(type, name, data, children);
}

/**
* Update an ariaset, dataset, or style property.
*
Expand Down

0 comments on commit 8165998

Please sign in to comment.