UNPKG

1.21 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4var state = {
5 __container__: {}
6};
7
8/**
9 * @description init new state
10 * @public
11 * @version 1.3.0
12 * @param {object} config
13 * @param {string} config.name name state
14 * @param {any} config.defaultValue default value
15 * @param {function} config.willUpdate function for before set state
16 * @param {function} config.shouldUpdate function for is need update
17 * @param {function} config.didUpdate function for after set state
18 * @returns {undefined} nothing
19 */
20exports.init = function init(config) {
21 state.__container__[config.name] = config.defaultValue;
22 Object.defineProperty(state, config.name, {
23 get: function get() {
24 return state.__container__[config.name];
25 },
26 set: function set(value) {
27 if (config.shouldUpdate)
28 if (
29 config.shouldUpdate(state.__container__[config.name], value) === false
30 )
31 return;
32
33 config.willUpdate &&
34 config.willUpdate(state.__container__[config.name], value);
35 state.__container__[config.name] = value;
36 config.didUpdate && config.didUpdate(value);
37 }
38 });
39};
40
41/**
42 * @description state container
43 * @public
44 * @version 1.2.0
45 */
46exports.state = state;