Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapState bind this of Page #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions dist/wechat-weapp-redux.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ return /******/ (function(modules) { // webpackBootstrap
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
/***/ (function(module, exports, __webpack_require__) {

'use strict';

Expand All @@ -71,9 +71,9 @@ return /******/ (function(modules) { // webpackBootstrap
connect: _connect2.default
};

/***/ },
/***/ }),
/* 1 */
/***/ function(module, exports) {
/***/ (function(module, exports) {

'use strict';

Expand Down Expand Up @@ -103,9 +103,9 @@ return /******/ (function(modules) { // webpackBootstrap
assign: assign
};

/***/ },
/***/ }),
/* 2 */
/***/ function(module, exports) {
/***/ (function(module, exports) {

'use strict';

Expand Down Expand Up @@ -133,9 +133,9 @@ return /******/ (function(modules) { // webpackBootstrap

module.exports = warning;

/***/ },
/***/ }),
/* 3 */
/***/ function(module, exports, __webpack_require__) {
/***/ (function(module, exports, __webpack_require__) {

'use strict';

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -329,7 +339,7 @@ return /******/ (function(modules) { // webpackBootstrap

module.exports = wrapActionCreators;

/***/ }
/***/ })
/******/ ])
});
;
2 changes: 1 addition & 1 deletion dist/wechat-weapp-redux.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/connect.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 0 additions & 25 deletions src/shallowEqual.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/shallowEqualAndDiff.js
Original file line number Diff line number Diff line change
@@ -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