Skip to content

Commit be26f98

Browse files
committed
feat: add code
1 parent 3dd66f0 commit be26f98

File tree

12 files changed

+150
-80
lines changed

12 files changed

+150
-80
lines changed

demo/store/mpstore.esm.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Mpstore.js v0.2.1
2+
* Mpstore.js v0.2.3
33
* (c) 2019-2020 Imtaotao
44
* Released under the MIT License.
55
*/
@@ -731,7 +731,9 @@ function () {
731731
}();
732732

733733
var defaultOption = {
734-
env: 'develop'
734+
env: 'develop',
735+
storeNamespace: 'store',
736+
globalNamespace: 'global'
735737
};
736738

737739
var COMMONACTION = function COMMONACTION() {};
@@ -884,14 +886,14 @@ function () {
884886
this.reducers = [];
885887
this.id = ++storeId;
886888
this.depComponents = [];
887-
this.GLOBALWORD = 'global';
888889
this.isDispatching = false;
889890
this.restoreCallbacks = [];
890891
this.dispatchCallbacks = [];
891-
this.version = '0.2.1';
892+
this.version = '0.2.3';
892893
this.state = Object.freeze(createModule({}));
893894
this.middleware = new Middleware(this);
894-
this.options = Object.assign(defaultOption, options);
895+
this.options = Object.assign({}, defaultOption, options);
896+
this.GLOBALWORD = this.options.globalNamespace;
895897
}
896898

897899
_createClass(Store, [{
@@ -1028,7 +1030,12 @@ function () {
10281030
key: "setNamespace",
10291031
value: function setNamespace(key) {
10301032
assert(key && typeof key === 'string', 'The [namespace] must be a string');
1031-
this.GLOBALWORD = key;
1033+
1034+
if (this.options.env === 'develop') {
1035+
console.error('The `setNamespace` is deprecated, please use options to specify.');
1036+
}
1037+
1038+
this.options.globalNamespace = this.GLOBALWORD = key;
10321039
}
10331040
}, {
10341041
key: "getModule",
@@ -1089,6 +1096,7 @@ function () {
10891096
var data = config.data,
10901097
_config$storeConfig = config.storeConfig,
10911098
storeConfig = _config$storeConfig === void 0 ? {} : _config$storeConfig;
1099+
var storeNamespace = this.options.storeNamespace;
10921100
var addDep = storeConfig.addDep,
10931101
useState = storeConfig.useState,
10941102
didUpdate = storeConfig.didUpdate,
@@ -1174,7 +1182,7 @@ function () {
11741182

11751183
function onLoad() {
11761184
addDepToStore(this);
1177-
this.store = store;
1185+
this[storeNamespace] = store;
11781186
this._$loaded = true;
11791187
}
11801188

@@ -1206,7 +1214,7 @@ function () {
12061214
return Store;
12071215
}();
12081216

1209-
var version = '0.2.1';
1217+
var version = '0.2.3';
12101218
var nativePage = Page;
12111219
var nativeComponent = Component;
12121220

dist/mpstore.common.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Mpstore.js v0.2.1
2+
* Mpstore.js v0.2.3
33
* (c) 2019-2020 Imtaotao
44
* Released under the MIT License.
55
*/
@@ -735,7 +735,9 @@ function () {
735735
}();
736736

737737
var defaultOption = {
738-
env: 'develop'
738+
env: 'develop',
739+
storeNamespace: 'store',
740+
globalNamespace: 'global'
739741
};
740742

741743
var COMMONACTION = function COMMONACTION() {};
@@ -888,14 +890,14 @@ function () {
888890
this.reducers = [];
889891
this.id = ++storeId;
890892
this.depComponents = [];
891-
this.GLOBALWORD = 'global';
892893
this.isDispatching = false;
893894
this.restoreCallbacks = [];
894895
this.dispatchCallbacks = [];
895-
this.version = '0.2.1';
896+
this.version = '0.2.3';
896897
this.state = Object.freeze(createModule({}));
897898
this.middleware = new Middleware(this);
898-
this.options = Object.assign(defaultOption, options);
899+
this.options = Object.assign({}, defaultOption, options);
900+
this.GLOBALWORD = this.options.globalNamespace;
899901
}
900902

901903
_createClass(Store, [{
@@ -1032,7 +1034,12 @@ function () {
10321034
key: "setNamespace",
10331035
value: function setNamespace(key) {
10341036
assert(key && typeof key === 'string', 'The [namespace] must be a string');
1035-
this.GLOBALWORD = key;
1037+
1038+
if (this.options.env === 'develop') {
1039+
console.error('The `setNamespace` is deprecated, please use options to specify.');
1040+
}
1041+
1042+
this.options.globalNamespace = this.GLOBALWORD = key;
10361043
}
10371044
}, {
10381045
key: "getModule",
@@ -1093,6 +1100,7 @@ function () {
10931100
var data = config.data,
10941101
_config$storeConfig = config.storeConfig,
10951102
storeConfig = _config$storeConfig === void 0 ? {} : _config$storeConfig;
1103+
var storeNamespace = this.options.storeNamespace;
10961104
var addDep = storeConfig.addDep,
10971105
useState = storeConfig.useState,
10981106
didUpdate = storeConfig.didUpdate,
@@ -1178,7 +1186,7 @@ function () {
11781186

11791187
function onLoad() {
11801188
addDepToStore(this);
1181-
this.store = store;
1189+
this[storeNamespace] = store;
11821190
this._$loaded = true;
11831191
}
11841192

@@ -1210,7 +1218,7 @@ function () {
12101218
return Store;
12111219
}();
12121220

1213-
var version = '0.2.1';
1221+
var version = '0.2.3';
12141222
var nativePage = Page;
12151223
var nativeComponent = Component;
12161224

dist/mpstore.es6m.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Mpstore.js v0.2.1
2+
* Mpstore.js v0.2.3
33
* (c) 2019-2020 Imtaotao
44
* Released under the MIT License.
55
*/
@@ -558,6 +558,8 @@ class TimeTravel {
558558

559559
var defaultOption = {
560560
env: 'develop',
561+
storeNamespace: 'store',
562+
globalNamespace: 'global',
561563
};
562564

563565
const COMMONACTION = () => {};
@@ -701,14 +703,14 @@ class Store {
701703
this.reducers = [];
702704
this.id = ++storeId;
703705
this.depComponents = [];
704-
this.GLOBALWORD = 'global';
705706
this.isDispatching = false;
706707
this.restoreCallbacks = [];
707708
this.dispatchCallbacks = [];
708-
this.version = '0.2.1';
709+
this.version = '0.2.3';
709710
this.state = Object.freeze(createModule({}));
710711
this.middleware = new Middleware(this);
711-
this.options = Object.assign(defaultOption, options);
712+
this.options = Object.assign({}, defaultOption, options);
713+
this.GLOBALWORD = this.options.globalNamespace;
712714
}
713715
add (action, reducer) {
714716
const env = this.options.env;
@@ -843,7 +845,10 @@ class Store {
843845
key && typeof key === 'string',
844846
'The [namespace] must be a string',
845847
);
846-
this.GLOBALWORD = key;
848+
if (this.options.env === 'develop') {
849+
console.error('The `setNamespace` is deprecated, please use options to specify.');
850+
}
851+
this.options.globalNamespace = this.GLOBALWORD = key;
847852
}
848853
getModule (namespace, remainMsg) {
849854
assert(
@@ -888,6 +893,7 @@ class Store {
888893
const store = this;
889894
const GLOBALWORD = this.GLOBALWORD;
890895
const { data, storeConfig = {} } = config;
896+
const storeNamespace = this.options.storeNamespace;
891897
const {
892898
addDep,
893899
useState,
@@ -962,7 +968,7 @@ class Store {
962968
};
963969
function onLoad () {
964970
addDepToStore(this);
965-
this.store = store;
971+
this[storeNamespace] = store;
966972
this._$loaded = true;
967973
}
968974
function onUnload () {
@@ -982,7 +988,7 @@ class Store {
982988
}
983989
}
984990

985-
const version = '0.2.1';
991+
const version = '0.2.3';
986992
const nativePage = Page;
987993
const nativeComponent = Component;
988994
function expandConfig (config, expandMethods, isPage) {

dist/mpstore.esm.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Mpstore.js v0.2.1
2+
* Mpstore.js v0.2.3
33
* (c) 2019-2020 Imtaotao
44
* Released under the MIT License.
55
*/
@@ -731,7 +731,9 @@ function () {
731731
}();
732732

733733
var defaultOption = {
734-
env: 'develop'
734+
env: 'develop',
735+
storeNamespace: 'store',
736+
globalNamespace: 'global'
735737
};
736738

737739
var COMMONACTION = function COMMONACTION() {};
@@ -884,14 +886,14 @@ function () {
884886
this.reducers = [];
885887
this.id = ++storeId;
886888
this.depComponents = [];
887-
this.GLOBALWORD = 'global';
888889
this.isDispatching = false;
889890
this.restoreCallbacks = [];
890891
this.dispatchCallbacks = [];
891-
this.version = '0.2.1';
892+
this.version = '0.2.3';
892893
this.state = Object.freeze(createModule({}));
893894
this.middleware = new Middleware(this);
894-
this.options = Object.assign(defaultOption, options);
895+
this.options = Object.assign({}, defaultOption, options);
896+
this.GLOBALWORD = this.options.globalNamespace;
895897
}
896898

897899
_createClass(Store, [{
@@ -1028,7 +1030,12 @@ function () {
10281030
key: "setNamespace",
10291031
value: function setNamespace(key) {
10301032
assert(key && typeof key === 'string', 'The [namespace] must be a string');
1031-
this.GLOBALWORD = key;
1033+
1034+
if (this.options.env === 'develop') {
1035+
console.error('The `setNamespace` is deprecated, please use options to specify.');
1036+
}
1037+
1038+
this.options.globalNamespace = this.GLOBALWORD = key;
10321039
}
10331040
}, {
10341041
key: "getModule",
@@ -1089,6 +1096,7 @@ function () {
10891096
var data = config.data,
10901097
_config$storeConfig = config.storeConfig,
10911098
storeConfig = _config$storeConfig === void 0 ? {} : _config$storeConfig;
1099+
var storeNamespace = this.options.storeNamespace;
10921100
var addDep = storeConfig.addDep,
10931101
useState = storeConfig.useState,
10941102
didUpdate = storeConfig.didUpdate,
@@ -1174,7 +1182,7 @@ function () {
11741182

11751183
function onLoad() {
11761184
addDepToStore(this);
1177-
this.store = store;
1185+
this[storeNamespace] = store;
11781186
this._$loaded = true;
11791187
}
11801188

@@ -1206,7 +1214,7 @@ function () {
12061214
return Store;
12071215
}();
12081216

1209-
var version = '0.2.1';
1217+
var version = '0.2.3';
12101218
var nativePage = Page;
12111219
var nativeComponent = Component;
12121220

dist/mpstore.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/store.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ store 会把原生的 `Page`、`Component` 函数包装一层,来做一层拦
1616

1717
### Options
1818
+ `env: 'develop' | 'product'`: 如果指定为 `product`,会去掉状态和模块合并时的检测,state 将不会被冻结住,从而提升性能
19+
+ `storeNamespace: string`: 可以指定注入到组件内部的 store 命名空间,默认为 `store`
20+
+ `globalNamespace: string`: 可以指定注入到组件 data 中使用的全局状态命名空间,默认为 `global`
1921

2022
### 启动
2123
```js
@@ -160,7 +162,7 @@ const store = createStore(() => {}, {})
160162
`forceUpdate` 会强制所有依赖的组件走一遍 diff -> patch 的过程,从而更新视图,当你用的这个方法时,99% 都是你自己的代码写的有问题。
161163

162164
#### setNamespace(namespace: string) : void
163-
store 默认会在组件的 data 中添加 `global` 来接受用到的全局状态,如果需要更改,可以通过此方法。需要注意的是你必须在 `App` 初始化之前调用
165+
store 默认会在组件的 data 中添加 `global` 来接受用到的全局状态,如果需要更改,可以通过此方法。需要注意的是你必须在 `App` 初始化之前调用(此方法已经被弃用,请使用 options.globalNamespace 替换)
164166
```js
165167
store.setNamespace('xxx')
166168

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rustle/mp-store",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "A lightweight wechat miniprogram state management library",
55
"main": "dist/mpstore.common.js",
66
"module": "dist/mpstore.esm.js",

src/default-option.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export default {
22
env: 'develop',
3+
storeNamespace: 'store',
4+
globalNamespace: 'global',
35
}

src/store.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ export class Store {
9090
this.reducers = []
9191
this.id = ++storeId
9292
this.depComponents = []
93-
this.GLOBALWORD = 'global' // global state namespace
9493
this.isDispatching = false
9594
this.restoreCallbacks = []
9695
this.dispatchCallbacks = []
9796
this.version = __VERSION__
9897
this.state = Object.freeze(createModule({}))
9998
this.middleware = new Middleware(this)
100-
this.options = Object.assign(defaultOption, options)
99+
this.options = Object.assign({}, defaultOption, options)
100+
this.GLOBALWORD = this.options.globalNamespace // global state namespace
101101
}
102102

103103
add (action, reducer) {
@@ -276,7 +276,10 @@ export class Store {
276276
key && typeof key === 'string',
277277
'The [namespace] must be a string',
278278
)
279-
this.GLOBALWORD = key
279+
if (this.options.env === 'develop') {
280+
console.error('The `setNamespace` is deprecated, please use options to specify.')
281+
}
282+
this.options.globalNamespace = this.GLOBALWORD = key
280283
}
281284

282285
// get module
@@ -336,6 +339,7 @@ export class Store {
336339
const store = this
337340
const GLOBALWORD = this.GLOBALWORD
338341
const { data, storeConfig = {} } = config
342+
const storeNamespace = this.options.storeNamespace
339343
const {
340344
addDep,
341345
useState,
@@ -437,7 +441,7 @@ export class Store {
437441
function onLoad () {
438442
addDepToStore(this)
439443
// rigister store to component within
440-
this.store = store
444+
this[storeNamespace] = store
441445
this._$loaded = true
442446
}
443447

test/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,5 @@
159159
+ [x] restore 第三个参数可以在回恢复初始状态后不更新视图
160160

161161
### options
162-
+ [x] 如果设置为 env 为除开 `develop` 之外的其他值,则会取消数据的检测
162+
+ [x] 如果设置为 env 为除开 `develop` 之外的其他值,则会取消数据的检测
163+
+ [x] storeNamespace 检查

0 commit comments

Comments
 (0)