Comparing version 2.0.0-alpha.8 to 2.0.0-alpha.9
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).hyperapp={})}(this,function(e){"use strict";var n="http://www.w3.org/1999/xlink",t={},r=[],l=r.map,u=Array.isArray,i="function"==typeof Promise?function(e){Promise.resolve().then(e)}:setTimeout,o=function(e,n){var t={};for(var r in e)t[r]=e[r];for(var r in n)t[r]=n[r];return t};var f=function(e,t,r,l,i,f){if("key"===t);else if("style"===t)for(var a in o(r,l)){var c=null==l||null==l[a]?"":l[a];"-"===a[0]?e[t].setProperty(a,c):e[t][a]=c}else if("class"===t)(l=function e(n){var t=typeof n,r="";if("string"===t||"number"===t)return n||"";if(u(n)&&n.length>0)for(var l=0,i=n.length;l<i;l++)""!==(t=e(n[l]))&&(r+=(r&&" ")+t);else for(var l in n)n[l]&&(r+=(r&&" ")+l);return r}(l))?e.setAttribute(t,l):e.removeAttribute(t);else if("o"===t[0]&&"n"===t[1])e.events||(e.events={}),e.events[t=t.slice(2).toLowerCase()]=l,null==l?e.removeEventListener(t,i):null==r&&e.addEventListener(t,i);else{var s=null==l||!1===l;if(t in e&&"list"!==t&&"draggable"!==t&&"spellcheck"!==t&&"translate"!==t&&!f)e[t]=null==l?"":l,s&&e.removeAttribute(t);else f&&t!==(t=t.replace(/^xlink:?/,""))?s?e.removeAttributeNS(n,t):e.setAttributeNS(n,t,l):s?e.removeAttribute(t):e.setAttribute(t,l)}},a=function(e,n,t){for(var r=3===e.type?document.createTextNode(e.name):(t=t||"svg"===e.name)?document.createElementNS("http://www.w3.org/2000/svg",e.name):document.createElement(e.name),l=0,u=e.children.length;l<u;l++)r.appendChild(a(e.children[l]=v(e.children[l]),n,t));var i=e.props;for(var o in i)f(r,o,null,i[o],n,t);return e.element=r},c=function(e,n){e.removeChild(n.element)},s=function(e){return null==e?null:e.key},p=function(e,n,t,r,l,u){if(r===t);else if(null!=t&&3===t.type&&3===r.type)t.name!==r.name&&(n.nodeValue=r.name);else if(null==t||t.name!==r.name){var i=e.insertBefore(a(r=v(r),l,u),n);null!=t&&c(e,t),n=i}else{var m,d,h;!function(e,n,t,r,l){for(var u in o(n,t))("value"===u||"checked"===u?e[u]:n[u])!==t[u]&&f(e,u,n[u],t[u],r,l)}(n,t.props,r.props,l,u=u||"svg"===r.name);for(var y,g=t.children,b=0,k=g.length-1,w=r.children,A=0,x=w.length-1;A<=x&&b<=k&&(h=s(g[b]),y=s(w[A]),null!=h&&h===y);)p(n,g[b].element,g[b],w[A]=v(w[A],g[b]),l,u),b++,A++;for(;A<=x&&b<=k&&(h=s(g[k]),y=s(w[x]),null!=h&&h===y);)p(n,g[k].element,g[k],w[x]=v(w[x],g[k]),l,u),k--,x--;if(b>k)for(;A<=x;)n.insertBefore(a(w[A]=v(w[A++]),l,u),(d=g[b])&&d.element);else if(A>x)for(;b<=k;)c(n,g[b++]);else{for(var N=function(e,n,t){for(var r,l,u={};n<=t;n++)null!=(r=(l=e[n]).key)&&(u[r]=l);return u}(g,b,k),z={};A<=x;)h=s(d=g[b]),y=s(w[A]=v(w[A],d)),z[h]||null!=y&&y===s(g[b+1])?(null==h&&c(n,d),b++):null==y||1===t.type?(null==h&&(p(n,d&&d.element,d,w[A],l,u),A++),b++):(h===y?(p(n,d.element,d,w[A],l,u),z[y]=!0,b++):null!=(m=N[y])?(p(n,n.insertBefore(m.element,d&&d.element),m,w[A],l,u),z[y]=!0):p(n,d&&d.element,null,w[A],l,u),A++);for(;b<=k;)null==s(d=g[b++])&&c(n,d);for(var L in N)null==z[L]&&c(n,N[L])}}return r.element=n},v=function(e,n){return 2===e.type?!n||function(e,n){for(var t in e)if(e[t]!==n[t])return!0;for(var t in n)if(e[t]!==n[t])return!0}(e.lazy,n.lazy)?e.render():n:e},m=function(e,n,t,r,l,u){return{name:e,props:n,children:t,element:r,key:l,type:u}},d=function(e,n){return m(e,t,r,n,null,3)},h=function(e){return 3===e.nodeType?d(e.nodeValue,e):y(e)},y=function(e){return m(e.nodeName.toLowerCase(),t,l.call(e.childNodes,h),e,null,1)},g=function(e,n,t,r,l){return p(e,n,t,r,l)},b=function(e,n){return u(e)&&u(n)&&"function"==typeof e[0]&&e[0]===n[0]},k=function(e,n,t){if(n&&(!n[0]||u(n[0]))||e&&(!e[0]||u(e[0]))){for(var r=[],l=n||[n],i=e||[e],f=0;f<l.length||f<i.length;f++)r.push(k(i[f],l[f],t));return r}return n?!e||n[0]!==e[0]||function(e,n){for(var t in o(e,n)){if(e[t]!==n[t]&&!b(e[t],n[t]))return!0;n[t]=e[t]}}(n[1],e[1])?[n[0],n[1],n[0](n[1],t),e&&e[2]()]:e:e&&e[2]()};e.Lazy=function(e){return{type:2,key:e.key,lazy:e,render:function(){var n=e.render(e);return n.lazy=e,n}}},e.h=function(e,n){for(var t,r=[],l=[],i=arguments.length;i-- >2;)r.push(arguments[i]);for(null!=(n=null==n?{}:n).children&&(r.length<=0&&r.push(n.children),delete n.children);r.length>0;)if(u(t=r.pop()))for(i=t.length;i-- >0;)r.push(t[i]);else!1===t||!0===t||null==t||l.push("object"==typeof t?t:d(t));return"function"==typeof e?e(n,n.children=l):m(e,n,l,null,n.key,0)},e.app=function(e){var n,t,r=e.container,l=r&&r.children[0],o=l&&y(l),f=e.subscriptions,a=e.view,c=!1,s=function(e){n===e||c||i(m,c=!0),n=e},p=function(e,t){null==e||("function"==typeof e?p(e(n,t)):u(e)?"function"==typeof e[0]?p(e[0](n,e[1],t)):e.slice(1).map(function(e){e[0](e[1],p)},s(e[0])):s(e))},v=function(e){p(e.currentTarget.events[e.type],e)},m=function(){c=!1,f&&(t=k(t,f(n),p)),a&&(l=g(r,l,o,o=a(n),v))};p(e.init||{})}}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).hyperapp={})}(this,function(e){"use strict";var n={},t=[],r=t.map,o=Array.isArray,l="function"==typeof Promise?function(e){Promise.resolve().then(e)}:setTimeout,u=function(e,n){var t={};for(var r in e)t[r]=e[r];for(var r in n)t[r]=n[r];return t},i=function(e){return e.reduce(function(e,n){return e.concat(!(!n||!0===n)&&("function"==typeof n[0]?[n]:i(n)))},t)},f=function(e,n){return o(e)&&o(n)&&e[0]===n[0]&&"function"==typeof e[0]},c=function(e,n,t){return i(n).map(function(n,r){return r=e[r],n?!r||n[0]!==r[0]||function(e,n){for(var t in u(e,n)){if(e[t]!==n[t]&&!f(e[t],n[t]))return!0;n[t]=e[t]}}(n[1],r[1])?[n[0],n[1],n[0](n[1],t),r&&r[2]()]:r:r&&r[2]()})},a=function(e){var n="",t=typeof e;if("string"===t||"number"===t)return e;if(o(e)&&e.length>0)for(var r=0;r<e.length;r++)""!==(t=a(e[r]))&&(n+=(n&&" ")+t);else for(var r in e)e[r]&&(n+=(n&&" ")+r);return n},s=function(e,n,t,r,o,l){if("key"===n);else if("style"===n)for(var i in u(t,r)){var f=null==r||null==r[i]?"":r[i];"-"===i[0]?e[n].setProperty(i,f):e[n][i]=f}else"o"===n[0]&&"n"===n[1]?((e.events||(e.events={}))[n=n.slice(2).toLowerCase()]=r)?t||e.addEventListener(n,o):e.removeEventListener(n,o):"list"!==n&&!l&&n in e?e[n]=null==r?"":r:null==r||!1===r||"class"===n&&!(r=a(r))?e.removeAttribute(n):e.setAttribute(n,r)},p=function(e,n){e.removeChild(n.element)},m=function(e,n,t){for(var r=3===e.type?document.createTextNode(e.name):(t=t||"svg"===e.name)?document.createElementNS("http://www.w3.org/2000/svg",e.name):document.createElement(e.name),o=e.props,l=0,u=e.children.length;l<u;l++)r.appendChild(m(e.children[l]=d(e.children[l]),n,t));for(var i in o)s(r,i,null,o[i],n,t);return e.element=r},v=function(e){return null==e?null:e.key},y=function(e,n,t,r,o,l){if(r===t);else if(null!=t&&3===t.type&&3===r.type)t.name!==r.name&&(n.nodeValue=r.name);else if(null==t||t.name!==r.name){var i=e.insertBefore(m(r=d(r),o,l),n);null!=t&&p(e,t),n=i}else{var f,c;!function(e,n,t,r,o){for(var l in u(n,t))("value"===l||"checked"===l?e[l]:n[l])!==t[l]&&s(e,l,n[l],t[l],r,o)}(n,t.props,r.props,o,l=l||"svg"===r.name);for(var a,h=t.children,g=0,k=h.length-1,w=r.children,b=0,z=w.length-1;b<=z&&g<=k&&(A=v(h[g]),a=v(w[b]),null!=A&&A===a);)y(n,h[g].element,h[g],w[b]=d(w[b],h[g]),o,l),g++,b++;for(;b<=z&&g<=k&&(A=v(h[k]),a=v(w[z]),null!=A&&A===a);)y(n,h[k].element,h[k],w[z]=d(w[z],h[k]),o,l),k--,z--;if(g>k)for(;b<=z;)n.insertBefore(m(w[b]=d(w[b++]),o,l),(c=h[g])&&c.element);else if(b>z)for(;g<=k;)p(n,h[g++]);else{for(var L=function(e,n,t){for(var r,o,l={};n<=t;n++)null!=(r=(o=e[n]).key)&&(l[r]=o);return l}(h,g,k),x={};b<=z;)A=v(c=h[g]),a=v(w[b]=d(w[b],c)),x[A]||null!=a&&a===v(h[g+1])?(null==A&&p(n,c),g++):null==a||1===t.type?(null==A&&(y(n,c&&c.element,c,w[b],o,l),b++),g++):(A===a?(y(n,c.element,c,w[b],o,l),x[a]=!0,g++):null!=(f=L[a])?(y(n,n.insertBefore(f.element,c&&c.element),f,w[b],o,l),x[a]=!0):y(n,c&&c.element,null,w[b],o,l),b++);for(;g<=k;)null==v(c=h[g++])&&p(n,c);for(var A in L)null==x[A]&&p(n,L[A])}}return r.element=n},d=function(e,n){return 2===e.type?!n||function(e,n){for(var t in e)if(e[t]!==n[t])return!0;for(var t in n)if(e[t]!==n[t])return!0}(e.lazy,n.lazy)?e.render():n:e},h=function(e,n,t,r,o,l){return{name:e,props:n,children:t,element:r,type:l,key:o}},g=function(e,r){return h(e,n,t,r,null,3)},k=function(e){return 3===e.nodeType?g(e.nodeValue,e):w(e)},w=function(e){return h(e.nodeName.toLowerCase(),n,r.call(e.childNodes,k),e,null,1)};e.Lazy=function(e){return{type:2,key:e.key,lazy:e,render:function(){var n=e.render(e);return n.lazy=e,n}}},e.h=function(e,t){for(var r,l=[],u=[],i=arguments.length;i-- >2;)l.push(arguments[i]);for(;l.length>0;)if(o(r=l.pop()))for(i=r.length;i-- >0;)l.push(r[i]);else!1===r||!0===r||null==r||u.push("object"==typeof r?r:g(r));return t=t||n,"function"==typeof e?e(t,u):h(e,t,u,null,t.key,0)},e.app=function(e){var n=e.container,t=n&&n.children[0],r=t&&w(t),u=e.subscriptions,f=e.view,a=!1,s={},p=[],m=function(e){d(e.currentTarget.events[e.type],e)},v=function(e){s===e||a||l(h,a=!0),s=e},d=function(e,n){"function"==typeof e?d(e(s,n)):o(e)?"function"==typeof e[0]?d(e[0](s,e[1],n)):i(e.slice(1)).map(function(e){e&&e[0](e[1],d)},v(e[0])):v(e)},h=function(){a=!1,u&&(p=c(p,u(s),d)),f&&(t=y(n,t,r,r=f(s),m))};d(e.init)}}); | ||
//# sourceMappingURL=hyperapp.js.map |
{ | ||
"name": "hyperapp", | ||
"description": "JavaScript micro-framework for building web applications.", | ||
"version": "2.0.0-alpha.8", | ||
"version": "2.0.0-alpha.9", | ||
"main": "dist/hyperapp.js", | ||
"module": "src/index.js", | ||
"typings": "hyperapp.d.ts", | ||
"license": "MIT", | ||
"repository": "hyperapp/hyperapp", | ||
"repository": "jorgebucaran/hyperapp", | ||
"homepage": "https://hyperapp.js.org", | ||
@@ -30,3 +29,3 @@ "files": [ | ||
"prepare": "npm run build", | ||
"format": "prettier --write {src,test}/**/*.js {,test/ts/}*.{ts,tsx}", | ||
"format": "prettier --write {src,test}/**/*.js", | ||
"release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
@@ -36,4 +35,4 @@ }, | ||
"prettier": "^1.16.4", | ||
"rollup": "^1.2.3", | ||
"terser": "^3.16.1" | ||
"rollup": "^1.6.0", | ||
"terser": "^3.17.0" | ||
}, | ||
@@ -40,0 +39,0 @@ "prettier": { |
426
src/index.js
@@ -5,6 +5,2 @@ var DEFAULT_NODE = 0 | ||
var TEXT_NODE = 3 | ||
var XLINK_NS = "http://www.w3.org/1999/xlink" | ||
var SVG_NS = "http://www.w3.org/2000/svg" | ||
var EMPTY_OBJECT = {} | ||
@@ -24,18 +20,52 @@ var EMPTY_ARRAY = [] | ||
var merge = function(a, b) { | ||
var target = {} | ||
var out = {} | ||
for (var i in a) target[i] = a[i] | ||
for (var i in b) target[i] = b[i] | ||
for (var i in a) out[i] = a[i] | ||
for (var i in b) out[i] = b[i] | ||
return target | ||
return out | ||
} | ||
function createClass(obj) { | ||
var flatten = function(arr) { | ||
return arr.reduce(function(out, obj) { | ||
return out.concat( | ||
!obj || obj === true | ||
? false | ||
: typeof obj[0] === "function" | ||
? [obj] | ||
: flatten(obj) | ||
) | ||
}, EMPTY_ARRAY) | ||
} | ||
var isSameAction = function(a, b) { | ||
return isArray(a) && isArray(b) && a[0] === b[0] && typeof a[0] === "function" | ||
} | ||
var shouldRestart = function(a, b) { | ||
for (var k in merge(a, b)) { | ||
if (a[k] === b[k] || isSameAction(a[k], b[k])) b[k] = a[k] | ||
else return true | ||
} | ||
} | ||
var patchSub = function(sub, newSub, dispatch) { | ||
return flatten(newSub).map(function(a, b) { | ||
b = sub[b] | ||
return a | ||
? !b || a[0] !== b[0] || shouldRestart(a[1], b[1]) | ||
? [a[0], a[1], a[0](a[1], dispatch), b && b[2]()] | ||
: b | ||
: b && b[2]() | ||
}) | ||
} | ||
var createClass = function(obj) { | ||
var out = "" | ||
var tmp = typeof obj | ||
var out = "" | ||
if (tmp === "string" || tmp === "number") return obj || "" | ||
if (tmp === "string" || tmp === "number") return obj | ||
if (isArray(obj) && obj.length > 0) { | ||
for (var i = 0, length = obj.length; i < length; i++) { | ||
for (var i = 0; i < obj.length; i++) { | ||
if ((tmp = createClass(obj[i])) !== "") out += (out && " ") + tmp | ||
@@ -52,13 +82,6 @@ } | ||
var updateProperty = function( | ||
element, | ||
name, | ||
oldValue, | ||
newValue, | ||
eventCb, | ||
isSvg | ||
) { | ||
var updateProperty = function(element, name, value, newValue, eventCb, isSvg) { | ||
if (name === "key") { | ||
} else if (name === "style") { | ||
for (var i in merge(oldValue, newValue)) { | ||
for (var i in merge(value, newValue)) { | ||
var style = newValue == null || newValue[i] == null ? "" : newValue[i] | ||
@@ -71,49 +94,23 @@ if (i[0] === "-") { | ||
} | ||
} else if (name === "class") { | ||
if ((newValue = createClass(newValue))) { | ||
element.setAttribute(name, newValue) | ||
} else { | ||
element.removeAttribute(name) | ||
} | ||
} else { | ||
if (name[0] === "o" && name[1] === "n") { | ||
if (!element.events) element.events = {} | ||
element.events[(name = name.slice(2).toLowerCase())] = newValue | ||
if (newValue == null) { | ||
if ( | ||
!((element.events || (element.events = {}))[ | ||
(name = name.slice(2).toLowerCase()) | ||
] = newValue) | ||
) { | ||
element.removeEventListener(name, eventCb) | ||
} else if (oldValue == null) { | ||
} else if (!value) { | ||
element.addEventListener(name, eventCb) | ||
} | ||
} else if (name !== "list" && !isSvg && name in element) { | ||
element[name] = newValue == null ? "" : newValue | ||
} else if ( | ||
newValue == null || | ||
newValue === false || | ||
(name === "class" && !(newValue = createClass(newValue))) | ||
) { | ||
element.removeAttribute(name) | ||
} else { | ||
var nullOrFalse = newValue == null || newValue === false | ||
if ( | ||
name in element && | ||
name !== "list" && | ||
name !== "draggable" && | ||
name !== "spellcheck" && | ||
name !== "translate" && | ||
!isSvg | ||
) { | ||
element[name] = newValue == null ? "" : newValue | ||
if (nullOrFalse) { | ||
element.removeAttribute(name) | ||
} | ||
} else { | ||
var ns = isSvg && name !== (name = name.replace(/^xlink:?/, "")) | ||
if (ns) { | ||
if (nullOrFalse) { | ||
element.removeAttributeNS(XLINK_NS, name) | ||
} else { | ||
element.setAttributeNS(XLINK_NS, name, newValue) | ||
} | ||
} else { | ||
if (nullOrFalse) { | ||
element.removeAttribute(name) | ||
} else { | ||
element.setAttribute(name, newValue) | ||
} | ||
} | ||
} | ||
element.setAttribute(name, newValue) | ||
} | ||
@@ -123,2 +120,6 @@ } | ||
var removeElement = function(parent, node) { | ||
parent.removeChild(node.element) | ||
} | ||
var createElement = function(node, eventCb, isSvg) { | ||
@@ -129,9 +130,10 @@ var element = | ||
: (isSvg = isSvg || node.name === "svg") | ||
? document.createElementNS(SVG_NS, node.name) | ||
? document.createElementNS("http://www.w3.org/2000/svg", node.name) | ||
: document.createElement(node.name) | ||
var props = node.props | ||
for (var i = 0, length = node.children.length; i < length; i++) { | ||
for (var i = 0, len = node.children.length; i < len; i++) { | ||
element.appendChild( | ||
createElement( | ||
(node.children[i] = resolveNode(node.children[i])), | ||
(node.children[i] = getNode(node.children[i])), | ||
eventCb, | ||
@@ -143,5 +145,4 @@ isSvg | ||
var props = node.props | ||
for (var name in props) { | ||
updateProperty(element, name, null, props[name], eventCb, isSvg) | ||
for (var k in props) { | ||
updateProperty(element, k, null, props[k], eventCb, isSvg) | ||
} | ||
@@ -152,17 +153,8 @@ | ||
var updateElement = function(element, oldProps, newProps, eventCb, isSvg) { | ||
for (var name in merge(oldProps, newProps)) { | ||
var updateElement = function(element, props, newProps, eventCb, isSvg) { | ||
for (var k in merge(props, newProps)) { | ||
if ( | ||
(name === "value" || name === "checked" | ||
? element[name] | ||
: oldProps[name]) !== newProps[name] | ||
(k === "value" || k === "checked" ? element[k] : props[k]) !== newProps[k] | ||
) { | ||
updateProperty( | ||
element, | ||
name, | ||
oldProps[name], | ||
newProps[name], | ||
eventCb, | ||
isSvg | ||
) | ||
updateProperty(element, k, props[k], newProps[k], eventCb, isSvg) | ||
} | ||
@@ -172,6 +164,2 @@ } | ||
var removeElement = function(parent, node) { | ||
parent.removeChild(node.element) | ||
} | ||
var getKey = function(node) { | ||
@@ -190,19 +178,17 @@ return node == null ? null : node.key | ||
var patchElement = function(parent, element, oldNode, newNode, eventCb, isSvg) { | ||
if (newNode === oldNode) { | ||
var patch = function(parent, element, node, newNode, eventCb, isSvg) { | ||
if (newNode === node) { | ||
} else if ( | ||
oldNode != null && | ||
oldNode.type === TEXT_NODE && | ||
node != null && | ||
node.type === TEXT_NODE && | ||
newNode.type === TEXT_NODE | ||
) { | ||
if (oldNode.name !== newNode.name) { | ||
element.nodeValue = newNode.name | ||
} | ||
} else if (oldNode == null || oldNode.name !== newNode.name) { | ||
if (node.name !== newNode.name) element.nodeValue = newNode.name | ||
} else if (node == null || node.name !== newNode.name) { | ||
var newElement = parent.insertBefore( | ||
createElement((newNode = resolveNode(newNode)), eventCb, isSvg), | ||
createElement((newNode = getNode(newNode)), eventCb, isSvg), | ||
element | ||
) | ||
if (oldNode != null) removeElement(parent, oldNode) | ||
if (node != null) removeElement(parent, node) | ||
@@ -213,3 +199,3 @@ element = newElement | ||
element, | ||
oldNode.props, | ||
node.props, | ||
newNode.props, | ||
@@ -223,25 +209,25 @@ eventCb, | ||
var oldKey | ||
var oldChildren = oldNode.children | ||
var oldChStart = 0 | ||
var oldChEnd = oldChildren.length - 1 | ||
var key | ||
var children = node.children | ||
var start = 0 | ||
var end = children.length - 1 | ||
var newKey | ||
var newChildren = newNode.children | ||
var newChStart = 0 | ||
var newChEnd = newChildren.length - 1 | ||
var newStart = 0 | ||
var newEnd = newChildren.length - 1 | ||
while (newChStart <= newChEnd && oldChStart <= oldChEnd) { | ||
oldKey = getKey(oldChildren[oldChStart]) | ||
newKey = getKey(newChildren[newChStart]) | ||
while (newStart <= newEnd && start <= end) { | ||
key = getKey(children[start]) | ||
newKey = getKey(newChildren[newStart]) | ||
if (oldKey == null || oldKey !== newKey) break | ||
if (key == null || key !== newKey) break | ||
patchElement( | ||
patch( | ||
element, | ||
oldChildren[oldChStart].element, | ||
oldChildren[oldChStart], | ||
(newChildren[newChStart] = resolveNode( | ||
newChildren[newChStart], | ||
oldChildren[oldChStart] | ||
children[start].element, | ||
children[start], | ||
(newChildren[newStart] = getNode( | ||
newChildren[newStart], | ||
children[start] | ||
)), | ||
@@ -252,20 +238,17 @@ eventCb, | ||
oldChStart++ | ||
newChStart++ | ||
start++ | ||
newStart++ | ||
} | ||
while (newChStart <= newChEnd && oldChStart <= oldChEnd) { | ||
oldKey = getKey(oldChildren[oldChEnd]) | ||
newKey = getKey(newChildren[newChEnd]) | ||
while (newStart <= newEnd && start <= end) { | ||
key = getKey(children[end]) | ||
newKey = getKey(newChildren[newEnd]) | ||
if (oldKey == null || oldKey !== newKey) break | ||
if (key == null || key !== newKey) break | ||
patchElement( | ||
patch( | ||
element, | ||
oldChildren[oldChEnd].element, | ||
oldChildren[oldChEnd], | ||
(newChildren[newChEnd] = resolveNode( | ||
newChildren[newChEnd], | ||
oldChildren[oldChEnd] | ||
)), | ||
children[end].element, | ||
children[end], | ||
(newChildren[newEnd] = getNode(newChildren[newEnd], children[end])), | ||
eventCb, | ||
@@ -275,65 +258,62 @@ isSvg | ||
oldChEnd-- | ||
newChEnd-- | ||
end-- | ||
newEnd-- | ||
} | ||
if (oldChStart > oldChEnd) { | ||
while (newChStart <= newChEnd) { | ||
if (start > end) { | ||
while (newStart <= newEnd) { | ||
element.insertBefore( | ||
createElement( | ||
(newChildren[newChStart] = resolveNode(newChildren[newChStart++])), | ||
(newChildren[newStart] = getNode(newChildren[newStart++])), | ||
eventCb, | ||
isSvg | ||
), | ||
(childNode = oldChildren[oldChStart]) && childNode.element | ||
(childNode = children[start]) && childNode.element | ||
) | ||
} | ||
} else if (newChStart > newChEnd) { | ||
while (oldChStart <= oldChEnd) { | ||
removeElement(element, oldChildren[oldChStart++]) | ||
} else if (newStart > newEnd) { | ||
while (start <= end) { | ||
removeElement(element, children[start++]) | ||
} | ||
} else { | ||
var oldKeyed = createKeyMap(oldChildren, oldChStart, oldChEnd) | ||
var keyed = createKeyMap(children, start, end) | ||
var newKeyed = {} | ||
while (newChStart <= newChEnd) { | ||
oldKey = getKey((childNode = oldChildren[oldChStart])) | ||
while (newStart <= newEnd) { | ||
key = getKey((childNode = children[start])) | ||
newKey = getKey( | ||
(newChildren[newChStart] = resolveNode( | ||
newChildren[newChStart], | ||
childNode | ||
)) | ||
(newChildren[newStart] = getNode(newChildren[newStart], childNode)) | ||
) | ||
if ( | ||
newKeyed[oldKey] || | ||
(newKey != null && newKey === getKey(oldChildren[oldChStart + 1])) | ||
newKeyed[key] || | ||
(newKey != null && newKey === getKey(children[start + 1])) | ||
) { | ||
if (oldKey == null) { | ||
if (key == null) { | ||
removeElement(element, childNode) | ||
} | ||
oldChStart++ | ||
start++ | ||
continue | ||
} | ||
if (newKey == null || oldNode.type === RECYCLED_NODE) { | ||
if (oldKey == null) { | ||
patchElement( | ||
if (newKey == null || node.type === RECYCLED_NODE) { | ||
if (key == null) { | ||
patch( | ||
element, | ||
childNode && childNode.element, | ||
childNode, | ||
newChildren[newChStart], | ||
newChildren[newStart], | ||
eventCb, | ||
isSvg | ||
) | ||
newChStart++ | ||
newStart++ | ||
} | ||
oldChStart++ | ||
start++ | ||
} else { | ||
if (oldKey === newKey) { | ||
patchElement( | ||
if (key === newKey) { | ||
patch( | ||
element, | ||
childNode.element, | ||
childNode, | ||
newChildren[newChStart], | ||
newChildren[newStart], | ||
eventCb, | ||
@@ -343,6 +323,6 @@ isSvg | ||
newKeyed[newKey] = true | ||
oldChStart++ | ||
start++ | ||
} else { | ||
if ((savedNode = oldKeyed[newKey]) != null) { | ||
patchElement( | ||
if ((savedNode = keyed[newKey]) != null) { | ||
patch( | ||
element, | ||
@@ -354,3 +334,3 @@ element.insertBefore( | ||
savedNode, | ||
newChildren[newChStart], | ||
newChildren[newStart], | ||
eventCb, | ||
@@ -361,7 +341,7 @@ isSvg | ||
} else { | ||
patchElement( | ||
patch( | ||
element, | ||
childNode && childNode.element, | ||
null, | ||
newChildren[newChStart], | ||
newChildren[newStart], | ||
eventCb, | ||
@@ -372,8 +352,8 @@ isSvg | ||
} | ||
newChStart++ | ||
newStart++ | ||
} | ||
} | ||
while (oldChStart <= oldChEnd) { | ||
if (getKey((childNode = oldChildren[oldChStart++])) == null) { | ||
while (start <= end) { | ||
if (getKey((childNode = children[start++])) == null) { | ||
removeElement(element, childNode) | ||
@@ -383,5 +363,5 @@ } | ||
for (var key in oldKeyed) { | ||
for (var key in keyed) { | ||
if (newKeyed[key] == null) { | ||
removeElement(element, oldKeyed[key]) | ||
removeElement(element, keyed[key]) | ||
} | ||
@@ -400,7 +380,7 @@ } | ||
var resolveNode = function(newNode, oldNode) { | ||
var getNode = function(newNode, node) { | ||
return newNode.type === LAZY_NODE | ||
? !oldNode || shouldUpdate(newNode.lazy, oldNode.lazy) | ||
? !node || shouldUpdate(newNode.lazy, node.lazy) | ||
? newNode.render() | ||
: oldNode | ||
: node | ||
: newNode | ||
@@ -415,4 +395,4 @@ } | ||
element: element, | ||
key: key, | ||
type: type | ||
type: type, | ||
key: key | ||
} | ||
@@ -442,6 +422,2 @@ } | ||
var patch = function(container, element, oldNode, newNode, eventCb) { | ||
return (element = patchElement(container, element, oldNode, newNode, eventCb)) | ||
} | ||
export var Lazy = function(props) { | ||
@@ -461,14 +437,4 @@ return { | ||
export var h = function(name, props) { | ||
var node | ||
var rest = [] | ||
var children = [] | ||
var length = arguments.length | ||
while (length-- > 2) rest.push(arguments[length]) | ||
if ((props = props == null ? {} : props).children != null) { | ||
if (rest.length <= 0) { | ||
rest.push(props.children) | ||
} | ||
delete props.children | ||
for (var node, rest = [], children = [], i = arguments.length; i-- > 2; ) { | ||
rest.push(arguments[i]) | ||
} | ||
@@ -478,5 +444,3 @@ | ||
if (isArray((node = rest.pop()))) { | ||
for (length = node.length; length-- > 0; ) { | ||
rest.push(node[length]) | ||
} | ||
for (i = node.length; i-- > 0; ) rest.push(node[i]) | ||
} else if (node === false || node === true || node == null) { | ||
@@ -488,59 +452,26 @@ } else { | ||
props = props || EMPTY_OBJECT | ||
return typeof name === "function" | ||
? name(props, (props.children = children)) | ||
? name(props, children) | ||
: createVNode(name, props, children, null, props.key, DEFAULT_NODE) | ||
} | ||
var isSameAction = function(a, b) { | ||
return isArray(a) && isArray(b) && typeof a[0] === "function" && a[0] === b[0] | ||
} | ||
var shouldRestart = function(a, b) { | ||
for (var k in merge(a, b)) { | ||
if (a[k] === b[k] || isSameAction(a[k], b[k])) b[k] = a[k] | ||
else return true | ||
} | ||
} | ||
var patchSub = function(oldSub, newSub, dispatch) { | ||
if ( | ||
(newSub && (!newSub[0] || isArray(newSub[0]))) || | ||
(oldSub && (!oldSub[0] || isArray(oldSub[0]))) | ||
) { | ||
var subs = [] | ||
var newSubs = newSub ? newSub : [newSub] | ||
var oldSubs = oldSub ? oldSub : [oldSub] | ||
for (var i = 0; i < newSubs.length || i < oldSubs.length; i++) { | ||
subs.push(patchSub(oldSubs[i], newSubs[i], dispatch)) | ||
} | ||
return subs | ||
} | ||
return newSub | ||
? !oldSub || newSub[0] !== oldSub[0] || shouldRestart(newSub[1], oldSub[1]) | ||
? [ | ||
newSub[0], | ||
newSub[1], | ||
newSub[0](newSub[1], dispatch), | ||
oldSub && oldSub[2]() | ||
] | ||
: oldSub | ||
: oldSub && oldSub[2]() | ||
} | ||
export function app(props) { | ||
export var app = function(props) { | ||
var container = props.container | ||
var element = container && container.children[0] | ||
var oldNode = element && recycleElement(element) | ||
var node = element && recycleElement(element) | ||
var subs = props.subscriptions | ||
var view = props.view | ||
var renderLock = false | ||
var state | ||
var sub | ||
var lock = false | ||
var state = {} | ||
var sub = [] | ||
var eventCb = function(event) { | ||
dispatch(event.currentTarget.events[event.type], event) | ||
} | ||
var setState = function(newState) { | ||
if (!(state === newState || renderLock)) { | ||
defer(render, (renderLock = true)) | ||
if (!(state === newState || lock)) { | ||
defer(render, (lock = true)) | ||
} | ||
@@ -551,4 +482,3 @@ state = newState | ||
var dispatch = function(obj, props) { | ||
if (obj == null) { | ||
} else if (typeof obj === "function") { | ||
if (typeof obj === "function") { | ||
dispatch(obj(state, props)) | ||
@@ -559,4 +489,4 @@ } else if (isArray(obj)) { | ||
} else { | ||
obj.slice(1).map(function(fx) { | ||
fx[0](fx[1], dispatch) | ||
flatten(obj.slice(1)).map(function(fx) { | ||
fx && fx[0](fx[1], dispatch) | ||
}, setState(obj[0])) | ||
@@ -569,21 +499,11 @@ } | ||
var eventCb = function(event) { | ||
dispatch(event.currentTarget.events[event.type], event) | ||
} | ||
var render = function() { | ||
renderLock = false | ||
lock = false | ||
if (subs) sub = patchSub(sub, subs(state), dispatch) | ||
if (view) { | ||
element = patch( | ||
container, | ||
element, | ||
oldNode, | ||
(oldNode = view(state)), | ||
eventCb | ||
) | ||
element = patch(container, element, node, (node = view(state)), eventCb) | ||
} | ||
} | ||
dispatch(props.init || {}) | ||
dispatch(props.init) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
44526
438