From aee07e33e410ba370cf574fe20bd0f99b37eb219 Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Thu, 1 Jun 2017 16:08:28 -0700 Subject: [PATCH] make eventListeners dynamic (added if needed), and add advanced optimizations :zap: --- dist/moon.js | 46 +++++++++++++++++++++++------------------ dist/moon.min.js | 2 +- src/instance/methods.js | 7 ++++--- src/util/dom.js | 20 +++++++++++------- src/util/util.js | 6 ++++-- src/util/vdom.js | 13 +++++------- test/js/test.js | 6 +++--- 7 files changed, 56 insertions(+), 44 deletions(-) diff --git a/dist/moon.js b/dist/moon.js index a8463e26..5241cfc7 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -229,12 +229,14 @@ var child = children[i]; var childProps = child.props.attrs; var slotName = ""; + var slotValue = null; if ((slotName = childProps.slot) !== undefined) { - if (slots[slotName] === undefined) { + slotValue = slots[slotName]; + if (slotValue === undefined) { slots[slotName] = [child]; } else { - slots[slotName].push(child); + slotValue.push(child); } delete childProps.slot; } else { @@ -383,7 +385,10 @@ } } // Add all event listeners - addEventListeners(el, vnode); + var eventListeners = null; + if ((eventListeners = vnode.meta.eventListeners) !== undefined) { + addEventListeners(el, eventListeners); + } } // Setup Props @@ -406,8 +411,9 @@ parent.appendChild(node); // Check for Component - if (vnode.meta.component) { - createComponentFromVNode(node, vnode, vnode.meta.component); + var component = null; + if ((component = vnode.meta.component) !== undefined) { + createComponentFromVNode(node, vnode, component); } }; @@ -418,9 +424,10 @@ */ var removeChild = function (node, parent) { // Check for Component - if (node.__moon__) { + var componentInstance = null; + if ((componentInstance = node.__moon__) !== undefined) { // Component was unmounted, destroy it here - node.__moon__.destroy(); + componentInstance.destroy(); } // Remove the Node @@ -445,8 +452,9 @@ parent.replaceChild(newNode, oldNode); // Check for Component - if (vnode.meta.component) { - createComponentFromVNode(newNode, vnode, vnode.meta.component); + var component = null; + if ((component = vnode.meta.component) !== undefined) { + createComponentFromVNode(newNode, vnode, component); } }; @@ -563,7 +571,7 @@ } else if ((component = components[tag]) !== undefined) { // Resolve Component if (component.options.functional === true) { - return createFunctionalComponent(attrs, children, components[tag]); + return createFunctionalComponent(attrs, children, component); } else { meta.component = component; } @@ -620,9 +628,7 @@ * @param {Object} eventListeners * @param {Object} oldVNode */ - var diffEventListeners = function (node, eventListeners, oldVNode) { - var oldEventListeners = oldVNode.meta.eventListeners; - + var diffEventListeners = function (node, eventListeners, oldEventListeners) { for (var type in eventListeners) { var oldEventListener = oldEventListeners[type]; if (oldEventListener === undefined) { @@ -736,7 +742,7 @@ * @return {Object} adjusted node only if it was replaced */ var hydrate = function (node, vnode, parent) { - var nodeName = node ? node.nodeName.toLowerCase() : null; + var nodeName = node !== null ? node.nodeName.toLowerCase() : null; if (node === null) { // No node, create one @@ -819,10 +825,9 @@ * @param {Object} oldVNode * @param {Object} vnode * @param {Object} parent - * @param {Object} instance * @return {Number} patch type */ - var diff = function (oldVNode, vnode, parent, instance) { + var diff = function (oldVNode, vnode, parent) { if (oldVNode === null) { // No Node, append a node appendChild(createNodeFromVNode(vnode), vnode, parent); @@ -875,7 +880,7 @@ // Diff event listeners var eventListeners = null; if ((eventListeners = vnode.meta.eventListeners) !== undefined) { - diffEventListeners(_node, eventListeners, oldVNode); + diffEventListeners(_node, eventListeners, oldVNode.meta.eventListeners); } // Check if innerHTML was changed, don't diff children @@ -2088,11 +2093,12 @@ if (vnode.type !== old.type) { // Root Element Changed During Diff // Replace Root Element - replaceChild(old.meta.el, createNodeFromVNode(vnode), parent); + var newRoot = createNodeFromVNode(vnode); + replaceChild(old.meta.el, newRoot, vnode, parent); // Update Bound Instance - this.$el = vnode.meta.el; - this.$el.__moon__ = this; + newRoot.__moon__ = this; + this.$el = newRoot; } else { // Diff diff(old, vnode, parent); diff --git a/dist/moon.min.js b/dist/moon.min.js index ed148eee..e9bc8d18 100644 --- a/dist/moon.min.js +++ b/dist/moon.min.js @@ -4,4 +4,4 @@ * Released under the MIT License * http://moonjs.ga */ -!function(e,t){"object"==typeof module&&module.exports?module.exports=t():e.Moon=t()}(this,function(){"use strict";function e(e){this.instance=e,this.cache={},this.setters={},this.clear={},this.target=null,this.map={}}function t(t){void 0===t&&(t={}),this.$options=t;var r=this;m(this,"$name",t.name,"root");var n=t.data;this.$data=void 0===n?{}:"function"==typeof n?n():n,m(this,"$render",t.render,$),m(this,"$hooks",t.hooks,{});var i=t.methods;void 0!==i&&a(r,i),this.$events={},this.$dom={},this.$observer=new e(this),this.$destroyed=!0,this.$queued=!1;var o=t.computed;void 0!==o&&s(this,o),this.init()}var r={},n={},i={},o={stop:"event.stopPropagation();",prevent:"event.preventDefault();",ctrl:"if(!event.ctrlKey) {return;};",shift:"if(!event.shiftKey) {return;};",alt:"if(!event.altKey) {return;};",enter:"if(event.keyCode !== 13) {return;};"},a=function(e,t){for(var r in t)!function(t,r){e.$data[t]=function(){return r.apply(e,arguments)}}(r,t[r])},s=function(e,t){for(var r in t)!function(r){var n=e.$observer;n.observe(r),Object.defineProperty(e.$data,r,{get:function(){var i=null;return void 0===n.cache[r]?(n.target=r,i=t[r].get.call(e),n.target=null,n.cache[r]=i):i=n.cache[r],i},set:$});var i=null;void 0!==(i=t[r].set)&&(n.setters[r]=i)}(r)};e.prototype.observe=function(e){var t=this;this.clear[e]=function(){t.cache[e]=void 0}},e.prototype.notify=function(e,t){var r=null;if(void 0!==(r=this.map[e]))for(var n=0;nv?c:v,d=0,h=0;d"===o||" "===o)break;i+=o,n++}var a={type:"tag",value:i};return e.tokens.push(a),e.current=n,a},W=function(e,t){for(var r=t.input,n=r.length,i=t.current,o=r.charAt(i),a=r.charAt(i+1),s=function(){i++,o=r.charAt(i),a=r.charAt(i+1)},l={};i"!==o&&("/"!==o||">"!==a);)if(" "!==o){for(var u="",c=!1;i"===o||"/"===o&&">"===a){c=!0;break}u+=o,s()}var v={name:u,value:"",meta:{}};if(!0!==c){s();var f=" ";for("'"!==o&&'"'!==o||(f=o,s());i1&&(a=c.shift(),u=c.join("(").slice(0,-1),q(u,i));for(var v="",f=0;fc?u:c,p=0,d=0;p"===o||" "===o)break;i+=o,n++}var a={type:"tag",value:i};return e.tokens.push(a),e.current=n,a},W=function(e,t){for(var r=t.input,n=r.length,i=t.current,o=r.charAt(i),a=r.charAt(i+1),s=function(){i++,o=r.charAt(i),a=r.charAt(i+1)},l={};i"!==o&&("/"!==o||">"!==a);)if(" "!==o){for(var u="",c=!1;i"===o||"/"===o&&">"===a){c=!0;break}u+=o,s()}var v={name:u,value:"",meta:{}};if(!0!==c){s();var f=" ";for("'"!==o&&'"'!==o||(f=o,s());i1&&(a=c.shift(),u=c.join("(").slice(0,-1),q(u,i));for(var v="",f=0;f