From 7f1befc5734d51da2cd82c15e4c5abce66aa1c2b Mon Sep 17 00:00:00 2001 From: jinker Date: Sat, 9 Dec 2017 21:45:32 +0800 Subject: [PATCH 1/3] mapState bind this of Page --- dist/wechat-weapp-redux.js | 20 ++++++++++---------- dist/wechat-weapp-redux.min.js | 2 +- src/connect.js | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/dist/wechat-weapp-redux.js b/dist/wechat-weapp-redux.js index fb3062b..dfecb44 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,9 +166,9 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = Provider; -/***/ }, +/***/ }), /* 4 */ -/***/ function(module, exports, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { 'use strict'; @@ -217,7 +217,7 @@ return /******/ (function(modules) { // webpackBootstrap } var state = this.store.getState(); - var mappedState = mapState(state, options); + var mappedState = mapState.bind(this)(state, options); if (!this.data || (0, _shallowEqual2.default)(this.data, mappedState)) { return; } diff --git a/dist/wechat-weapp-redux.min.js b/dist/wechat-weapp-redux.min.js index 16bd46e..ea10bb5 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),s=o(u);t.exports={Provider:i.default,connect:s.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,s.default)("Store似乎不是一个合法的Redux Store对象: 缺少这些方法: "+n.join(", ")+"。")}function i(t){return r(t),function(n){return(0,f.assign)({},n,{store:t})}}var u=e(2),s=o(u),f=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);this.data&&!(0,u.default)(this.data,e)&&this.setData(e)}}function s(t){this.store=r.store,this.store||(0,f.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:s,onUnload:c})}}var i=e(5),u=o(i),s=e(2),f=o(s),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(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 s=o[u],f=t[s];"function"==typeof f&&(r[s]=e(f,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..7909baa 100644 --- a/src/connect.js +++ b/src/connect.js @@ -28,7 +28,7 @@ function connect(mapStateToProps, mapDispatchToProps) { } const state = this.store.getState() - const mappedState = mapState(state, options); + const mappedState = mapState.bind(this)(state, options); if (!this.data || shallowEqual(this.data, mappedState)) { return; } From 6fd724d41d3beff3a8635450f2f4d4277015ba7d Mon Sep 17 00:00:00 2001 From: jinker Date: Wed, 13 Dec 2017 11:03:04 +0800 Subject: [PATCH 2/3] optimization: diff mapState and current data, and only setData diff --- dist/wechat-weapp-redux.js | 32 ++++++++++++------- dist/wechat-weapp-redux.min.js | 2 +- src/connect.js | 7 ++-- ...shallowEqual.js => shallowEqualAndDiff.js} | 15 ++++++--- 4 files changed, 36 insertions(+), 20 deletions(-) rename src/{shallowEqual.js => shallowEqualAndDiff.js} (61%) diff --git a/dist/wechat-weapp-redux.js b/dist/wechat-weapp-redux.js index dfecb44..28f006c 100644 --- a/dist/wechat-weapp-redux.js +++ b/dist/wechat-weapp-redux.js @@ -172,9 +172,9 @@ return /******/ (function(modules) { // webpackBootstrap '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); @@ -218,10 +218,11 @@ return /******/ (function(modules) { // webpackBootstrap var state = this.store.getState(); var mappedState = mapState.bind(this)(state, options); - if (!this.data || (0, _shallowEqual2.default)(this.data, mappedState)) { + 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,9 +256,9 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = connect; -/***/ }, +/***/ }), /* 5 */ -/***/ function(module, exports) { +/***/ (function(module, exports) { "use strict"; @@ -273,22 +274,29 @@ return /******/ (function(modules) { // webpackBootstrap return false; } + var equal = true; + var diffAByB = {}; // 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; -/***/ }, +/***/ }), /* 6 */ -/***/ function(module, exports) { +/***/ (function(module, exports) { 'use strict'; @@ -329,7 +337,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 ea10bb5..8ffca98 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),s=o(u);t.exports={Provider:i.default,connect:s.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,s.default)("Store似乎不是一个合法的Redux Store对象: 缺少这些方法: "+n.join(", ")+"。")}function i(t){return r(t),function(n){return(0,f.assign)({},n,{store:t})}}var u=e(2),s=o(u),f=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);this.data&&!(0,u.default)(this.data,e)&&this.setData(e)}}function s(t){this.store=r.store,this.store||(0,f.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:s,onUnload:c})}}var i=e(5),u=o(i),s=e(2),f=o(s),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(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 s=o[u],f=t[s];"function"==typeof f&&(r[s]=e(f,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(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=!0,i={},u=Object.prototype.hasOwnProperty,f=0;e.length>f;f++){var s=e[f];u.call(n,s)&&t[s]===n[s]||(i[s]=t[s],r=!1)}return{equal:r,diff:i}}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 7909baa..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' @@ -29,10 +29,11 @@ function connect(mapStateToProps, mapDispatchToProps) { const state = this.store.getState() const mappedState = mapState.bind(this)(state, options); - if (!this.data || shallowEqual(this.data, mappedState)) { + 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/shallowEqualAndDiff.js similarity index 61% rename from src/shallowEqual.js rename to src/shallowEqualAndDiff.js index 040c7a4..9a36bce 100644 --- a/src/shallowEqual.js +++ b/src/shallowEqualAndDiff.js @@ -10,16 +10,23 @@ function shallowEqual(objA, objB) { return false } + let equal = true; + const diffAByB = {}; // 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 + const keyA = keysA[i]; + if (!hasOwn.call(objB, keyA) || + objA[keyA] !== objB[keyA]) { + diffAByB[keyA] = objA[keyA] + equal = false; } } - return true + return { + equal, + diff: diffAByB + } } module.exports = shallowEqual \ No newline at end of file From 04c91439bb144ab3b864e32c57c47e40c4deafd7 Mon Sep 17 00:00:00 2001 From: jinker Date: Wed, 13 Dec 2017 11:18:05 +0800 Subject: [PATCH 3/3] optimization: diff mapState and current data, and only setData diff --- dist/wechat-weapp-redux.js | 20 +++++++++++--------- dist/wechat-weapp-redux.min.js | 2 +- src/shallowEqualAndDiff.js | 17 ++++++++--------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/dist/wechat-weapp-redux.js b/dist/wechat-weapp-redux.js index 28f006c..c2d64c8 100644 --- a/dist/wechat-weapp-redux.js +++ b/dist/wechat-weapp-redux.js @@ -262,20 +262,22 @@ return /******/ (function(modules) { // webpackBootstrap "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; - var diffAByB = {}; // Test for A's keys different from B. var hasOwn = Object.prototype.hasOwnProperty; for (var i = 0; i < keysA.length; i++) { @@ -292,7 +294,7 @@ return /******/ (function(modules) { // webpackBootstrap }; } - module.exports = shallowEqual; + module.exports = shallowEqualAndDiff; /***/ }), /* 6 */ diff --git a/dist/wechat-weapp-redux.min.js b/dist/wechat-weapp-redux.min.js index 8ffca98..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||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(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=!0,i={},u=Object.prototype.hasOwnProperty,f=0;e.length>f;f++){var s=e[f];u.call(n,s)&&t[s]===n[s]||(i[s]=t[s],r=!1)}return{equal:r,diff:i}}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/shallowEqualAndDiff.js b/src/shallowEqualAndDiff.js index 9a36bce..07202d6 100644 --- a/src/shallowEqualAndDiff.js +++ b/src/shallowEqualAndDiff.js @@ -1,17 +1,16 @@ -function shallowEqual(objA, objB) { +function shallowEqualAndDiff(objA = {}, objB = {}) { + const diffAByB = {}; + if (objA === objB) { - return true + return { + equal: true, + diff: diffAByB + } } const keysA = Object.keys(objA) - const keysB = Object.keys(objB) - - if (keysA.length !== keysB.length) { - return false - } let equal = true; - const diffAByB = {}; // Test for A's keys different from B. const hasOwn = Object.prototype.hasOwnProperty for (let i = 0; i < keysA.length; i++) { @@ -29,4 +28,4 @@ function shallowEqual(objA, objB) { } } -module.exports = shallowEqual \ No newline at end of file +module.exports = shallowEqualAndDiff \ No newline at end of file