Skip to content
This repository was archived by the owner on Oct 19, 2019. It is now read-only.

Commit 50bfba7

Browse files
committed
fix: Downgrade to deepclone when call methods: push、unshift、splice in Vue
1 parent 74840b5 commit 50bfba7

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
},
3636
"homepage": "https://talkingdata.github.io/rxloop-immer",
3737
"dependencies": {
38-
"immer": "^1.6.0"
38+
"immer": "^1.6.0",
39+
"lodash": "^4.17.11"
3940
},
4041
"peerDependencies": {
4142
"@rxloop/core": ">= 0.9.0",

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if (env === 'es' || env === 'cjs') {
1717
nodeResolve({
1818
jsnext: true
1919
}),
20+
commonjs(),
2021
babel({
2122
plugins: ['external-helpers'],
2223
})

src/index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
import produce from "immer";
2+
import cloneDeep from 'lodash/fp/cloneDeep';
23

34
export default function rxloopImmer() {
45
return function init() {
56
function createReducer(action = {}, reducer = () => {}) {
67
return (state) => {
7-
const rtn = produce(state, draft => {
8-
const compatiableRet = reducer(draft, action);
9-
if (compatiableRet !== undefined) {
10-
// which means you are use redux pattern
11-
// it's compatiable. https://github.com/mweststrate/immer#returning-data-from-producers
12-
return compatiableRet;
8+
try {
9+
const rtn = produce(state, draft => {
10+
const compatiableRet = reducer(draft, action);
11+
if (compatiableRet !== undefined) {
12+
// which means you are use redux pattern
13+
// it's compatiable. https://github.com/mweststrate/immer#returning-data-from-producers
14+
return compatiableRet;
15+
}
16+
});
17+
return rtn === undefined ? {} : rtn;
18+
} catch(e) {
19+
// 在 Vue 下开启 rxloop-immer 插件
20+
// 当对数组执行 push、unshift、splice 三个方法时会报错
21+
// 这个时候降级为深拷贝方式
22+
if(e.toString().indexOf('\'observeArray\' of undefined')) {
23+
console.warn('Downgrade to deepclone when call methods: push、unshift、splice in Vue');
24+
const draft = cloneDeep(state);
25+
reducer(draft, action);
26+
return draft;
1327
}
14-
});
15-
return rtn === undefined ? {} : rtn;
28+
throw e;
29+
}
1630
}
1731
}
1832
this.createReducer = createReducer;

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,10 @@ lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.2.1:
26832683
version "4.17.10"
26842684
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
26852685

2686+
lodash@^4.17.11:
2687+
version "4.17.11"
2688+
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
2689+
26862690
loose-envify@^1.0.0:
26872691
version "1.4.0"
26882692
resolved "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"

0 commit comments

Comments
 (0)