Skip to content

Commit

Permalink
feat: add code
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Apr 10, 2020
1 parent 3dd66f0 commit be26f98
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 80 deletions.
24 changes: 16 additions & 8 deletions demo/store/mpstore.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.2.1
* Mpstore.js v0.2.3
* (c) 2019-2020 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -731,7 +731,9 @@ function () {
}();

var defaultOption = {
env: 'develop'
env: 'develop',
storeNamespace: 'store',
globalNamespace: 'global'
};

var COMMONACTION = function COMMONACTION() {};
Expand Down Expand Up @@ -884,14 +886,14 @@ function () {
this.reducers = [];
this.id = ++storeId;
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.restoreCallbacks = [];
this.dispatchCallbacks = [];
this.version = '0.2.1';
this.version = '0.2.3';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
this.options = Object.assign(defaultOption, options);
this.options = Object.assign({}, defaultOption, options);
this.GLOBALWORD = this.options.globalNamespace;
}

_createClass(Store, [{
Expand Down Expand Up @@ -1028,7 +1030,12 @@ function () {
key: "setNamespace",
value: function setNamespace(key) {
assert(key && typeof key === 'string', 'The [namespace] must be a string');
this.GLOBALWORD = key;

if (this.options.env === 'develop') {
console.error('The `setNamespace` is deprecated, please use options to specify.');
}

this.options.globalNamespace = this.GLOBALWORD = key;
}
}, {
key: "getModule",
Expand Down Expand Up @@ -1089,6 +1096,7 @@ function () {
var data = config.data,
_config$storeConfig = config.storeConfig,
storeConfig = _config$storeConfig === void 0 ? {} : _config$storeConfig;
var storeNamespace = this.options.storeNamespace;
var addDep = storeConfig.addDep,
useState = storeConfig.useState,
didUpdate = storeConfig.didUpdate,
Expand Down Expand Up @@ -1174,7 +1182,7 @@ function () {

function onLoad() {
addDepToStore(this);
this.store = store;
this[storeNamespace] = store;
this._$loaded = true;
}

Expand Down Expand Up @@ -1206,7 +1214,7 @@ function () {
return Store;
}();

var version = '0.2.1';
var version = '0.2.3';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
24 changes: 16 additions & 8 deletions dist/mpstore.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.2.1
* Mpstore.js v0.2.3
* (c) 2019-2020 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -735,7 +735,9 @@ function () {
}();

var defaultOption = {
env: 'develop'
env: 'develop',
storeNamespace: 'store',
globalNamespace: 'global'
};

var COMMONACTION = function COMMONACTION() {};
Expand Down Expand Up @@ -888,14 +890,14 @@ function () {
this.reducers = [];
this.id = ++storeId;
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.restoreCallbacks = [];
this.dispatchCallbacks = [];
this.version = '0.2.1';
this.version = '0.2.3';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
this.options = Object.assign(defaultOption, options);
this.options = Object.assign({}, defaultOption, options);
this.GLOBALWORD = this.options.globalNamespace;
}

_createClass(Store, [{
Expand Down Expand Up @@ -1032,7 +1034,12 @@ function () {
key: "setNamespace",
value: function setNamespace(key) {
assert(key && typeof key === 'string', 'The [namespace] must be a string');
this.GLOBALWORD = key;

if (this.options.env === 'develop') {
console.error('The `setNamespace` is deprecated, please use options to specify.');
}

this.options.globalNamespace = this.GLOBALWORD = key;
}
}, {
key: "getModule",
Expand Down Expand Up @@ -1093,6 +1100,7 @@ function () {
var data = config.data,
_config$storeConfig = config.storeConfig,
storeConfig = _config$storeConfig === void 0 ? {} : _config$storeConfig;
var storeNamespace = this.options.storeNamespace;
var addDep = storeConfig.addDep,
useState = storeConfig.useState,
didUpdate = storeConfig.didUpdate,
Expand Down Expand Up @@ -1178,7 +1186,7 @@ function () {

function onLoad() {
addDepToStore(this);
this.store = store;
this[storeNamespace] = store;
this._$loaded = true;
}

Expand Down Expand Up @@ -1210,7 +1218,7 @@ function () {
return Store;
}();

var version = '0.2.1';
var version = '0.2.3';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
20 changes: 13 additions & 7 deletions dist/mpstore.es6m.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.2.1
* Mpstore.js v0.2.3
* (c) 2019-2020 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -558,6 +558,8 @@ class TimeTravel {

var defaultOption = {
env: 'develop',
storeNamespace: 'store',
globalNamespace: 'global',
};

const COMMONACTION = () => {};
Expand Down Expand Up @@ -701,14 +703,14 @@ class Store {
this.reducers = [];
this.id = ++storeId;
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.restoreCallbacks = [];
this.dispatchCallbacks = [];
this.version = '0.2.1';
this.version = '0.2.3';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
this.options = Object.assign(defaultOption, options);
this.options = Object.assign({}, defaultOption, options);
this.GLOBALWORD = this.options.globalNamespace;
}
add (action, reducer) {
const env = this.options.env;
Expand Down Expand Up @@ -843,7 +845,10 @@ class Store {
key && typeof key === 'string',
'The [namespace] must be a string',
);
this.GLOBALWORD = key;
if (this.options.env === 'develop') {
console.error('The `setNamespace` is deprecated, please use options to specify.');
}
this.options.globalNamespace = this.GLOBALWORD = key;
}
getModule (namespace, remainMsg) {
assert(
Expand Down Expand Up @@ -888,6 +893,7 @@ class Store {
const store = this;
const GLOBALWORD = this.GLOBALWORD;
const { data, storeConfig = {} } = config;
const storeNamespace = this.options.storeNamespace;
const {
addDep,
useState,
Expand Down Expand Up @@ -962,7 +968,7 @@ class Store {
};
function onLoad () {
addDepToStore(this);
this.store = store;
this[storeNamespace] = store;
this._$loaded = true;
}
function onUnload () {
Expand All @@ -982,7 +988,7 @@ class Store {
}
}

const version = '0.2.1';
const version = '0.2.3';
const nativePage = Page;
const nativeComponent = Component;
function expandConfig (config, expandMethods, isPage) {
Expand Down
24 changes: 16 additions & 8 deletions dist/mpstore.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.2.1
* Mpstore.js v0.2.3
* (c) 2019-2020 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -731,7 +731,9 @@ function () {
}();

var defaultOption = {
env: 'develop'
env: 'develop',
storeNamespace: 'store',
globalNamespace: 'global'
};

var COMMONACTION = function COMMONACTION() {};
Expand Down Expand Up @@ -884,14 +886,14 @@ function () {
this.reducers = [];
this.id = ++storeId;
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.restoreCallbacks = [];
this.dispatchCallbacks = [];
this.version = '0.2.1';
this.version = '0.2.3';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
this.options = Object.assign(defaultOption, options);
this.options = Object.assign({}, defaultOption, options);
this.GLOBALWORD = this.options.globalNamespace;
}

_createClass(Store, [{
Expand Down Expand Up @@ -1028,7 +1030,12 @@ function () {
key: "setNamespace",
value: function setNamespace(key) {
assert(key && typeof key === 'string', 'The [namespace] must be a string');
this.GLOBALWORD = key;

if (this.options.env === 'develop') {
console.error('The `setNamespace` is deprecated, please use options to specify.');
}

this.options.globalNamespace = this.GLOBALWORD = key;
}
}, {
key: "getModule",
Expand Down Expand Up @@ -1089,6 +1096,7 @@ function () {
var data = config.data,
_config$storeConfig = config.storeConfig,
storeConfig = _config$storeConfig === void 0 ? {} : _config$storeConfig;
var storeNamespace = this.options.storeNamespace;
var addDep = storeConfig.addDep,
useState = storeConfig.useState,
didUpdate = storeConfig.didUpdate,
Expand Down Expand Up @@ -1174,7 +1182,7 @@ function () {

function onLoad() {
addDepToStore(this);
this.store = store;
this[storeNamespace] = store;
this._$loaded = true;
}

Expand Down Expand Up @@ -1206,7 +1214,7 @@ function () {
return Store;
}();

var version = '0.2.1';
var version = '0.2.3';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
4 changes: 2 additions & 2 deletions dist/mpstore.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/store.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ store 会把原生的 `Page`、`Component` 函数包装一层,来做一层拦

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustle/mp-store",
"version": "0.2.2",
"version": "0.2.3",
"description": "A lightweight wechat miniprogram state management library",
"main": "dist/mpstore.common.js",
"module": "dist/mpstore.esm.js",
Expand Down
2 changes: 2 additions & 0 deletions src/default-option.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default {
env: 'develop',
storeNamespace: 'store',
globalNamespace: 'global',
}
12 changes: 8 additions & 4 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export class Store {
this.reducers = []
this.id = ++storeId
this.depComponents = []
this.GLOBALWORD = 'global' // global state namespace
this.isDispatching = false
this.restoreCallbacks = []
this.dispatchCallbacks = []
this.version = __VERSION__
this.state = Object.freeze(createModule({}))
this.middleware = new Middleware(this)
this.options = Object.assign(defaultOption, options)
this.options = Object.assign({}, defaultOption, options)
this.GLOBALWORD = this.options.globalNamespace // global state namespace
}

add (action, reducer) {
Expand Down Expand Up @@ -276,7 +276,10 @@ export class Store {
key && typeof key === 'string',
'The [namespace] must be a string',
)
this.GLOBALWORD = key
if (this.options.env === 'develop') {
console.error('The `setNamespace` is deprecated, please use options to specify.')
}
this.options.globalNamespace = this.GLOBALWORD = key
}

// get module
Expand Down Expand Up @@ -336,6 +339,7 @@ export class Store {
const store = this
const GLOBALWORD = this.GLOBALWORD
const { data, storeConfig = {} } = config
const storeNamespace = this.options.storeNamespace
const {
addDep,
useState,
Expand Down Expand Up @@ -437,7 +441,7 @@ export class Store {
function onLoad () {
addDepToStore(this)
// rigister store to component within
this.store = store
this[storeNamespace] = store
this._$loaded = true
}

Expand Down
3 changes: 2 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@
+ [x] restore 第三个参数可以在回恢复初始状态后不更新视图

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

0 comments on commit be26f98

Please sign in to comment.