diff --git a/dist/wechat-weapp-redux.js b/dist/wechat-weapp-redux.js index fb3062b..c2d64c8 100644 --- a/dist/wechat-weapp-redux.js +++ b/dist/wechat-weapp-redux.js @@ -52,7 +52,7 @@ return /******/ (function(modules) { // webpackBootstrap /************************************************************************/ /******/ ([ /* 0 */ -/***/ function(module, exports, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { 'use strict'; @@ -71,9 +71,9 @@ return /******/ (function(modules) { // webpackBootstrap connect: _connect2.default }; -/***/ }, +/***/ }), /* 1 */ -/***/ function(module, exports) { +/***/ (function(module, exports) { 'use strict'; @@ -103,9 +103,9 @@ return /******/ (function(modules) { // webpackBootstrap assign: assign }; -/***/ }, +/***/ }), /* 2 */ -/***/ function(module, exports) { +/***/ (function(module, exports) { 'use strict'; @@ -133,9 +133,9 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = warning; -/***/ }, +/***/ }), /* 3 */ -/***/ function(module, exports, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { 'use strict'; @@ -166,15 +166,15 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = Provider; -/***/ }, +/***/ }), /* 4 */ -/***/ function(module, exports, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { 'use strict'; - var _shallowEqual = __webpack_require__(5); + var _shallowEqualAndDiff = __webpack_require__(5); - var _shallowEqual2 = _interopRequireDefault(_shallowEqual); + var _shallowEqualAndDiff2 = _interopRequireDefault(_shallowEqualAndDiff); var _warning = __webpack_require__(2); @@ -217,11 +217,12 @@ return /******/ (function(modules) { // webpackBootstrap } var state = this.store.getState(); - var mappedState = mapState(state, options); - if (!this.data || (0, _shallowEqual2.default)(this.data, mappedState)) { + var mappedState = mapState.bind(this)(state, options); + var shallowEqualRes = (0, _shallowEqualAndDiff2.default)(mappedState, this.data); + if (!this.data || shallowEqualRes.equal) { return; } - this.setData(mappedState); + this.setData(shallowEqualRes.diff); } var _onLoad = pageConfig.onLoad, @@ -255,40 +256,49 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = connect; -/***/ }, +/***/ }), /* 5 */ -/***/ function(module, exports) { +/***/ (function(module, exports) { "use strict"; - function shallowEqual(objA, objB) { + function shallowEqualAndDiff() { + var objA = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + var objB = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + var diffAByB = {}; + if (objA === objB) { - return true; + return { + equal: true, + diff: diffAByB + }; } var keysA = Object.keys(objA); - var keysB = Object.keys(objB); - - if (keysA.length !== keysB.length) { - return false; - } + var equal = true; // Test for A's keys different from B. var hasOwn = Object.prototype.hasOwnProperty; for (var i = 0; i < keysA.length; i++) { - if (!hasOwn.call(objB, keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) { - return false; + var keyA = keysA[i]; + if (!hasOwn.call(objB, keyA) || objA[keyA] !== objB[keyA]) { + diffAByB[keyA] = objA[keyA]; + equal = false; } } - return true; + return { + equal: equal, + diff: diffAByB + }; } - module.exports = shallowEqual; + module.exports = shallowEqualAndDiff; -/***/ }, +/***/ }), /* 6 */ -/***/ function(module, exports) { +/***/ (function(module, exports) { 'use strict'; @@ -329,7 +339,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = wrapActionCreators; -/***/ } +/***/ }) /******/ ]) }); ; \ No newline at end of file diff --git a/dist/wechat-weapp-redux.min.js b/dist/wechat-weapp-redux.min.js index 16bd46e..c115c3b 100644 --- a/dist/wechat-weapp-redux.min.js +++ b/dist/wechat-weapp-redux.min.js @@ -1 +1 @@ -!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.WeAppRedux=n():t.WeAppRedux=n()}(this,function(){return function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,n),r.loaded=!0,r.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}var r=e(3),i=o(r),u=e(4),f=o(u);t.exports={Provider:i.default,connect:f.default}},function(t,n){"use strict";var e=function(t){if(void 0===t||null===t)throw new TypeError("Cannot convert undefined or null to object");for(var n=Object(t),e=1;arguments.length>e;e++){var o=arguments[e];if(void 0!==o&&null!==o)for(var r in o)o.hasOwnProperty(r)&&(n[r]=o[r])}return n};t.exports={assign:e}},function(t,n){"use strict";function e(t){"undefined"!=typeof console&&"function"==typeof console.error&&console.error(t);try{throw Error(t)}catch(t){}}t.exports=e},function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t){var n=["subscribe","dispatch","getState"].filter(function(n){return!t.hasOwnProperty(n)});n.length>0&&(0,f.default)("Store似乎不是一个合法的Redux Store对象: 缺少这些方法: "+n.join(", ")+"。")}function i(t){return r(t),function(n){return(0,s.assign)({},n,{store:t})}}var u=e(2),f=o(u),s=e(1);t.exports=i},function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,n){var e=!!t,o=t||d,r=getApp(),i=void 0;return i="function"==typeof n?n:n?(0,a.default)(n):p,function(t){function n(t){if(this.unsubscribe){var n=this.store.getState(),e=o(n,t);this.data&&!(0,u.default)(this.data,e)&&this.setData(e)}}function f(t){this.store=r.store,this.store||(0,s.default)("Store对象不存在!"),e&&(this.unsubscribe=this.store.subscribe(n.bind(this,t)),n.call(this,t)),"function"==typeof a&&a.call(this,t)}function c(){"function"==typeof d&&d.call(this),"function"==typeof this.unsubscribe&&this.unsubscribe()}var a=t.onLoad,d=t.onUnload;return(0,l.assign)({},t,i(r.store.dispatch),{onLoad:f,onUnload:c})}}var i=e(5),u=o(i),f=e(2),s=o(f),c=e(6),a=o(c),l=e(1),d=function(t){return{}},p=function(t){return{dispatch:t}};t.exports=r},function(t,n){"use strict";function e(t,n){if(t===n)return!0;var e=Object.keys(t),o=Object.keys(n);if(e.length!==o.length)return!1;for(var r=Object.prototype.hasOwnProperty,i=0;e.length>i;i++)if(!r.call(n,e[i])||t[e[i]]!==n[e[i]])return!1;return!0}t.exports=e},function(t,n){"use strict";function e(t,n){return function(){return n(t.apply(void 0,arguments))}}function o(t,n){if("function"==typeof t)return e(t,n);if("object"!==(void 0===t?"undefined":i(t))||null===t)throw Error("bindActionCreators expected an object or a function, instead received "+(null===t?"null":void 0===t?"undefined":i(t))+'. Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?');for(var o=Object.keys(t),r={},u=0;o.length>u;u++){var f=o[u],s=t[f];"function"==typeof s&&(r[f]=e(s,n))}return r}function r(t){return function(n){return o(t,n)}}var i="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.exports=r}])}); \ No newline at end of file +!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.WeAppRedux=n():t.WeAppRedux=n()}(this,function(){return function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,n),r.loaded=!0,r.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}var r=e(3),i=o(r),u=e(4),f=o(u);t.exports={Provider:i.default,connect:f.default}},function(t,n){"use strict";var e=function(t){if(void 0===t||null===t)throw new TypeError("Cannot convert undefined or null to object");for(var n=Object(t),e=1;arguments.length>e;e++){var o=arguments[e];if(void 0!==o&&null!==o)for(var r in o)o.hasOwnProperty(r)&&(n[r]=o[r])}return n};t.exports={assign:e}},function(t,n){"use strict";function e(t){"undefined"!=typeof console&&"function"==typeof console.error&&console.error(t);try{throw Error(t)}catch(t){}}t.exports=e},function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t){var n=["subscribe","dispatch","getState"].filter(function(n){return!t.hasOwnProperty(n)});n.length>0&&(0,f.default)("Store似乎不是一个合法的Redux Store对象: 缺少这些方法: "+n.join(", ")+"。")}function i(t){return r(t),function(n){return(0,s.assign)({},n,{store:t})}}var u=e(2),f=o(u),s=e(1);t.exports=i},function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,n){var e=!!t,o=t||l,r=getApp(),i=void 0;return i="function"==typeof n?n:n?(0,a.default)(n):p,function(t){function n(t){if(this.unsubscribe){var n=this.store.getState(),e=o.bind(this)(n,t),r=(0,u.default)(e,this.data);this.data&&!r.equal&&this.setData(r.diff)}}function f(t){this.store=r.store,this.store||(0,s.default)("Store对象不存在!"),e&&(this.unsubscribe=this.store.subscribe(n.bind(this,t)),n.call(this,t)),"function"==typeof a&&a.call(this,t)}function c(){"function"==typeof l&&l.call(this),"function"==typeof this.unsubscribe&&this.unsubscribe()}var a=t.onLoad,l=t.onUnload;return(0,d.assign)({},t,i(r.store.dispatch),{onLoad:f,onUnload:c})}}var i=e(5),u=o(i),f=e(2),s=o(f),c=e(6),a=o(c),d=e(1),l=function(t){return{}},p=function(t){return{dispatch:t}};t.exports=r},function(t,n){"use strict";function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e={};if(t===n)return{equal:!0,diff:e};for(var o=Object.keys(t),r=!0,i=Object.prototype.hasOwnProperty,u=0;o.length>u;u++){var f=o[u];i.call(n,f)&&t[f]===n[f]||(e[f]=t[f],r=!1)}return{equal:r,diff:e}}t.exports=e},function(t,n){"use strict";function e(t,n){return function(){return n(t.apply(void 0,arguments))}}function o(t,n){if("function"==typeof t)return e(t,n);if("object"!==(void 0===t?"undefined":i(t))||null===t)throw Error("bindActionCreators expected an object or a function, instead received "+(null===t?"null":void 0===t?"undefined":i(t))+'. Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?');for(var o=Object.keys(t),r={},u=0;o.length>u;u++){var f=o[u],s=t[f];"function"==typeof s&&(r[f]=e(s,n))}return r}function r(t){return function(n){return o(t,n)}}var i="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.exports=r}])}); \ No newline at end of file diff --git a/src/connect.js b/src/connect.js index 812c706..2f29b4c 100644 --- a/src/connect.js +++ b/src/connect.js @@ -1,4 +1,4 @@ -import shallowEqual from './shallowEqual.js' +import shallowEqualAndDiff from './shallowEqualAndDiff.js' import warning from './warning.js' import wrapActionCreators from './wrapActionCreators.js' import {assign} from './utils/Object.js' @@ -28,11 +28,12 @@ function connect(mapStateToProps, mapDispatchToProps) { } const state = this.store.getState() - const mappedState = mapState(state, options); - if (!this.data || shallowEqual(this.data, mappedState)) { + const mappedState = mapState.bind(this)(state, options); + const shallowEqualRes = shallowEqualAndDiff(mappedState, this.data); + if (!this.data || shallowEqualRes.equal) { return; } - this.setData(mappedState) + this.setData(shallowEqualRes.diff) } const { diff --git a/src/shallowEqual.js b/src/shallowEqual.js deleted file mode 100644 index 040c7a4..0000000 --- a/src/shallowEqual.js +++ /dev/null @@ -1,25 +0,0 @@ -function shallowEqual(objA, objB) { - if (objA === objB) { - return true - } - - const keysA = Object.keys(objA) - const keysB = Object.keys(objB) - - if (keysA.length !== keysB.length) { - return false - } - - // Test for A's keys different from B. - const hasOwn = Object.prototype.hasOwnProperty - for (let i = 0; i < keysA.length; i++) { - if (!hasOwn.call(objB, keysA[i]) || - objA[keysA[i]] !== objB[keysA[i]]) { - return false - } - } - - return true -} - -module.exports = shallowEqual \ No newline at end of file diff --git a/src/shallowEqualAndDiff.js b/src/shallowEqualAndDiff.js new file mode 100644 index 0000000..07202d6 --- /dev/null +++ b/src/shallowEqualAndDiff.js @@ -0,0 +1,31 @@ +function shallowEqualAndDiff(objA = {}, objB = {}) { + const diffAByB = {}; + + if (objA === objB) { + return { + equal: true, + diff: diffAByB + } + } + + const keysA = Object.keys(objA) + + let equal = true; + // Test for A's keys different from B. + const hasOwn = Object.prototype.hasOwnProperty + for (let i = 0; i < keysA.length; i++) { + const keyA = keysA[i]; + if (!hasOwn.call(objB, keyA) || + objA[keyA] !== objB[keyA]) { + diffAByB[keyA] = objA[keyA] + equal = false; + } + } + + return { + equal, + diff: diffAByB + } +} + +module.exports = shallowEqualAndDiff \ No newline at end of file