Skip to content

Commit

Permalink
refactor(request): improve request plugin. Add more tests for request…
Browse files Browse the repository at this point in the history
… plugin
  • Loading branch information
Masquerade-Circus committed Oct 3, 2020
1 parent d518c22 commit 5f9c4ab
Show file tree
Hide file tree
Showing 11 changed files with 597 additions and 463 deletions.
12 changes: 3 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
"no-with": 2,
"semi-spacing": 2,
"space-before-blocks": 2,
"space-before-function-paren": [
2,
{ "anonymous": "always", "named": "never" }
],
"space-before-function-paren": [0, { "anonymous": "always", "named": "always" }],
"space-in-parens": 0,
"space-infix-ops": 2,
"space-unary-ops": 2,
Expand Down Expand Up @@ -59,11 +56,8 @@
"sonarjs/no-duplicate-string": 0,
"sonarjs/prefer-object-literal": 0,
"sonarjs/no-identical-functions": 0,
"max-lines-per-function": [
"warn",
{ "max": 300, "skipBlankLines": true, "skipComments": true }
],
"max-len": ["error", 165],
"max-lines-per-function": ["warn", { "max": 300, "skipBlankLines": true, "skipComments": true }],
"max-len": ["error", 220],
"max-statements-per-line": ["error", { "max": 3 }]
},
"env": {
Expand Down
6 changes: 3 additions & 3 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"gzipped": 2075
},
"valyrian.min.js": {
"bundled": 13481,
"minified": 5030,
"gzipped": 2068
"bundled": 12907,
"minified": 5021,
"gzipped": 2077
}
}
2 changes: 1 addition & 1 deletion dist/valyrian.min.js

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

108 changes: 22 additions & 86 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function valyrian() {
let oldMainNode;
let mountedComponent;
let directives = {};
let mainContainer;

function Vnode(name, props, children) {
this.props = props || {};
Expand All @@ -30,9 +31,7 @@ function valyrian() {
let emptyNode = new TextVnode();

function createElement(tag, isSVG) {
return isSVG
? document.createElementNS("http://www.w3.org/2000/svg", tag)
: document.createElement(tag);
return isSVG ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag);
}

function lifecycleCall(vnode, methodName, oldNode) {
Expand Down Expand Up @@ -65,10 +64,7 @@ function valyrian() {

if (dom.nodeType === 1) {
let props = {};
[].forEach.call(
dom.attributes,
(prop) => (props[prop.nodeName] = prop.nodeValue)
);
[].forEach.call(dom.attributes, (prop) => (props[prop.nodeName] = prop.nodeValue));

let vnode = new Vnode(dom.nodeName, props, []);
vnode.dom = dom;
Expand All @@ -90,18 +86,9 @@ function valyrian() {

// Plugin system
let plugins = new Map();
v.usePlugin = (plugin, options) =>
!plugins.has(plugin) && plugins.set(plugin, true) && plugin(v, options);

v.reservedWords = [
"key",
once,
oncreate,
onbeforeupdate,
onupdate,
onremove,
"data"
];
v.usePlugin = (plugin, options) => !plugins.has(plugin) && plugins.set(plugin, true) && plugin(v, options);

v.reservedWords = ["key", once, oncreate, onbeforeupdate, onupdate, onremove, "data"];

let attachedListeners = {};
function eventListener(e) {
Expand Down Expand Up @@ -129,7 +116,7 @@ function valyrian() {
} else if (typeof value === functionstr) {
name = `__${name}`;
if (!attachedListeners[name]) {
document.addEventListener(name.slice(4), eventListener);
mainContainer.addEventListener(name.slice(4), eventListener);
attachedListeners[name] = true;
}
newNode.dom[name] = value;
Expand All @@ -151,11 +138,7 @@ function valyrian() {

function removeProps(newNode, oldNode) {
for (let name in oldNode.props) {
if (
v.reservedWords.indexOf(name) === -1 &&
name in newNode.props === false &&
typeof oldNode.props[name] !== functionstr
) {
if (v.reservedWords.indexOf(name) === -1 && name in newNode.props === false && typeof oldNode.props[name] !== functionstr) {
if (name in newNode.dom) {
newNode.dom[name] = UND;
} else {
Expand All @@ -173,20 +156,15 @@ function valyrian() {

function removeVnode(vnode) {
callRemove(vnode);
vnode.dom &&
vnode.dom.parentNode &&
vnode.dom.parentNode.removeChild(vnode.dom);
vnode.dom && vnode.dom.parentNode && vnode.dom.parentNode.removeChild(vnode.dom);
}

function updateKeyedNode($parent, newNode, compareNode, newIndex) {
let oldDom = $parent.childNodes[newIndex];
// Moved or updated
if (compareNode.dom) {
newNode.dom = compareNode.dom;
if (
newNode.props[once] ||
lifecycleCall(newNode, onbeforeupdate, compareNode) === false
) {
if (newNode.props[once] || lifecycleCall(newNode, onbeforeupdate, compareNode) === false) {
newNode.children = compareNode.children;
moveDom(newNode.dom, $parent, oldDom);
} else {
Expand Down Expand Up @@ -237,9 +215,7 @@ function valyrian() {

// eslint-disable-next-line complexity,sonarjs/cognitive-complexity
function patch(parentNode, oldParentNode) {
let newTree = isArray(parentNode.children)
? parentNode.children
: [parentNode.children];
let newTree = isArray(parentNode.children) ? parentNode.children : [parentNode.children];
let oldTree = oldParentNode.children;
v.current.parentVnode = parentNode;
v.current.oldParentVnode = oldParentNode;
Expand All @@ -255,17 +231,7 @@ function valyrian() {
childVnode.isSVG = parentNode.isSVG || childVnode.name === "svg";
} else {
v.current.component = childVnode;
newTree.splice(
i--,
1,
...[
(childVnode.name.view || childVnode.name).call(
childVnode.name,
childVnode.props,
...childVnode.children
)
]
);
newTree.splice(i--, 1, ...[(childVnode.name.view || childVnode.name).call(childVnode.name, childVnode.props, ...childVnode.children)]);
}
} else if (childVnode === null || childVnode === UND) {
newTree.splice(i--, 1);
Expand All @@ -282,11 +248,7 @@ function valyrian() {
}

// Is keyed list
} else if (
oldTree.length &&
newTree[0] instanceof Vnode &&
newTree[0].props.key
) {
} else if (oldTree.length && newTree[0] instanceof Vnode && newTree[0].props.key) {
let oldKeys = oldTree.map((vnode) => vnode.props.key);
let newKeys = newTree.map((vnode) => vnode.props.key);

Expand All @@ -305,12 +267,7 @@ function valyrian() {
// Moved: Key exists in old keys
if (oldIndex !== -1) {
oldTree[oldIndex].processed = true;
updateKeyedNode(
parentNode.dom,
newNode,
oldTree[oldIndex],
newIndex
);
updateKeyedNode(parentNode.dom, newNode, oldTree[oldIndex], newIndex);
// Added: Key does not exists in old keys
} else {
updateKeyedNode(parentNode.dom, newNode, emptyNode, newIndex);
Expand Down Expand Up @@ -349,29 +306,19 @@ function valyrian() {
} else {
if (newNode.name === oldNode.name) {
newNode.dom = oldNode.dom;
if (
newNode.props[once] ||
lifecycleCall(newNode, onbeforeupdate, oldNode) === false
) {
if (newNode.props[once] || lifecycleCall(newNode, onbeforeupdate, oldNode) === false) {
newNode.children = oldNode.children;
} else {
removeProps(newNode, oldNode);
updateProps(newNode, oldNode);
lifecycleCall(
newNode,
v.isMounted ? onupdate : oncreate,
oldNode
);
lifecycleCall(newNode, v.isMounted ? onupdate : oncreate, oldNode);
patch(newNode, oldNode);
}
} else {
callRemove(oldNode);
newNode.dom = createElement(newNode.name, newNode.isSVG);
updateProps(newNode);
parentNode.dom.replaceChild(
newNode.dom,
parentNode.dom.childNodes[i]
);
parentNode.dom.replaceChild(newNode.dom, parentNode.dom.childNodes[i]);
lifecycleCall(newNode, oncreate);
patch(newNode, emptyNode);
}
Expand Down Expand Up @@ -409,9 +356,7 @@ function valyrian() {
if (mountedComponent) {
cleanupVnodes();
oldMainNode = mainNode;
mainNode = new Vnode(mainNode.name, mainNode.props, [
v(mountedComponent, props, ...children)
]);
mainNode = new Vnode(mainNode.name, mainNode.props, [v(mountedComponent, props, ...children)]);
mainNode.dom = oldMainNode.dom;
mainNode.isSVG = mainNode.name === "svg";
patch(mainNode, oldMainNode);
Expand All @@ -423,11 +368,7 @@ function valyrian() {
};

v.mount = (container, component, props, ...children) => {
let mainContainer = v.isNode
? createElement("div")
: typeof container === "string"
? document.querySelectorAll(container)[0]
: container;
mainContainer = v.isNode ? createElement("div") : typeof container === "string" ? document.querySelectorAll(container)[0] : container;

mainNode = v.domToVnode(mainContainer);
mountedComponent = component;
Expand All @@ -436,6 +377,7 @@ function valyrian() {
};

v.unMount = () => {
mainContainer = null;
mountedComponent = () => "";
let result = v.update();
mountedComponent = UND;
Expand Down Expand Up @@ -468,14 +410,8 @@ function valyrian() {

v.directive("if", hideDirective(false));
v.directive("unless", hideDirective(true));
v.directive(
"for",
(set, vnode) => (vnode.children = set.map(vnode.children[0]))
);
v.directive(
"show",
(bool, vnode) => (vnode.dom.style.display = bool ? "" : "none")
);
v.directive("for", (set, vnode) => (vnode.children = set.map(vnode.children[0])));
v.directive("show", (bool, vnode) => (vnode.dom.style.display = bool ? "" : "none"));
v.directive("class", (classes, vnode) => {
for (let name in classes) {
vnode.dom.classList.toggle(name, classes[name]);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"eslint-plugin-sonarjs": "^0.5.0",
"expect": "^26.4.2",
"faker": "^5.1.0",
"micro": "^9.3.4",
"micro-ex-router": "^1.7.2",
"fastify": "^3.5.0",
"mocha": "^8.1.3",
"node-dev": "^5.2.0",
"nyc": "^15.1.0",
Expand Down
Loading

0 comments on commit 5f9c4ab

Please sign in to comment.