diff --git a/demo/store/mpstore.esm.js b/demo/store/mpstore.esm.js index e5cb535..da4e556 100644 --- a/demo/store/mpstore.esm.js +++ b/demo/store/mpstore.esm.js @@ -1,5 +1,5 @@ /*! - * Mpstore.js v0.1.8 + * Mpstore.js v0.1.9 * (c) 2019-2019 Imtaotao * Released under the MIT License. */ @@ -514,7 +514,7 @@ function updateComponents(store, callback) { } }; - for (var i = 0; i < len; i++) { + for (var i = 0; i < depComponents.length; i++) { var _depComponents$i = depComponents[i], isPage = _depComponents$i.isPage, component = _depComponents$i.component, @@ -816,7 +816,7 @@ function () { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.1.8'; + this.version = '0.1.9'; this.state = Object.freeze(createModule({})); this.middleware = new Middleware(this); } @@ -1080,7 +1080,7 @@ function () { return Store; }(); -var version = '0.1.8'; +var version = '0.1.9'; var nativePage = Page; var nativeComponent = Component; diff --git a/dist/mpstore.common.js b/dist/mpstore.common.js index 5c129f7..e7de141 100644 --- a/dist/mpstore.common.js +++ b/dist/mpstore.common.js @@ -1,5 +1,5 @@ /*! - * Mpstore.js v0.1.8 + * Mpstore.js v0.1.9 * (c) 2019-2019 Imtaotao * Released under the MIT License. */ @@ -518,7 +518,7 @@ function updateComponents(store, callback) { } }; - for (var i = 0; i < len; i++) { + for (var i = 0; i < depComponents.length; i++) { var _depComponents$i = depComponents[i], isPage = _depComponents$i.isPage, component = _depComponents$i.component, @@ -820,7 +820,7 @@ function () { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.1.8'; + this.version = '0.1.9'; this.state = Object.freeze(createModule({})); this.middleware = new Middleware(this); } @@ -1084,7 +1084,7 @@ function () { return Store; }(); -var version = '0.1.8'; +var version = '0.1.9'; var nativePage = Page; var nativeComponent = Component; diff --git a/dist/mpstore.es6m.js b/dist/mpstore.es6m.js index a8c1183..7720ed9 100644 --- a/dist/mpstore.es6m.js +++ b/dist/mpstore.es6m.js @@ -1,5 +1,5 @@ /*! - * Mpstore.js v0.1.8 + * Mpstore.js v0.1.9 * (c) 2019-2019 Imtaotao * Released under the MIT License. */ @@ -383,7 +383,7 @@ function updateComponents (store, callback) { callback(); } }; - for (let i = 0; i < len; i++) { + for (let i = 0; i < depComponents.length; i++) { const { isPage, component, @@ -645,7 +645,7 @@ class Store { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.1.8'; + this.version = '0.1.9'; this.state = Object.freeze(createModule({})); this.middleware = new Middleware(this); } @@ -886,7 +886,7 @@ class Store { } } -const version = '0.1.8'; +const version = '0.1.9'; const nativePage = Page; const nativeComponent = Component; function expandConfig (config, expandMethods, isPage) { diff --git a/dist/mpstore.esm.js b/dist/mpstore.esm.js index e5cb535..da4e556 100644 --- a/dist/mpstore.esm.js +++ b/dist/mpstore.esm.js @@ -1,5 +1,5 @@ /*! - * Mpstore.js v0.1.8 + * Mpstore.js v0.1.9 * (c) 2019-2019 Imtaotao * Released under the MIT License. */ @@ -514,7 +514,7 @@ function updateComponents(store, callback) { } }; - for (var i = 0; i < len; i++) { + for (var i = 0; i < depComponents.length; i++) { var _depComponents$i = depComponents[i], isPage = _depComponents$i.isPage, component = _depComponents$i.component, @@ -816,7 +816,7 @@ function () { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.1.8'; + this.version = '0.1.9'; this.state = Object.freeze(createModule({})); this.middleware = new Middleware(this); } @@ -1080,7 +1080,7 @@ function () { return Store; }(); -var version = '0.1.8'; +var version = '0.1.9'; var nativePage = Page; var nativeComponent = Component; diff --git a/dist/mpstore.min.js b/dist/mpstore.min.js index b6b3715..e04f611 100644 --- a/dist/mpstore.min.js +++ b/dist/mpstore.min.js @@ -1,6 +1,6 @@ /*! - * Mpstore.js v0.1.8 + * Mpstore.js v0.1.9 * (c) 2019-2019 Imtaotao * Released under the MIT License. */ -"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n-1&&e.splice(n,1)}function callHook(e,t,n){if(e&&"function"==typeof e[t])return e[t].apply(e,n)}function isEmptyObject(e){for(var t in e)return!1;return!0}function mapObject(e,t){var n={};for(var a in e)e.hasOwnProperty(a)&&(n[a]=t(e[a]));return n}function createWraper(e,t,n){return function(){for(var a,r=arguments.length,o=new Array(r),i=0;i1&&void 0!==arguments[1]?arguments[1]:new WeakMap;if(null==e||isPrimitive(e)||"function"==typeof e||e instanceof Date)return e;if(t.has(e))return t.get(e);var n="function"!=typeof e.constructor?Object.create(null):new e.constructor;for(var a in t.set(e,n),e)n[a]=clone(e[a],t);return n}function mixin(e){var t=Object.create(null);if("function"==typeof e){e((function(e,n){assert("string"==typeof e,"The mixed method name must a string."),assert("function"==typeof n,"The mixed method is not a function."),assert(!(e in t),'The "'.concat(e,'" is exist,')),t[e]=n}))}return t}Object.defineProperty(exports,"__esModule",{value:!0});var MODULE_FLAG=Symbol("module");function isModule(e){return isPlainObject(e)&&!0===e[MODULE_FLAG]}function addModuleFlag(e){return e[MODULE_FLAG]=!0,e}function createModule(e){return assert(isPlainObject(e),"The base module object must be an plain object"),isModule(e)?e:(addModuleFlag(e),e)}function getModule(e,t){if(!t)return e;for(var n=e,a=t.split("."),r=0,o=a.length;re.length)for(r=t.length;--r>=e.length;){var i="".concat(n,"[").concat(r,"]");a.push(new Patch(ADD,i,t[r],e[r]))}}else a.push(new Patch(REPLACE,n,t,e))}function walkObject(e,t,n,a){for(var r in e){var o="".concat(n,".").concat(r);r in t?e[r]!==t[r]&&diffValues(e[r],t[r],o,a):a.push(new Patch(REMOVE,o,null,e[r]))}for(var i in t)if(!(i in e)){var s="".concat(n,".").concat(i);a.push(new Patch(ADD,s,t[i],null))}}function diff(e,t,n){var a=[];return walkObject(e,t,n,a),a}var REG=/[^\[\].]+(?=[\[\].])/g;function separatePath(e,t){var n=t.match(REG);if(n&&n.shift()&&n.length>0){for(var a=-1,r=null,o=e,i=null;a++0){if(!1===callHook(a,"willUpdate",[f,y,m,l])){s();continue}applyPatchs(f,m,s),"function"==typeof h&&h.call(e,f,y,m),callHook(a,"didUpdate",[f,y,l]),f.timeTravel&&f.timeTravel.push(m)}else s()}else s()}else t()}var TimeTravel=function(){function e(t,n,a){_classCallCheck(this,e),this.history=[],this.limit=a,this.component=t,this.GLOBALWORD=n,this.length=this.history.length,this.current=this.history.length,this.finallyState=t.data[n]}return _createClass(e,[{key:"push",value:function(e){var t=this.limit,n=this.history,a=this.GLOBALWORD,r=this.history.length,o=this.component.data;if(t>0){var i=r-t;i>=0&&this.history.splice(0,i+1),this.history.push(e),this.length=n.length,this.current=n.length,this.finallyState=clone(o[a])}}},{key:"go",value:function(e){var t=this.current,n=this.history,a=this.component,r=this.GLOBALWORD,o=this.finallyState;if(assert(r in a.data,"You can't use [timeTravel] because it only works for [global state]"),this.limit>0&&0!==e){var i=e+t,s=Math.abs(e);if(i<0||i>n.length)return void warning("Index [".concat(i,"] is not within the allowed range."),!0);for(var c=0,u=clone(a.data[r]);c++0&&applyPatchs(a,f),this.current+=e}}},{key:"forward",value:function(){this.go(1)}},{key:"back",value:function(){this.go(-1)}},{key:"toStart",value:function(){this.go(-this.current)}},{key:"toEnd",value:function(){this.go(this.history.length-this.current)}}]),e}(),COMMONACTION=function(){};function match(e,t){return e.action===COMMONACTION||(Array.isArray(e.action)?e.action.indexOf(t)>-1:t===e.action)}function handleLayer(e,t,n,a,r,o){try{t.call(n,a,r,e),o()}catch(t){var i=n.hooks;o(),i&&"function"==typeof i.middlewareError?i.middlewareError(e,a,t):warning("".concat(t,"\n\n --- from middleware [").concat(e.toString(),"] action."))}}var Middleware=function(){function e(t){_classCallCheck(this,e),this.stack=[],this.store=t,this.isProcessing=!1}return _createClass(e,[{key:"use",value:function(e,t){assert(!this.isProcessing,"can't allow add new middleware in the middleware processing."),this.stack.push({fn:t,action:e})}},{key:"remove",value:function(e,t){var n=this.stack.findIndex((function(n){return n.fn===t&&n.action===e}));n>-1&&this.stack.splice(n,1)}},{key:"process",value:function(e,t,n){var a=this;this.isProcessing=!0;var r=function(){a.isProcessing=!1};if(this.stack.length>0){var o=0;!function t(i){var s=a.stack[o];for(o++;s&&!match(s,e);)s=a.stack[o++];s?handleLayer(e,s.fn,a.store,i,t,r):n(i,r)}(t)}else n(t,r)}}]),e}(),storeId=0;function assertReducer(e,t){var n=t.setter,a=t.partialState,r=e.toString();return assert(!("partialState"in t&&!isPlainObject(a)),"The [partialState] must be an object."+"\n\n --- from [".concat(r,"] action.")),"function"!=typeof n&&(t.setter=function(){warning("Can't changed [".concat(r,"] action value. Have you defined a setter?")+"\n\n --- from [".concat(r,"] action."))}),t}function filterReducer(e,t,n){var a=t.toString(),r=n.namespace,o=n.partialState;if("namespace"in n)assert("string"==typeof r,"The module namespace must be a string."+"\n\n --- from [".concat(a,"] action.")),n.partialState=createModuleByNamespace(r,o,e,a,(function(e,t){return"The [".concat(e,"] already exists in [").concat(t,"] module, ")+"Please don't repeat defined. \n\n --- from [".concat(a,"] action.")}));else for(var i in o)assert(!e.hasOwnProperty(i),"The [".concat(i,"] already exists in global state, ")+"Please don't repeat defined. \n\n --- from [".concat(a,"] action."));return n}var Store=function(){function e(t){_classCallCheck(this,e),this.hooks=t,this.reducers=[],this.id=++storeId,this.depComponents=[],this.GLOBALWORD="global",this.isDispatching=!1,this.version="0.1.8",this.state=Object.freeze(createModule({})),this.middleware=new Middleware(this)}return _createClass(e,[{key:"add",value:function(e,t){var n=_typeof(e);assert("string"===n||"symbol"===n,"The action must be a Symbol or String, but now is [".concat(n,"].")),assert(!this.reducers.find((function(t){return t.action===e})),"Can't repeat defined [".concat(e.toString(),"] action.")),assertReducer(e,t),filterReducer(this.state,e,t),t.action=e,this.reducers.push(t);var a=t.partialState;a&&!isEmptyObject(a)&&(this.state=mergeState(this.state,a))}},{key:"dispatch",value:function(e,t,n){var a=this,r=this.reducers,o=this.isDispatching,i=e.toString();assert(!o,'It is not allowed to call "dispatch" during dispatch execution.'+"\n\n --- from [".concat(i,"] action."));var s=r.find((function(t){return t.action===e}));assert(s,"The [".concat(i,"] action does not exist. ")+"Maybe you have not defined."),this.middleware.process(e,t,(function(e,t){a.isDispatching=!0;try{var r,o=s.namespace,c="string"==typeof o;if(c){var u=a.getModule(o,"\n\n --- from [".concat(i,"] action."));r=s.setter(u,e,a.state)}else r=s.setter(a.state,e);assert(isPlainObject(r),"setter function should be return a plain object."),isEmptyObject(r)||(c?(r=createModuleByNamespace(o,r,a.state,i),a.state=mergeState(a.state,r)):a.state=deepFreeze(mergeModule(a.state,r)))}finally{a.isDispatching=!1,t()}updateComponents(a,(function(){"function"==typeof n&&n(e)}))}))}},{key:"use",value:function(e,t){var n=this;return"function"==typeof e&&e!==COMMONACTION&&(t=e,e=COMMONACTION),this.middleware.use(e,t),function(){return n.middleware.remove(e,t)}}},{key:"setNamespace",value:function(e){assert(e&&"string"==typeof e,"The [namespace] must be a string"),this.GLOBALWORD=e}},{key:"getModule",value:function(e,t){if(assert("string"==typeof e,"the namespace mast be a string"),!e)return this.state;var n=getModule(this.state,e);return t&&null===n&&warning("The [".concat(e,"] module is not exist.").concat(t||"")),n}},{key:"addModule",value:function(e,t){var n=this;if(assert("string"==typeof e,"the namespace mast be a string"),isPlainObject(t)){var a=Object.keys(t),r=Object.getOwnPropertySymbols(t);if(a.length+r.length>0){for(var o=0,i=function(a){var r=t[a];r.namespace=e,n.add(a,r)};o0&&applyPatchs(e,r,o)}};if(t)e.onLoad=createWraper(e.onLoad,(function(){b(this),this.store=r})),e.onUnload=createWraper(e.onUnload,null,(function(){remove(r.depComponents,this)}));else{e.lifetimes=e.lifetimes||{};var O=function(t){return e[t]||e.lifetimes[t]},w=function(t,n){return e[t]=e.lifetimes[t]=n};w("attached",createWraper(O("attached"),(function(){b(this),this.store=r}))),w("detached",createWraper(O("detached"),null,(function(){remove(r.depComponents,this)})))}}}]),e}(),version="0.1.8",nativePage=Page,nativeComponent=Component;function expandConfig(e,t,n){isEmptyObject(t)||(n?mixinMethods(e,t):(e.methods=e.methods||{},mixinMethods(e.methods,t)))}function index(e,t){var n=new Store(t),a=mixin(e);return Page=createWraper(nativePage,(function(e){callHook(t,"createBefore",[e,!0]),expandConfig(e,a,!0),n.rewirteConfigAndAddDep(e,!0)})),Component=createWraper(nativeComponent,(function(e){callHook(t,"createBefore",[e,!1]),expandConfig(e,a,!1),n.rewirteConfigAndAddDep(e,!1)})),n}exports.clone=clone,exports.createModule=createModule,exports.default=index,exports.diff=diff,exports.isModule=isModule,exports.restore=restore,exports.version=version; +"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n-1&&e.splice(n,1)}function callHook(e,t,n){if(e&&"function"==typeof e[t])return e[t].apply(e,n)}function isEmptyObject(e){for(var t in e)return!1;return!0}function mapObject(e,t){var n={};for(var a in e)e.hasOwnProperty(a)&&(n[a]=t(e[a]));return n}function createWraper(e,t,n){return function(){for(var a,r=arguments.length,o=new Array(r),i=0;i1&&void 0!==arguments[1]?arguments[1]:new WeakMap;if(null==e||isPrimitive(e)||"function"==typeof e||e instanceof Date)return e;if(t.has(e))return t.get(e);var n="function"!=typeof e.constructor?Object.create(null):new e.constructor;for(var a in t.set(e,n),e)n[a]=clone(e[a],t);return n}function mixin(e){var t=Object.create(null);if("function"==typeof e){e((function(e,n){assert("string"==typeof e,"The mixed method name must a string."),assert("function"==typeof n,"The mixed method is not a function."),assert(!(e in t),'The "'.concat(e,'" is exist,')),t[e]=n}))}return t}Object.defineProperty(exports,"__esModule",{value:!0});var MODULE_FLAG=Symbol("module");function isModule(e){return isPlainObject(e)&&!0===e[MODULE_FLAG]}function addModuleFlag(e){return e[MODULE_FLAG]=!0,e}function createModule(e){return assert(isPlainObject(e),"The base module object must be an plain object"),isModule(e)?e:(addModuleFlag(e),e)}function getModule(e,t){if(!t)return e;for(var n=e,a=t.split("."),r=0,o=a.length;re.length)for(r=t.length;--r>=e.length;){var i="".concat(n,"[").concat(r,"]");a.push(new Patch(ADD,i,t[r],e[r]))}}else a.push(new Patch(REPLACE,n,t,e))}function walkObject(e,t,n,a){for(var r in e){var o="".concat(n,".").concat(r);r in t?e[r]!==t[r]&&diffValues(e[r],t[r],o,a):a.push(new Patch(REMOVE,o,null,e[r]))}for(var i in t)if(!(i in e)){var s="".concat(n,".").concat(i);a.push(new Patch(ADD,s,t[i],null))}}function diff(e,t,n){var a=[];return walkObject(e,t,n,a),a}var REG=/[^\[\].]+(?=[\[\].])/g;function separatePath(e,t){var n=t.match(REG);if(n&&n.shift()&&n.length>0){for(var a=-1,r=null,o=e,i=null;a++0){if(!1===callHook(a,"willUpdate",[f,y,m,u])){s();continue}applyPatchs(f,m,s),"function"==typeof h&&h.call(e,f,y,m),callHook(a,"didUpdate",[f,y,u]),f.timeTravel&&f.timeTravel.push(m)}else s()}else s()}else t()}var TimeTravel=function(){function e(t,n,a){_classCallCheck(this,e),this.history=[],this.limit=a,this.component=t,this.GLOBALWORD=n,this.length=this.history.length,this.current=this.history.length,this.finallyState=t.data[n]}return _createClass(e,[{key:"push",value:function(e){var t=this.limit,n=this.history,a=this.GLOBALWORD,r=this.history.length,o=this.component.data;if(t>0){var i=r-t;i>=0&&this.history.splice(0,i+1),this.history.push(e),this.length=n.length,this.current=n.length,this.finallyState=clone(o[a])}}},{key:"go",value:function(e){var t=this.current,n=this.history,a=this.component,r=this.GLOBALWORD,o=this.finallyState;if(assert(r in a.data,"You can't use [timeTravel] because it only works for [global state]"),this.limit>0&&0!==e){var i=e+t,s=Math.abs(e);if(i<0||i>n.length)return void warning("Index [".concat(i,"] is not within the allowed range."),!0);for(var c=0,l=clone(a.data[r]);c++0&&applyPatchs(a,f),this.current+=e}}},{key:"forward",value:function(){this.go(1)}},{key:"back",value:function(){this.go(-1)}},{key:"toStart",value:function(){this.go(-this.current)}},{key:"toEnd",value:function(){this.go(this.history.length-this.current)}}]),e}(),COMMONACTION=function(){};function match(e,t){return e.action===COMMONACTION||(Array.isArray(e.action)?e.action.indexOf(t)>-1:t===e.action)}function handleLayer(e,t,n,a,r,o){try{t.call(n,a,r,e),o()}catch(t){var i=n.hooks;o(),i&&"function"==typeof i.middlewareError?i.middlewareError(e,a,t):warning("".concat(t,"\n\n --- from middleware [").concat(e.toString(),"] action."))}}var Middleware=function(){function e(t){_classCallCheck(this,e),this.stack=[],this.store=t,this.isProcessing=!1}return _createClass(e,[{key:"use",value:function(e,t){assert(!this.isProcessing,"can't allow add new middleware in the middleware processing."),this.stack.push({fn:t,action:e})}},{key:"remove",value:function(e,t){var n=this.stack.findIndex((function(n){return n.fn===t&&n.action===e}));n>-1&&this.stack.splice(n,1)}},{key:"process",value:function(e,t,n){var a=this;this.isProcessing=!0;var r=function(){a.isProcessing=!1};if(this.stack.length>0){var o=0;!function t(i){var s=a.stack[o];for(o++;s&&!match(s,e);)s=a.stack[o++];s?handleLayer(e,s.fn,a.store,i,t,r):n(i,r)}(t)}else n(t,r)}}]),e}(),storeId=0;function assertReducer(e,t){var n=t.setter,a=t.partialState,r=e.toString();return assert(!("partialState"in t&&!isPlainObject(a)),"The [partialState] must be an object."+"\n\n --- from [".concat(r,"] action.")),"function"!=typeof n&&(t.setter=function(){warning("Can't changed [".concat(r,"] action value. Have you defined a setter?")+"\n\n --- from [".concat(r,"] action."))}),t}function filterReducer(e,t,n){var a=t.toString(),r=n.namespace,o=n.partialState;if("namespace"in n)assert("string"==typeof r,"The module namespace must be a string."+"\n\n --- from [".concat(a,"] action.")),n.partialState=createModuleByNamespace(r,o,e,a,(function(e,t){return"The [".concat(e,"] already exists in [").concat(t,"] module, ")+"Please don't repeat defined. \n\n --- from [".concat(a,"] action.")}));else for(var i in o)assert(!e.hasOwnProperty(i),"The [".concat(i,"] already exists in global state, ")+"Please don't repeat defined. \n\n --- from [".concat(a,"] action."));return n}var Store=function(){function e(t){_classCallCheck(this,e),this.hooks=t,this.reducers=[],this.id=++storeId,this.depComponents=[],this.GLOBALWORD="global",this.isDispatching=!1,this.version="0.1.9",this.state=Object.freeze(createModule({})),this.middleware=new Middleware(this)}return _createClass(e,[{key:"add",value:function(e,t){var n=_typeof(e);assert("string"===n||"symbol"===n,"The action must be a Symbol or String, but now is [".concat(n,"].")),assert(!this.reducers.find((function(t){return t.action===e})),"Can't repeat defined [".concat(e.toString(),"] action.")),assertReducer(e,t),filterReducer(this.state,e,t),t.action=e,this.reducers.push(t);var a=t.partialState;a&&!isEmptyObject(a)&&(this.state=mergeState(this.state,a))}},{key:"dispatch",value:function(e,t,n){var a=this,r=this.reducers,o=this.isDispatching,i=e.toString();assert(!o,'It is not allowed to call "dispatch" during dispatch execution.'+"\n\n --- from [".concat(i,"] action."));var s=r.find((function(t){return t.action===e}));assert(s,"The [".concat(i,"] action does not exist. ")+"Maybe you have not defined."),this.middleware.process(e,t,(function(e,t){a.isDispatching=!0;try{var r,o=s.namespace,c="string"==typeof o;if(c){var l=a.getModule(o,"\n\n --- from [".concat(i,"] action."));r=s.setter(l,e,a.state)}else r=s.setter(a.state,e);assert(isPlainObject(r),"setter function should be return a plain object."),isEmptyObject(r)||(c?(r=createModuleByNamespace(o,r,a.state,i),a.state=mergeState(a.state,r)):a.state=deepFreeze(mergeModule(a.state,r)))}finally{a.isDispatching=!1,t()}updateComponents(a,(function(){"function"==typeof n&&n(e)}))}))}},{key:"use",value:function(e,t){var n=this;return"function"==typeof e&&e!==COMMONACTION&&(t=e,e=COMMONACTION),this.middleware.use(e,t),function(){return n.middleware.remove(e,t)}}},{key:"setNamespace",value:function(e){assert(e&&"string"==typeof e,"The [namespace] must be a string"),this.GLOBALWORD=e}},{key:"getModule",value:function(e,t){if(assert("string"==typeof e,"the namespace mast be a string"),!e)return this.state;var n=getModule(this.state,e);return t&&null===n&&warning("The [".concat(e,"] module is not exist.").concat(t||"")),n}},{key:"addModule",value:function(e,t){var n=this;if(assert("string"==typeof e,"the namespace mast be a string"),isPlainObject(t)){var a=Object.keys(t),r=Object.getOwnPropertySymbols(t);if(a.length+r.length>0){for(var o=0,i=function(a){var r=t[a];r.namespace=e,n.add(a,r)};o0&&applyPatchs(e,r,o)}};if(t)e.onLoad=createWraper(e.onLoad,(function(){b(this),this.store=r})),e.onUnload=createWraper(e.onUnload,null,(function(){remove(r.depComponents,this)}));else{e.lifetimes=e.lifetimes||{};var O=function(t){return e[t]||e.lifetimes[t]},w=function(t,n){return e[t]=e.lifetimes[t]=n};w("attached",createWraper(O("attached"),(function(){b(this),this.store=r}))),w("detached",createWraper(O("detached"),null,(function(){remove(r.depComponents,this)})))}}}]),e}(),version="0.1.9",nativePage=Page,nativeComponent=Component;function expandConfig(e,t,n){isEmptyObject(t)||(n?mixinMethods(e,t):(e.methods=e.methods||{},mixinMethods(e.methods,t)))}function index(e,t){var n=new Store(t),a=mixin(e);return Page=createWraper(nativePage,(function(e){callHook(t,"createBefore",[e,!0]),expandConfig(e,a,!0),n.rewirteConfigAndAddDep(e,!0)})),Component=createWraper(nativeComponent,(function(e){callHook(t,"createBefore",[e,!1]),expandConfig(e,a,!1),n.rewirteConfigAndAddDep(e,!1)})),n}exports.clone=clone,exports.createModule=createModule,exports.default=index,exports.diff=diff,exports.isModule=isModule,exports.restore=restore,exports.version=version; diff --git a/package.json b/package.json index b58a5eb..8bdba25 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rustle/mp-store", - "version": "0.1.8", + "version": "0.1.9", "description": "A lightweight wechat miniprogram state management library", "main": "dist/mpstore.common.js", "module": "dist/mpstore.esm.js", diff --git a/src/update.js b/src/update.js index 827f910..a3979f9 100644 --- a/src/update.js +++ b/src/update.js @@ -1,89 +1,90 @@ -import { diff } from './diff' -import { callHook } from './utils' - -export function applyPatchs (component, patchs, callback) { - const destObject = {} - - for (let i = 0, len = patchs.length; i < len; i++) { - const { value, path } = patchs[i] - destObject[path] = value - } - - component.setData(destObject, callback) -} - -// update page and component -export function updateComponents (store, callback) { - let total = 0 - const { - hooks, - GLOBALWORD, - depComponents, - } = store - const len = depComponents.length - - if (len === 0) { - callback() - return - } - - // call `callback`, when all component views are rendered - const renderedCallback = () => { - if (++total === len) { - callback() - } - } - - for (let i = 0; i < len; i++) { - const { - isPage, - component, - didUpdate, - willUpdate, - createState, - } = depComponents[i] - - if (component.data[GLOBALWORD]) { - const newPartialState = createState() - - // the `willUpdate` function will optimize component - if (typeof willUpdate === 'function') { - if (willUpdate.call(store, component, newPartialState) === false) { - renderedCallback() - continue - } - } - - // the base path is `GLOBALWORD` - // example: this.setData({ 'global.xx': xx }) - const patchs = diff(component.data[GLOBALWORD], newPartialState, GLOBALWORD) - - if (patchs.length > 0) { - // call global hooks - const params = [component, newPartialState, patchs, isPage] - if (callHook(hooks, 'willUpdate', params) === false) { - renderedCallback() - continue - } - - // update component - applyPatchs(component, patchs, renderedCallback) - - if (typeof didUpdate === 'function') { - didUpdate.call(store, component, newPartialState, patchs) - } - - callHook(hooks, 'didUpdate', [component, newPartialState, isPage]) - - // record patchs, allow playback view - if (component.timeTravel) { - component.timeTravel.push(patchs) - } - } else { - renderedCallback() - } - } else { - renderedCallback() - } - } +import { diff } from './diff' +import { callHook } from './utils' + +export function applyPatchs (component, patchs, callback) { + const destObject = {} + + for (let i = 0, len = patchs.length; i < len; i++) { + const { value, path } = patchs[i] + destObject[path] = value + } + + component.setData(destObject, callback) +} + +// update page and component +export function updateComponents (store, callback) { + let total = 0 + const { + hooks, + GLOBALWORD, + depComponents, + } = store + const len = depComponents.length + + if (len === 0) { + callback() + return + } + + // call `callback`, when all component views are rendered + const renderedCallback = () => { + if (++total === len) { + callback() + } + } + + // the component maybe aleard unload, so, can't use `len` + for (let i = 0; i < depComponents.length; i++) { + const { + isPage, + component, + didUpdate, + willUpdate, + createState, + } = depComponents[i] + + if (component.data[GLOBALWORD]) { + const newPartialState = createState() + + // the `willUpdate` function will optimize component + if (typeof willUpdate === 'function') { + if (willUpdate.call(store, component, newPartialState) === false) { + renderedCallback() + continue + } + } + + // the base path is `GLOBALWORD` + // example: this.setData({ 'global.xx': xx }) + const patchs = diff(component.data[GLOBALWORD], newPartialState, GLOBALWORD) + + if (patchs.length > 0) { + // call global hooks + const params = [component, newPartialState, patchs, isPage] + if (callHook(hooks, 'willUpdate', params) === false) { + renderedCallback() + continue + } + + // update component + applyPatchs(component, patchs, renderedCallback) + + if (typeof didUpdate === 'function') { + didUpdate.call(store, component, newPartialState, patchs) + } + + callHook(hooks, 'didUpdate', [component, newPartialState, isPage]) + + // record patchs, allow playback view + if (component.timeTravel) { + component.timeTravel.push(patchs) + } + } else { + renderedCallback() + } + } else { + renderedCallback() + } + } } \ No newline at end of file