Skip to content

Commit

Permalink
refactor: optimize useState method
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Nov 20, 2019
1 parent c33821c commit ebda10a
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 76 deletions.
70 changes: 35 additions & 35 deletions demo/pages/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ Page({
storeConfig: {
travelLimit: 5,
defineReducer (store) {
// store.add('one', {
store.add('one', {
partialState: {
name: 'chen',
},
setter: (state, payload) => ({ name: payload })
})
store.add('two', {
namespace: 'a',
partialState: {
age: 0,
},
setter: (state, payload) => ({ age: payload })
})
store.add('three', {
namespace: 'a.b',
partialState: {
sex: 'man',
},
setter: (state, payload) => ({ sex: payload })
})

// store.add('action', {
// partialState: {
// name: 'chen',
// },
// setter: (state, payload) => ({ name: payload })
// a: {},
// }
// })
// store.add('two', {
// namespace: 'a',
// partialState: {
// age: 0,
// },
// setter: (state, payload) => ({ age: payload })
// namespace: 'b',
// partialState: {},
// })
// store.add('three', {
// namespace: 'a.b',
// partialState: {
// sex: 'man',
// },
// setter: (state, payload) => ({ sex: payload })
// })

store.add('action', {
partialState: {
a: {},
}
})
// store.add('two', {
// namespace: 'b',
// partialState: {},
// partialState: { a: 1 },
// })
store.add('three', {
namespace: 'a.b',
partialState: { a: 1 },
})
// store.add('four', {
// namespace: 'a.a',
// partialState: { b: 2 },
Expand Down Expand Up @@ -114,14 +114,14 @@ Page({
// useState: () => (['taoo.tao', {
// index: state => state.index,
// }]),
// travelLimit: 5,
// useState () {
// return ['a.b', {
// sex: s => s.sex,
// name: (s, r) => r.name,
// age: (s, r) => r.a.age,
// }]
// },
travelLimit: 5,
useState () {
return ['a.b', {
sex: s => s.sex,
name: (s, r) => r.name,
age: (s, r) => r.a.age,
}]
},
},

check (sex, name, age) {
Expand Down
20 changes: 12 additions & 8 deletions demo/store/mpstore.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,17 +990,21 @@ function () {

assert(isPlainObject(defineObject), '[useState] must return a plain object, ' + "but now is return a [".concat(_typeof(defineObject), "]"));

createState = function createState() {
return clone(mapObject(defineObject, function (fn) {
if (namespace === null) {
if (namespace === null) {
createState = function createState() {
return clone(mapObject(defineObject, function (fn) {
return fn(store.state);
}

}));
};
} else {
createState = function createState() {
var module = _this4.getModule(namespace, "\n\n --- from [".concat(namespace, "] of useState."));

return fn(module, store.state);
}));
};
return clone(mapObject(defineObject, function (fn) {
return fn(module, store.state);
}));
};
}
}

if (createState !== null) {
Expand Down
20 changes: 12 additions & 8 deletions dist/mpstore.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,17 +994,21 @@ function () {

assert(isPlainObject(defineObject), '[useState] must return a plain object, ' + "but now is return a [".concat(_typeof(defineObject), "]"));

createState = function createState() {
return clone(mapObject(defineObject, function (fn) {
if (namespace === null) {
if (namespace === null) {
createState = function createState() {
return clone(mapObject(defineObject, function (fn) {
return fn(store.state);
}

}));
};
} else {
createState = function createState() {
var module = _this4.getModule(namespace, "\n\n --- from [".concat(namespace, "] of useState."));

return fn(module, store.state);
}));
};
return clone(mapObject(defineObject, function (fn) {
return fn(module, store.state);
}));
};
}
}

if (createState !== null) {
Expand Down
15 changes: 8 additions & 7 deletions dist/mpstore.es6m.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,14 @@ class Store {
'[useState] must return a plain object, ' +
`but now is return a [${typeof defineObject}]`,
);
createState = () => clone(mapObject(defineObject, fn => {
if (namespace === null) {
return fn(store.state)
}
const module = this.getModule(namespace, `\n\n --- from [${namespace}] of useState.`);
return fn(module, store.state)
}));
if (namespace === null) {
createState = () => clone(mapObject(defineObject, fn => fn(store.state)));
} else {
createState = () => {
const module = this.getModule(namespace, `\n\n --- from [${namespace}] of useState.`);
return clone(mapObject(defineObject, fn => fn(module, store.state)))
};
}
}
if (createState !== null) {
const useState = createState();
Expand Down
20 changes: 12 additions & 8 deletions dist/mpstore.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,17 +990,21 @@ function () {

assert(isPlainObject(defineObject), '[useState] must return a plain object, ' + "but now is return a [".concat(_typeof(defineObject), "]"));

createState = function createState() {
return clone(mapObject(defineObject, function (fn) {
if (namespace === null) {
if (namespace === null) {
createState = function createState() {
return clone(mapObject(defineObject, function (fn) {
return fn(store.state);
}

}));
};
} else {
createState = function createState() {
var module = _this4.getModule(namespace, "\n\n --- from [".concat(namespace, "] of useState."));

return fn(module, store.state);
}));
};
return clone(mapObject(defineObject, function (fn) {
return fn(module, store.state);
}));
};
}
}

if (createState !== null) {
Expand Down
Loading

0 comments on commit ebda10a

Please sign in to comment.