From bda023e34704c480edfa9602387219323cf78f50 Mon Sep 17 00:00:00 2001 From: imtaotao Date: Tue, 12 Nov 2019 14:42:39 +0800 Subject: [PATCH] build new version --- demo/store/mpstore.esm.js | 44 +++++++++++++++++++++++++++------------ dist/mpstore.common.js | 44 +++++++++++++++++++++++++++------------ dist/mpstore.es6m.js | 40 ++++++++++++++++++++++++----------- dist/mpstore.esm.js | 44 +++++++++++++++++++++++++++------------ dist/mpstore.min.js | 2 +- package.json | 2 +- 6 files changed, 123 insertions(+), 53 deletions(-) diff --git a/demo/store/mpstore.esm.js b/demo/store/mpstore.esm.js index 0c37e3c..f981bee 100644 --- a/demo/store/mpstore.esm.js +++ b/demo/store/mpstore.esm.js @@ -392,7 +392,7 @@ function restore(obj, patchs) { return obj; } -function applyPatchs(component, patchs) { +function applyPatchs(component, patchs, callback) { var destObject = {}; for (var i = 0, len = patchs.length; i < len; i++) { @@ -402,14 +402,30 @@ function applyPatchs(component, patchs) { destObject[path] = value; } - component.setData(destObject); + component.setData(destObject, callback); } -function updateComponents(store) { +function updateComponents(store, callback) { + var total = 0; var hooks = store.hooks, GLOBALWORD = store.GLOBALWORD, depComponents = store.depComponents; var len = depComponents.length; - if (len === 0) return; + + if (len === 0) { + if (typeof callback === 'function') { + callback(); + } + + return; + } + + var renderedCallback = function renderedCallback() { + if (++total === len) { + if (typeof callback === 'function') { + callback(); + } + } + }; for (var i = 0; i < len; i++) { var _depComponents$i = depComponents[i], @@ -424,6 +440,7 @@ function updateComponents(store) { if (typeof willUpdate === 'function') { if (willUpdate.call(store, component, newPartialState) === false) { + renderedCallback(); continue; } } @@ -434,10 +451,11 @@ function updateComponents(store) { var params = [component, newPartialState, patchs, isPage]; if (callHook(hooks, 'willUpdate', params) === false) { + renderedCallback(); continue; } - applyPatchs(component, patchs); + applyPatchs(component, patchs, renderedCallback); if (typeof didUpdate === 'function') { didUpdate.call(store, component, newPartialState, patchs); @@ -448,7 +466,11 @@ function updateComponents(store) { if (component.timeTravel) { component.timeTravel.push(patchs); } + } else { + renderedCallback(); } + } else { + renderedCallback(); } } } @@ -685,7 +707,7 @@ function () { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.0.9'; + this.version = '0.0.10'; this.state = Object.freeze({}); this.middleware = new Middleware(this); } @@ -734,11 +756,7 @@ function () { restoreProcessState(); } - updateComponents(_this); - - if (typeof callback === 'function') { - callback(); - } + updateComponents(_this, callback); }); } }, { @@ -823,7 +841,7 @@ function () { var patchs = diff(component.data[GLOBALWORD], createState(), GLOBALWORD); if (patchs.length > 0) { - applyPatchs(component, patchs); + applyPatchs(component, patchs, GLOBALWORD); } } } @@ -862,7 +880,7 @@ function () { return Store; }(); -var version = '0.0.9'; +var version = '0.0.10'; var nativePage = Page; var nativeComponent = Component; diff --git a/dist/mpstore.common.js b/dist/mpstore.common.js index e7de14d..333e80e 100644 --- a/dist/mpstore.common.js +++ b/dist/mpstore.common.js @@ -396,7 +396,7 @@ function restore(obj, patchs) { return obj; } -function applyPatchs(component, patchs) { +function applyPatchs(component, patchs, callback) { var destObject = {}; for (var i = 0, len = patchs.length; i < len; i++) { @@ -406,14 +406,30 @@ function applyPatchs(component, patchs) { destObject[path] = value; } - component.setData(destObject); + component.setData(destObject, callback); } -function updateComponents(store) { +function updateComponents(store, callback) { + var total = 0; var hooks = store.hooks, GLOBALWORD = store.GLOBALWORD, depComponents = store.depComponents; var len = depComponents.length; - if (len === 0) return; + + if (len === 0) { + if (typeof callback === 'function') { + callback(); + } + + return; + } + + var renderedCallback = function renderedCallback() { + if (++total === len) { + if (typeof callback === 'function') { + callback(); + } + } + }; for (var i = 0; i < len; i++) { var _depComponents$i = depComponents[i], @@ -428,6 +444,7 @@ function updateComponents(store) { if (typeof willUpdate === 'function') { if (willUpdate.call(store, component, newPartialState) === false) { + renderedCallback(); continue; } } @@ -438,10 +455,11 @@ function updateComponents(store) { var params = [component, newPartialState, patchs, isPage]; if (callHook(hooks, 'willUpdate', params) === false) { + renderedCallback(); continue; } - applyPatchs(component, patchs); + applyPatchs(component, patchs, renderedCallback); if (typeof didUpdate === 'function') { didUpdate.call(store, component, newPartialState, patchs); @@ -452,7 +470,11 @@ function updateComponents(store) { if (component.timeTravel) { component.timeTravel.push(patchs); } + } else { + renderedCallback(); } + } else { + renderedCallback(); } } } @@ -689,7 +711,7 @@ function () { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.0.9'; + this.version = '0.0.10'; this.state = Object.freeze({}); this.middleware = new Middleware(this); } @@ -738,11 +760,7 @@ function () { restoreProcessState(); } - updateComponents(_this); - - if (typeof callback === 'function') { - callback(); - } + updateComponents(_this, callback); }); } }, { @@ -827,7 +845,7 @@ function () { var patchs = diff(component.data[GLOBALWORD], createState(), GLOBALWORD); if (patchs.length > 0) { - applyPatchs(component, patchs); + applyPatchs(component, patchs, GLOBALWORD); } } } @@ -866,7 +884,7 @@ function () { return Store; }(); -var version = '0.0.9'; +var version = '0.0.10'; var nativePage = Page; var nativeComponent = Component; diff --git a/dist/mpstore.es6m.js b/dist/mpstore.es6m.js index 74e1e23..7927f13 100644 --- a/dist/mpstore.es6m.js +++ b/dist/mpstore.es6m.js @@ -257,22 +257,35 @@ function restore (obj, patchs) { return obj } -function applyPatchs (component, patchs) { +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); + component.setData(destObject, callback); } -function updateComponents (store) { +function updateComponents (store, callback) { + let total = 0; const { hooks, GLOBALWORD, depComponents, } = store; const len = depComponents.length; - if (len === 0) return + if (len === 0) { + if (typeof callback === 'function') { + callback(); + } + return + } + const renderedCallback = () => { + if (++total === len) { + if (typeof callback === 'function') { + callback(); + } + } + }; for (let i = 0; i < len; i++) { const { isPage, @@ -285,6 +298,7 @@ function updateComponents (store) { const newPartialState = createState(); if (typeof willUpdate === 'function') { if (willUpdate.call(store, component, newPartialState) === false) { + renderedCallback(); continue } } @@ -292,9 +306,10 @@ function updateComponents (store) { if (patchs.length > 0) { const params = [component, newPartialState, patchs, isPage]; if (callHook(hooks, 'willUpdate', params) === false) { + renderedCallback(); continue } - applyPatchs(component, patchs); + applyPatchs(component, patchs, renderedCallback); if (typeof didUpdate === 'function') { didUpdate.call(store, component, newPartialState, patchs); } @@ -302,7 +317,11 @@ function updateComponents (store) { if (component.timeTravel) { component.timeTravel.push(patchs); } + } else { + renderedCallback(); } + } else { + renderedCallback(); } } } @@ -504,7 +523,7 @@ class Store { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.0.9'; + this.version = '0.0.10'; this.state = Object.freeze({}); this.middleware = new Middleware(this); } @@ -548,10 +567,7 @@ class Store { this.isDispatching = false; restoreProcessState(); } - updateComponents(this); - if (typeof callback === 'function') { - callback(); - } + updateComponents(this, callback); }); } use (action, fn) { @@ -623,7 +639,7 @@ class Store { }); const patchs = diff(component.data[GLOBALWORD], createState(), GLOBALWORD); if (patchs.length > 0) { - applyPatchs(component, patchs); + applyPatchs(component, patchs, GLOBALWORD); } } } @@ -665,7 +681,7 @@ class Store { } } -const version = '0.0.9'; +const version = '0.0.10'; const nativePage = Page; const nativeComponent = Component; function expandConfig (config, expandMethods, isPage) { diff --git a/dist/mpstore.esm.js b/dist/mpstore.esm.js index 0c37e3c..f981bee 100644 --- a/dist/mpstore.esm.js +++ b/dist/mpstore.esm.js @@ -392,7 +392,7 @@ function restore(obj, patchs) { return obj; } -function applyPatchs(component, patchs) { +function applyPatchs(component, patchs, callback) { var destObject = {}; for (var i = 0, len = patchs.length; i < len; i++) { @@ -402,14 +402,30 @@ function applyPatchs(component, patchs) { destObject[path] = value; } - component.setData(destObject); + component.setData(destObject, callback); } -function updateComponents(store) { +function updateComponents(store, callback) { + var total = 0; var hooks = store.hooks, GLOBALWORD = store.GLOBALWORD, depComponents = store.depComponents; var len = depComponents.length; - if (len === 0) return; + + if (len === 0) { + if (typeof callback === 'function') { + callback(); + } + + return; + } + + var renderedCallback = function renderedCallback() { + if (++total === len) { + if (typeof callback === 'function') { + callback(); + } + } + }; for (var i = 0; i < len; i++) { var _depComponents$i = depComponents[i], @@ -424,6 +440,7 @@ function updateComponents(store) { if (typeof willUpdate === 'function') { if (willUpdate.call(store, component, newPartialState) === false) { + renderedCallback(); continue; } } @@ -434,10 +451,11 @@ function updateComponents(store) { var params = [component, newPartialState, patchs, isPage]; if (callHook(hooks, 'willUpdate', params) === false) { + renderedCallback(); continue; } - applyPatchs(component, patchs); + applyPatchs(component, patchs, renderedCallback); if (typeof didUpdate === 'function') { didUpdate.call(store, component, newPartialState, patchs); @@ -448,7 +466,11 @@ function updateComponents(store) { if (component.timeTravel) { component.timeTravel.push(patchs); } + } else { + renderedCallback(); } + } else { + renderedCallback(); } } } @@ -685,7 +707,7 @@ function () { this.depComponents = []; this.GLOBALWORD = 'global'; this.isDispatching = false; - this.version = '0.0.9'; + this.version = '0.0.10'; this.state = Object.freeze({}); this.middleware = new Middleware(this); } @@ -734,11 +756,7 @@ function () { restoreProcessState(); } - updateComponents(_this); - - if (typeof callback === 'function') { - callback(); - } + updateComponents(_this, callback); }); } }, { @@ -823,7 +841,7 @@ function () { var patchs = diff(component.data[GLOBALWORD], createState(), GLOBALWORD); if (patchs.length > 0) { - applyPatchs(component, patchs); + applyPatchs(component, patchs, GLOBALWORD); } } } @@ -862,7 +880,7 @@ function () { return Store; }(); -var version = '0.0.9'; +var version = '0.0.10'; var nativePage = Page; var nativeComponent = Component; diff --git a/dist/mpstore.min.js b/dist/mpstore.min.js index ac3f2fc..05dc09c 100644 --- a/dist/mpstore.min.js +++ b/dist/mpstore.min.js @@ -1 +1 @@ -"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var n=0;n-1&&t.splice(n,1)}function callHook(t,e,n){if(t&&"function"==typeof t[e])return t[e].apply(t,n)}function isEmptyObject(t){for(var e in t)return!1;return!0}function mapObject(t,e){var n={};for(var r in t)t.hasOwnProperty(r)&&(n[r]=e(t[r]));return n}function createWraper(t,e,n){return function(){for(var r,a=arguments.length,i=new Array(a),o=0;o1&&void 0!==arguments[1]?arguments[1]:new WeakMap;if(null==t||isPrimitive(t)||"function"==typeof t||t instanceof Date)return t;if(e.has(t))return e.get(t);var n="function"!=typeof t.constructor?Object.create(null):new t.constructor;for(var r in e.set(t,n),t)n[r]=clone(t[r],e);return n}function mixin(t){var e=Object.create(null);if("function"==typeof t){t((function(t,n){assert("string"==typeof t,"The mixed method name must a string."),assert("function"==typeof n,"The mixed method is not a function."),assert(!(t in e),'The "'.concat(t,'" is exist,')),e[t]=n}))}return e}Object.defineProperty(exports,"__esModule",{value:!0});var ADD=1,REMOVE=2,REPLACE=3;function Patch(t,e,n,r){this.type=t,this.path=e,this.value=n,this.leftValue=r}function diffValues(t,e,n,r){"function"==typeof t||null===t?r.push(new Patch(REPLACE,n,e,t)):Array.isArray(t)?Array.isArray(e)?walkArray(t,e,n,r):r.push(new Patch(REPLACE,n,e,t)):"object"===_typeof(t)?null===e||"object"!==_typeof(e)||Array.isArray(e)?r.push(new Patch(REPLACE,n,e,t)):t instanceof Date||e instanceof Date?r.push(new Patch(REPLACE,n,e,t)):walkObject(t,e,n,r):r.push(new Patch(REPLACE,n,e,t))}function walkArray(t,e,n,r){if(t.length<=e.length){for(var a=t.length;~--a;)if(t[a]!==e[a]){var i="".concat(n,"[").concat(a,"]");diffValues(t[a],e[a],i,r)}if(e.length>t.length)for(a=e.length;--a>=t.length;){var o="".concat(n,"[").concat(a,"]");r.push(new Patch(ADD,o,e[a],t[a]))}}else r.push(new Patch(REPLACE,n,e,t))}function walkObject(t,e,n,r){for(var a in t){var i="".concat(n,".").concat(a);a in e?t[a]!==e[a]&&diffValues(t[a],e[a],i,r):r.push(new Patch(REMOVE,i,null,t[a]))}for(var o in e)if(!(o in t)){var s="".concat(n,".").concat(o);r.push(new Patch(ADD,s,e[o],null))}}function diff(t,e,n){var r=[];return walkObject(t,e,n,r),r}var REG=/[^\[\].]+(?=[\[\].])/g;function separatePath(t,e){var n=e.match(REG);if(n&&n.shift()&&n.length>0){for(var r=-1,a=null,i=t,o=null;r++0){if(!1===callHook(e,"willUpdate",[c,h,p,s]))continue;applyPatchs(c,p),"function"==typeof l&&l.call(t,c,h,p),callHook(e,"didUpdate",[c,h,s]),c.timeTravel&&c.timeTravel.push(p)}}}}var TimeTravel=function(){function t(e,n,r){_classCallCheck(this,t),this.history=[],this.limit=r,this.component=e,this.GLOBALWORD=n,this.length=this.history.length,this.current=this.history.length,this.finallyState=e.data[n]}return _createClass(t,[{key:"push",value:function(t){var e=this.limit,n=this.history,r=this.GLOBALWORD,a=this.history.length,i=this.component.data;if(e>0){var o=a-e;o>=0&&this.history.splice(0,o+1),this.history.push(t),this.length=n.length,this.current=n.length,this.finallyState=clone(i[r])}}},{key:"go",value:function(t){var e=this.current,n=this.history,r=this.component,a=this.GLOBALWORD,i=this.finallyState;if(assert(a in r.data,"You can't use [timeTravel] because it only works for [global state]"),this.limit>0&&0!==t){var o=t+e,s=Math.abs(t);if(o<0||o>n.length)return void warning("Index [".concat(o,"] is not within the allowed range."),!0);for(var c=0,l=clone(r.data[a]);c++0&&applyPatchs(r,u),this.current+=t}}},{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)}}]),t}(),COMMONACTION=function(){};function match(t,e){return t.action===COMMONACTION||e===t.action}function handleLayer(t,e,n,r,a,i){try{e.call(n,r,a,t),i()}catch(e){var o=n.hooks;i(),o&&"function"==typeof o.middlewareError?o.middlewareError(t,r,e):warning("".concat(e,"\n\n --- from middleware [").concat(t,"] action."))}}var Middleware=function(){function t(e){_classCallCheck(this,t),this.stack=[],this.store=e,this.isProcessing=!1}return _createClass(t,[{key:"use",value:function(t,e){assert(!this.isProcessing,"can't allow add new middleware in the middleware processing."),this.stack.push({fn:e,action:t})}},{key:"remove",value:function(t,e){var n=this.stack.findIndex((function(n){return n.fn===e&&n.action===t}));n>-1&&this.stack.splice(n,1)}},{key:"process",value:function(t,e,n){var r=this;this.isProcessing=!0;var a=function(){r.isProcessing=!1};if(this.stack.length>0){var i=0;!function e(o){var s=r.stack[i];for(i++;s&&!match(s,t);)s=r.stack[i++];s?handleLayer(t,s.fn,r.store,o,e,a):n(o,a)}(e)}else n(e,a)}}]),t}(),storeId=0;function assertReducer(t,e,n){var r=n.setter,a=n.partialState;for(var i in assert("partialState"in n,"You must defined [partialState]."+"\n\n --- from [".concat(e,"] action.")),assert(isPlainObject(a),"The [partialState] must be an object."+"\n\n --- from [".concat(e,"] action.")),a)assert(!t.hasOwnProperty(i),"The [".concat(i,"] already exists in global state, ")+"Please don't repeat defined. \n\n --- from [".concat(e,"] action."));return"function"!=typeof r&&(n.setter=function(){throw"Can't changed [".concat(e,"] action value. Have you defined a setter?")}),n}var Store=function(){function t(e){_classCallCheck(this,t),this.hooks=e,this.reducers=[],this.id=++storeId,this.depComponents=[],this.GLOBALWORD="global",this.isDispatching=!1,this.version="0.0.9",this.state=Object.freeze({}),this.middleware=new Middleware(this)}return _createClass(t,[{key:"add",value:function(t,e){assert(!this.reducers.find((function(e){return e.action===t})),"Can't repeat defined [".concat(t,"] action."));var n=assertReducer(this.state,t,e).partialState;e.action=t,this.reducers.push(e),isEmptyObject(n)||(this.state=mergeState(this.state,n))}},{key:"dispatch",value:function(t,e,n){var r=this,a=this.reducers;assert(!this.isDispatching,'It is not allowed to call "dispatch" during dispatch execution.'+"\n\n --- from [".concat(t,"] action."));var i=a.find((function(e){return e.action===t}));assert(i,"The [".concat(t,"] action does not exist. ")+"Maybe you have not defined."),this.middleware.process(t,e,(function(t,e){r.isDispatching=!0;try{var a=i.setter(r.state,t);assert(isPlainObject(a),"setter function should be return a plain object."),isEmptyObject(a)||(r.state=mergeState(r.state,a))}finally{r.isDispatching=!1,e()}updateComponents(r),"function"==typeof n&&n()}))}},{key:"use",value:function(t,e){var n=this;return"function"==typeof t&&t!==COMMONACTION&&(e=t,t=COMMONACTION),this.middleware.use(t,e),function(){return n.middleware.remove(t,e)}}},{key:"setNamespace",value:function(t){assert(t&&"string"==typeof t,"The [namespace] must be a string"),this.GLOBALWORD=t}},{key:"rewirteCfgAndAddDep",value:function(t,e){var n=this,r=null,a=this,i=this.GLOBALWORD,o=t.data,s=t.storeConfig,c=void 0===s?{}:s,l=c.useState,f=c.didUpdate,u=c.willUpdate,h=c.defineReducer,p=c.travelLimit,d=void 0===p?0:p;if(assert("number"==typeof d,"[travelLimit] must be a number, but now is [".concat(_typeof(d),"].")),delete t.storeConfig,"function"==typeof h&&h.call(a,a),"function"==typeof l){var y=l.call(a,a);assert(isPlainObject(y),"[useState] must return a plain object, "+"but now is return a [".concat(_typeof(y),"]")),r=function(){return clone(mapObject(y,(function(t){return t(a.state)})))}}if(null!==r){var v=r();isPlainObject(v)&&(o?o[i]=v:t.data=_defineProperty({},i,v))}var m=function(t){if(!1!==callHook(n.hooks,"addDep",[t,e])&&null!==r&&t.data&&isPlainObject(t.data[i])){t.timeTravel=new TimeTravel(t,i,d),n.depComponents.push({isPage:e,component:t,didUpdate:f,willUpdate:u,createState:r});var a=diff(t.data[i],r(),i);a.length>0&&applyPatchs(t,a)}};if(e)t.onLoad=createWraper(t.onLoad,(function(){m(this),this.store=a})),t.onUnload=createWraper(t.onUnload,null,(function(){remove(a.depComponents,this)}));else{t.lifetimes=t.lifetimes||{};var g=function(e){return t[e]||t.lifetimes[e]},b=function(e,n){return t[e]=t.lifetimes[e]=n};b("attached",createWraper(g("attached"),(function(){m(this),this.store=a}))),b("detached",createWraper(g("detached"),null,(function(){remove(a.depComponents,this)})))}}}]),t}(),version="0.0.9",nativePage=Page,nativeComponent=Component;function expandConfig(t,e,n){isEmptyObject(e)||(n?mixinMethods(t,e):(t.methods=t.methods||{},mixinMethods(t.methods,e)))}function index(t,e){var n=new Store(e),r=mixin(t);return Page=createWraper(nativePage,(function(t){callHook(e,"createBefore",[t,!0]),expandConfig(t,r,!0),n.rewirteCfgAndAddDep(t,!0)})),Component=createWraper(nativeComponent,(function(t){callHook(e,"createBefore",[t,!1]),expandConfig(t,r,!1),n.rewirteCfgAndAddDep(t,!1)})),n}exports.clone=clone,exports.default=index,exports.diff=diff,exports.restore=restore,exports.version=version; +"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var n=0;n-1&&t.splice(n,1)}function callHook(t,e,n){if(t&&"function"==typeof t[e])return t[e].apply(t,n)}function isEmptyObject(t){for(var e in t)return!1;return!0}function mapObject(t,e){var n={};for(var r in t)t.hasOwnProperty(r)&&(n[r]=e(t[r]));return n}function createWraper(t,e,n){return function(){for(var r,a=arguments.length,i=new Array(a),o=0;o1&&void 0!==arguments[1]?arguments[1]:new WeakMap;if(null==t||isPrimitive(t)||"function"==typeof t||t instanceof Date)return t;if(e.has(t))return e.get(t);var n="function"!=typeof t.constructor?Object.create(null):new t.constructor;for(var r in e.set(t,n),t)n[r]=clone(t[r],e);return n}function mixin(t){var e=Object.create(null);if("function"==typeof t){t((function(t,n){assert("string"==typeof t,"The mixed method name must a string."),assert("function"==typeof n,"The mixed method is not a function."),assert(!(t in e),'The "'.concat(t,'" is exist,')),e[t]=n}))}return e}Object.defineProperty(exports,"__esModule",{value:!0});var ADD=1,REMOVE=2,REPLACE=3;function Patch(t,e,n,r){this.type=t,this.path=e,this.value=n,this.leftValue=r}function diffValues(t,e,n,r){"function"==typeof t||null===t?r.push(new Patch(REPLACE,n,e,t)):Array.isArray(t)?Array.isArray(e)?walkArray(t,e,n,r):r.push(new Patch(REPLACE,n,e,t)):"object"===_typeof(t)?null===e||"object"!==_typeof(e)||Array.isArray(e)?r.push(new Patch(REPLACE,n,e,t)):t instanceof Date||e instanceof Date?r.push(new Patch(REPLACE,n,e,t)):walkObject(t,e,n,r):r.push(new Patch(REPLACE,n,e,t))}function walkArray(t,e,n,r){if(t.length<=e.length){for(var a=t.length;~--a;)if(t[a]!==e[a]){var i="".concat(n,"[").concat(a,"]");diffValues(t[a],e[a],i,r)}if(e.length>t.length)for(a=e.length;--a>=t.length;){var o="".concat(n,"[").concat(a,"]");r.push(new Patch(ADD,o,e[a],t[a]))}}else r.push(new Patch(REPLACE,n,e,t))}function walkObject(t,e,n,r){for(var a in t){var i="".concat(n,".").concat(a);a in e?t[a]!==e[a]&&diffValues(t[a],e[a],i,r):r.push(new Patch(REMOVE,i,null,t[a]))}for(var o in e)if(!(o in t)){var s="".concat(n,".").concat(o);r.push(new Patch(ADD,s,e[o],null))}}function diff(t,e,n){var r=[];return walkObject(t,e,n,r),r}var REG=/[^\[\].]+(?=[\[\].])/g;function separatePath(t,e){var n=e.match(REG);if(n&&n.shift()&&n.length>0){for(var r=-1,a=null,i=t,o=null;r++0){if(!1===callHook(r,"willUpdate",[u,y,v,f])){s();continue}applyPatchs(u,v,s),"function"==typeof h&&h.call(t,u,y,v),callHook(r,"didUpdate",[u,y,f]),u.timeTravel&&u.timeTravel.push(v)}else s()}else s()}else"function"==typeof e&&e()}var TimeTravel=function(){function t(e,n,r){_classCallCheck(this,t),this.history=[],this.limit=r,this.component=e,this.GLOBALWORD=n,this.length=this.history.length,this.current=this.history.length,this.finallyState=e.data[n]}return _createClass(t,[{key:"push",value:function(t){var e=this.limit,n=this.history,r=this.GLOBALWORD,a=this.history.length,i=this.component.data;if(e>0){var o=a-e;o>=0&&this.history.splice(0,o+1),this.history.push(t),this.length=n.length,this.current=n.length,this.finallyState=clone(i[r])}}},{key:"go",value:function(t){var e=this.current,n=this.history,r=this.component,a=this.GLOBALWORD,i=this.finallyState;if(assert(a in r.data,"You can't use [timeTravel] because it only works for [global state]"),this.limit>0&&0!==t){var o=t+e,s=Math.abs(t);if(o<0||o>n.length)return void warning("Index [".concat(o,"] is not within the allowed range."),!0);for(var c=0,l=clone(r.data[a]);c++0&&applyPatchs(r,u),this.current+=t}}},{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)}}]),t}(),COMMONACTION=function(){};function match(t,e){return t.action===COMMONACTION||e===t.action}function handleLayer(t,e,n,r,a,i){try{e.call(n,r,a,t),i()}catch(e){var o=n.hooks;i(),o&&"function"==typeof o.middlewareError?o.middlewareError(t,r,e):warning("".concat(e,"\n\n --- from middleware [").concat(t,"] action."))}}var Middleware=function(){function t(e){_classCallCheck(this,t),this.stack=[],this.store=e,this.isProcessing=!1}return _createClass(t,[{key:"use",value:function(t,e){assert(!this.isProcessing,"can't allow add new middleware in the middleware processing."),this.stack.push({fn:e,action:t})}},{key:"remove",value:function(t,e){var n=this.stack.findIndex((function(n){return n.fn===e&&n.action===t}));n>-1&&this.stack.splice(n,1)}},{key:"process",value:function(t,e,n){var r=this;this.isProcessing=!0;var a=function(){r.isProcessing=!1};if(this.stack.length>0){var i=0;!function e(o){var s=r.stack[i];for(i++;s&&!match(s,t);)s=r.stack[i++];s?handleLayer(t,s.fn,r.store,o,e,a):n(o,a)}(e)}else n(e,a)}}]),t}(),storeId=0;function assertReducer(t,e,n){var r=n.setter,a=n.partialState;for(var i in assert("partialState"in n,"You must defined [partialState]."+"\n\n --- from [".concat(e,"] action.")),assert(isPlainObject(a),"The [partialState] must be an object."+"\n\n --- from [".concat(e,"] action.")),a)assert(!t.hasOwnProperty(i),"The [".concat(i,"] already exists in global state, ")+"Please don't repeat defined. \n\n --- from [".concat(e,"] action."));return"function"!=typeof r&&(n.setter=function(){throw"Can't changed [".concat(e,"] action value. Have you defined a setter?")}),n}var Store=function(){function t(e){_classCallCheck(this,t),this.hooks=e,this.reducers=[],this.id=++storeId,this.depComponents=[],this.GLOBALWORD="global",this.isDispatching=!1,this.version="0.0.10",this.state=Object.freeze({}),this.middleware=new Middleware(this)}return _createClass(t,[{key:"add",value:function(t,e){assert(!this.reducers.find((function(e){return e.action===t})),"Can't repeat defined [".concat(t,"] action."));var n=assertReducer(this.state,t,e).partialState;e.action=t,this.reducers.push(e),isEmptyObject(n)||(this.state=mergeState(this.state,n))}},{key:"dispatch",value:function(t,e,n){var r=this,a=this.reducers;assert(!this.isDispatching,'It is not allowed to call "dispatch" during dispatch execution.'+"\n\n --- from [".concat(t,"] action."));var i=a.find((function(e){return e.action===t}));assert(i,"The [".concat(t,"] action does not exist. ")+"Maybe you have not defined."),this.middleware.process(t,e,(function(t,e){r.isDispatching=!0;try{var a=i.setter(r.state,t);assert(isPlainObject(a),"setter function should be return a plain object."),isEmptyObject(a)||(r.state=mergeState(r.state,a))}finally{r.isDispatching=!1,e()}updateComponents(r,n)}))}},{key:"use",value:function(t,e){var n=this;return"function"==typeof t&&t!==COMMONACTION&&(e=t,t=COMMONACTION),this.middleware.use(t,e),function(){return n.middleware.remove(t,e)}}},{key:"setNamespace",value:function(t){assert(t&&"string"==typeof t,"The [namespace] must be a string"),this.GLOBALWORD=t}},{key:"rewirteCfgAndAddDep",value:function(t,e){var n=this,r=null,a=this,i=this.GLOBALWORD,o=t.data,s=t.storeConfig,c=void 0===s?{}:s,l=c.useState,f=c.didUpdate,u=c.willUpdate,h=c.defineReducer,p=c.travelLimit,d=void 0===p?0:p;if(assert("number"==typeof d,"[travelLimit] must be a number, but now is [".concat(_typeof(d),"].")),delete t.storeConfig,"function"==typeof h&&h.call(a,a),"function"==typeof l){var y=l.call(a,a);assert(isPlainObject(y),"[useState] must return a plain object, "+"but now is return a [".concat(_typeof(y),"]")),r=function(){return clone(mapObject(y,(function(t){return t(a.state)})))}}if(null!==r){var v=r();isPlainObject(v)&&(o?o[i]=v:t.data=_defineProperty({},i,v))}var m=function(t){if(!1!==callHook(n.hooks,"addDep",[t,e])&&null!==r&&t.data&&isPlainObject(t.data[i])){t.timeTravel=new TimeTravel(t,i,d),n.depComponents.push({isPage:e,component:t,didUpdate:f,willUpdate:u,createState:r});var a=diff(t.data[i],r(),i);a.length>0&&applyPatchs(t,a,i)}};if(e)t.onLoad=createWraper(t.onLoad,(function(){m(this),this.store=a})),t.onUnload=createWraper(t.onUnload,null,(function(){remove(a.depComponents,this)}));else{t.lifetimes=t.lifetimes||{};var g=function(e){return t[e]||t.lifetimes[e]},b=function(e,n){return t[e]=t.lifetimes[e]=n};b("attached",createWraper(g("attached"),(function(){m(this),this.store=a}))),b("detached",createWraper(g("detached"),null,(function(){remove(a.depComponents,this)})))}}}]),t}(),version="0.0.10",nativePage=Page,nativeComponent=Component;function expandConfig(t,e,n){isEmptyObject(e)||(n?mixinMethods(t,e):(t.methods=t.methods||{},mixinMethods(t.methods,e)))}function index(t,e){var n=new Store(e),r=mixin(t);return Page=createWraper(nativePage,(function(t){callHook(e,"createBefore",[t,!0]),expandConfig(t,r,!0),n.rewirteCfgAndAddDep(t,!0)})),Component=createWraper(nativeComponent,(function(t){callHook(e,"createBefore",[t,!1]),expandConfig(t,r,!1),n.rewirteCfgAndAddDep(t,!1)})),n}exports.clone=clone,exports.default=index,exports.diff=diff,exports.restore=restore,exports.version=version; diff --git a/package.json b/package.json index bacdfdc..afb1288 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rustle/mp-store", - "version": "0.0.9", + "version": "0.0.10", "description": "A lightweight wechat miniprogram state management library", "main": "dist/mpstore.common.js", "module": "dist/mpstore.esm.js",