Skip to content

Commit

Permalink
fix: fix merge child module bug
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Dec 21, 2019
1 parent 6be8cbf commit 6cdb303
Show file tree
Hide file tree
Showing 18 changed files with 400 additions and 137 deletions.
45 changes: 36 additions & 9 deletions demo/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import createStore from './store/mpstore.esm'

const store = createStore(null, {
createBefore (isPage, config) {
// console.log(isPage, config)
},
})

//app.js
import createStore, { createModule } from './store/mpstore.esm'

const store = createStore(null, {
createBefore (isPage, config) {
// console.log(isPage, config)
},
})

store.addModule('a', {
clear: {
setter() {
return {
// b: createModule({
// c: createModule({})
// })
}
},
}
})

store.addModule('a.b.c', {
setB: {
partialState: {
name: 'tao'
}
}
})

console.clear()
setTimeout(() => {
store.dispatch('clear')
console.log(store.state)
}, 500)


//app.js
App({})
3 changes: 2 additions & 1 deletion demo/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"pages": [
"pages/index/index",
"pages/module/index",
"pages/time-travel/index"
"pages/time-travel/index",
"pages/module-two/index"
],
"window": {
"backgroundTextStyle": "light",
Expand Down
47 changes: 47 additions & 0 deletions demo/pages/module-two/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { createModule } from '../../store/mpstore.esm'

Page({
storeConfig: {
defineReducer (store) {
store.add('one', {
partialState: {
name: 'chen',
},
setter (state, payload) {
return {
name: payload,
a: createModule({
age: 20,
b: store.getModule('a.b'),
}),
}
},
})
store.add('two', {
namespace: 'a.b',
partialState: {
sex: 'man',
},
setter (state, payload) {
return { sex: payload }
},
})
},
useState () {
return ['a.b', {
sex: s => s.sex,
name: (s, r) => r.name,
age: (s, r) => r.a.age,
}]
},
},

onLoad() {
setTimeout(() => {
this.store.dispatch('one', 'imtaotao')
}, 1000)
setTimeout(() => {
this.store.dispatch('two', 'women')
}, 2000)
}
})
3 changes: 3 additions & 0 deletions demo/pages/module-two/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"usingComponents": {}
}
2 changes: 2 additions & 0 deletions demo/pages/module-two/index.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<view>{{ global.sex }}-{{ global.name }}-{{ global.age }}</view>

1 change: 1 addition & 0 deletions demo/pages/module-two/index.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* pages/module-two/index.wxss */
28 changes: 14 additions & 14 deletions demo/pages/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ let i = 0
Page({
storeConfig: {
travelLimit: 5,
defineReducer (store) {
store.add('action', {
partialState: {
a: createModule({})
},
setter (state, payload) {
// 这会报错,因为 b 原本不是一个模块,所以你不能新增 b 模块
return {
b: createModule({}),
}
},
})
store.dispatch('action')
// defineReducer (store) {
// store.add('action', {
// partialState: {
// a: createModule({})
// },
// setter (state, payload) {
// // 这会报错,因为 b 原本不是一个模块,所以你不能新增 b 模块
// return {
// b: createModule({}),
// }
// },
// })
// store.dispatch('action')
// store.add('one', {
// partialState: {
// name: 'chen',
Expand Down Expand Up @@ -137,7 +137,7 @@ Page({
// 'taotao': 1221
// }
// })
},
// },

// useState: () => (['taoo.tao', {
// index: state => state.index,
Expand Down
78 changes: 39 additions & 39 deletions demo/pages/time-travel/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
// this is a demo for time travel
Page({
storeConfig: {
travelLimit: 5,
defineReducer (store) {
store.add('TIMETRAVEL', {
partialState: {
index: 0,
},
setter (state, payload) {
return { index: payload }
},
})
},

useState: () => ({
index: state => state.index,
}),
},

changed () {
this.store.dispatch('TIMETRAVEL', this.data.global.index + 1)
},

forward () {
this.timeTravel.forward()
},

back () {
this.timeTravel.back()
},

toStart () {
this.timeTravel.toStart()
},

toEnd () {
this.timeTravel.toEnd()
},
// this is a demo for time travel
Page({
storeConfig: {
travelLimit: 5,
defineReducer (store) {
// store.add('TIMETRAVEL', {
// partialState: {
// index: 0,
// },
// setter (state, payload) {
// return { index: payload }
// },
// })
},

useState: () => ({
index: state => state.index,
}),
},

changed () {
this.store.dispatch('TIMETRAVEL', this.data.global.index + 1)
},

forward () {
this.timeTravel.forward()
},

back () {
this.timeTravel.back()
},

toStart () {
this.timeTravel.toStart()
},

toEnd () {
this.timeTravel.toEnd()
},
})
9 changes: 8 additions & 1 deletion demo/project.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"list": []
},
"miniprogram": {
"current": 2,
"current": 3,
"list": [
{
"id": -1,
Expand All @@ -57,6 +57,13 @@
"pathName": "pages/module/index",
"query": "",
"scene": null
},
{
"id": -1,
"name": "模块嵌套",
"pathName": "pages/module-two/index",
"query": "",
"scene": null
}
]
}
Expand Down
47 changes: 38 additions & 9 deletions demo/store/mpstore.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.1.11
* Mpstore.js v0.1.12
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -269,6 +269,39 @@ function getModule(state, namespace) {

return isModule(module) ? module : null;
}

function assertChildModule(a, b, key) {
var isModuleForOrigin = isModule(a);
var isModuleForCurrent = isModule(b);
assert(!(isModuleForOrigin && !isModuleForCurrent), "The namespace [".concat(key, "] is a module that you can change to other value, ") + 'You can use `createModule` method to recreate a module.' + '\n\n --- from setter function.');
assert(!(!isModuleForOrigin && isModuleForCurrent), "The namespace [".concat(key, "] is not a module, you can't create it as a module, ") + 'you must define the module in `reducer`.' + '\n\n --- from setter function.');
return isModuleForOrigin && isModuleForCurrent;
}

function checkChildModule(a, b) {
var keys = Object.keys(b);
var len = keys.length;

while (~--len) {
var key = keys[len];

if (assertChildModule(a[key], b[key], key)) {
checkChildModule(a[key], b[key]);
}
}

keys = Object.keys(a);
len = keys.length;

while (~--len) {
var _key = keys[len];

if (!(_key in b)) {
assertChildModule(a[_key], b[_key], _key);
}
}
}

function mergeModule(module, partialModule, moduleName, createMsg) {
var keys = Object.keys(partialModule);
var len = keys.length;
Expand All @@ -281,13 +314,9 @@ function mergeModule(module, partialModule, moduleName, createMsg) {
} else {
var originItem = module[key];
var currentPartialItem = partialModule[key];
var isModuleForOrigin = isModule(originItem);
var isModuleForCurrent = isModule(currentPartialItem);
assert(!(isModuleForOrigin && !isModuleForCurrent), "The namespace [".concat(key, "] is a module that you can change to other value, ") + 'You can use `createModule` method to recreate a module.' + '\n\n --- from setter function.');
assert(!(!isModuleForOrigin && isModuleForCurrent), "The namespace [".concat(key, "] is not a module, you can't create it as a module, ") + 'you must define the module in `reducer`.' + '\n\n --- from setter function.');

if (isModuleForOrigin && isModuleForCurrent) {
partialModule[key] = mergeModule(originItem, currentPartialItem);
if (assertChildModule(originItem, currentPartialItem, key)) {
checkChildModule(originItem, currentPartialItem);
}
}
}
Expand Down Expand Up @@ -818,7 +847,7 @@ function () {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.11';
this.version = '0.1.12';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -1082,7 +1111,7 @@ function () {
return Store;
}();

var version = '0.1.11';
var version = '0.1.12';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
Loading

0 comments on commit 6cdb303

Please sign in to comment.