@tramvai/state 2.70.0 → 2.72.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/connect/Provider.es.js +8 -0
- package/lib/connect/Provider.js +12 -0
- package/lib/connect/Subscription.es.js +37 -0
- package/lib/connect/Subscription.js +41 -0
- package/lib/connect/connectAdvanced.es.js +159 -0
- package/lib/connect/connectAdvanced.js +170 -0
- package/lib/connect/context.es.js +6 -0
- package/lib/connect/context.js +11 -0
- package/lib/connect/hooks/useActions.es.js +17 -0
- package/lib/connect/hooks/useActions.js +25 -0
- package/lib/connect/hooks/useConsumerContext.es.js +12 -0
- package/lib/connect/hooks/useConsumerContext.js +16 -0
- package/lib/connect/hooks/useSelector.es.js +53 -0
- package/lib/connect/hooks/useSelector.js +63 -0
- package/lib/connect/hooks/useStore.es.js +37 -0
- package/lib/connect/hooks/useStore.js +41 -0
- package/lib/connect/hooks/useStoreSelector.es.js +11 -0
- package/lib/connect/hooks/useStoreSelector.js +15 -0
- package/lib/connect/index.es.js +91 -0
- package/lib/connect/index.js +107 -0
- package/lib/connect/scheduling.es.js +12 -0
- package/lib/connect/scheduling.js +16 -0
- package/lib/connect/selectorFactory.es.js +95 -0
- package/lib/connect/selectorFactory.js +101 -0
- package/lib/connect/toProps/mapContextToProps.es.js +23 -0
- package/lib/connect/toProps/mapContextToProps.js +34 -0
- package/lib/connect/toProps/mapStateToProps.es.js +17 -0
- package/lib/connect/toProps/mapStateToProps.js +27 -0
- package/lib/connect/toProps/mergeProps.es.js +36 -0
- package/lib/connect/toProps/mergeProps.js +42 -0
- package/lib/connect/toProps/wrapMapToProps.es.js +73 -0
- package/lib/connect/toProps/wrapMapToProps.js +85 -0
- package/lib/connect/utils/verifyFunction.es.js +10 -0
- package/lib/connect/utils/verifyFunction.js +18 -0
- package/lib/connect/utils/verifyMapToProps.es.js +20 -0
- package/lib/connect/utils/verifyMapToProps.js +28 -0
- package/lib/connect/utils/verifyPlainObject.es.js +10 -0
- package/lib/connect/utils/verifyPlainObject.js +18 -0
- package/lib/createEvent/createEvent.es.js +22 -0
- package/lib/createEvent/createEvent.js +31 -0
- package/lib/createReducer/createReducer.es.js +60 -0
- package/lib/createReducer/createReducer.js +64 -0
- package/lib/devTools/constants.es.js +3 -0
- package/lib/devTools/constants.js +7 -0
- package/lib/{index_middleware.es.js → devTools/devTools.es.js} +2 -36
- package/lib/{index_middleware.js → devTools/devTools.js} +4 -40
- package/lib/devTools/index.es.js +21 -0
- package/lib/devTools/index.js +23 -0
- package/lib/devTools/middleware.es.js +37 -0
- package/lib/devTools/middleware.js +45 -0
- package/lib/dispatcher/childDispatcherContext.es.js +46 -0
- package/lib/dispatcher/childDispatcherContext.js +50 -0
- package/lib/dispatcher/dispatcher.es.js +105 -0
- package/lib/dispatcher/dispatcher.js +110 -0
- package/lib/dispatcher/dispatcherContext.es.js +279 -0
- package/lib/dispatcher/dispatcherContext.js +288 -0
- package/lib/dispatcher/storeSubscribe.es.js +8 -0
- package/lib/dispatcher/storeSubscribe.js +12 -0
- package/lib/index.es.js +16 -1246
- package/lib/index.js +36 -1281
- package/lib/logger.es.js +3 -0
- package/lib/logger.js +7 -0
- package/lib/stores/BaseStore.es.js +53 -0
- package/lib/stores/BaseStore.js +61 -0
- package/lib/stores/SimpleEmitter.es.js +30 -0
- package/lib/stores/SimpleEmitter.js +34 -0
- package/package.json +6 -7
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { verifyPlainObject } from '../utils/verifyPlainObject.es.js';
|
|
2
|
+
|
|
3
|
+
const defaultMergeProps = (stateProps, contextProps, ownProps) => {
|
|
4
|
+
return { ...ownProps, ...stateProps, ...contextProps };
|
|
5
|
+
};
|
|
6
|
+
function wrapMergePropsFunc(mergeProps) {
|
|
7
|
+
return function initMergePropsProxy(context, { displayName, pure, areMergedPropsEqual }) {
|
|
8
|
+
let hasRunOnce = false;
|
|
9
|
+
let mergedProps;
|
|
10
|
+
return function mergePropsProxy(stateProps, contextProps, ownProps) {
|
|
11
|
+
const nextMergedProps = mergeProps(stateProps, contextProps, ownProps);
|
|
12
|
+
if (hasRunOnce) {
|
|
13
|
+
if (!pure || !areMergedPropsEqual(nextMergedProps, mergedProps)) {
|
|
14
|
+
mergedProps = nextMergedProps;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
hasRunOnce = true;
|
|
19
|
+
mergedProps = nextMergedProps;
|
|
20
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
21
|
+
verifyPlainObject(mergedProps, displayName, 'mergeProps');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return mergedProps;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function whenMergePropsIsFunction(mergeProps) {
|
|
29
|
+
return typeof mergeProps === 'function' ? wrapMergePropsFunc(mergeProps) : undefined;
|
|
30
|
+
}
|
|
31
|
+
function whenMergePropsIsOmitted(mergeProps) {
|
|
32
|
+
return !mergeProps ? () => defaultMergeProps : undefined;
|
|
33
|
+
}
|
|
34
|
+
const mergePropsFactories = [whenMergePropsIsFunction, whenMergePropsIsOmitted];
|
|
35
|
+
|
|
36
|
+
export { mergePropsFactories, whenMergePropsIsFunction, whenMergePropsIsOmitted };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var verifyPlainObject = require('../utils/verifyPlainObject.js');
|
|
6
|
+
|
|
7
|
+
const defaultMergeProps = (stateProps, contextProps, ownProps) => {
|
|
8
|
+
return { ...ownProps, ...stateProps, ...contextProps };
|
|
9
|
+
};
|
|
10
|
+
function wrapMergePropsFunc(mergeProps) {
|
|
11
|
+
return function initMergePropsProxy(context, { displayName, pure, areMergedPropsEqual }) {
|
|
12
|
+
let hasRunOnce = false;
|
|
13
|
+
let mergedProps;
|
|
14
|
+
return function mergePropsProxy(stateProps, contextProps, ownProps) {
|
|
15
|
+
const nextMergedProps = mergeProps(stateProps, contextProps, ownProps);
|
|
16
|
+
if (hasRunOnce) {
|
|
17
|
+
if (!pure || !areMergedPropsEqual(nextMergedProps, mergedProps)) {
|
|
18
|
+
mergedProps = nextMergedProps;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
hasRunOnce = true;
|
|
23
|
+
mergedProps = nextMergedProps;
|
|
24
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
25
|
+
verifyPlainObject.verifyPlainObject(mergedProps, displayName, 'mergeProps');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return mergedProps;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function whenMergePropsIsFunction(mergeProps) {
|
|
33
|
+
return typeof mergeProps === 'function' ? wrapMergePropsFunc(mergeProps) : undefined;
|
|
34
|
+
}
|
|
35
|
+
function whenMergePropsIsOmitted(mergeProps) {
|
|
36
|
+
return !mergeProps ? () => defaultMergeProps : undefined;
|
|
37
|
+
}
|
|
38
|
+
const mergePropsFactories = [whenMergePropsIsFunction, whenMergePropsIsOmitted];
|
|
39
|
+
|
|
40
|
+
exports.mergePropsFactories = mergePropsFactories;
|
|
41
|
+
exports.whenMergePropsIsFunction = whenMergePropsIsFunction;
|
|
42
|
+
exports.whenMergePropsIsOmitted = whenMergePropsIsOmitted;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import always from '@tinkoff/utils/function/always';
|
|
2
|
+
import mapObject from '@tinkoff/utils/object/map';
|
|
3
|
+
import { verifyPlainObject } from '../utils/verifyPlainObject.es.js';
|
|
4
|
+
import { verifyFunction } from '../utils/verifyFunction.es.js';
|
|
5
|
+
|
|
6
|
+
function wrapMapToPropsConstant(getConstant) {
|
|
7
|
+
return function initConstantSelector(context, options) {
|
|
8
|
+
const constantSelector = always(getConstant(context, options));
|
|
9
|
+
constantSelector.dependsOnOwnProps = false;
|
|
10
|
+
return constantSelector;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
// dependsOnOwnProps is used by createMapToPropsProxy to determine whether to pass props as args
|
|
14
|
+
// to the mapToProps function being wrapped. It is also used by makePurePropsSelector to determine
|
|
15
|
+
// whether mapToProps needs to be invoked when props have changed.
|
|
16
|
+
//
|
|
17
|
+
// A length of one signals that mapToProps does not depend on props from the parent component.
|
|
18
|
+
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
|
|
19
|
+
// therefore not reporting its length accurately..
|
|
20
|
+
function getDependsOnOwnProps(mapToProps) {
|
|
21
|
+
return mapToProps.dependsOnOwnProps !== null && mapToProps.dependsOnOwnProps !== undefined
|
|
22
|
+
? Boolean(mapToProps.dependsOnOwnProps)
|
|
23
|
+
: mapToProps.length !== 1;
|
|
24
|
+
}
|
|
25
|
+
// Used by whenMapStateToPropsIsFunction and whenMapDispatchToPropsIsFunction,
|
|
26
|
+
// this function wraps mapToProps in a proxy function which does several things:
|
|
27
|
+
//
|
|
28
|
+
// * Detects whether the mapToProps function being called depends on props, which
|
|
29
|
+
// is used by selectorFactory to decide if it should reinvoke on props changes.
|
|
30
|
+
//
|
|
31
|
+
// * On first call, handles mapToProps if returns another function, and treats that
|
|
32
|
+
// new function as the true mapToProps for subsequent calls.
|
|
33
|
+
//
|
|
34
|
+
// * On first call, verifies the first result is a plain object, in order to warn
|
|
35
|
+
// the developer that their mapToProps function is not returning a valid result.
|
|
36
|
+
//
|
|
37
|
+
function wrapMapToPropsFunc(mapToProps, methodName) {
|
|
38
|
+
return function initProxySelector(context, { displayName }) {
|
|
39
|
+
const proxy = (stateOrContext, ownProps) => proxy.dependsOnOwnProps
|
|
40
|
+
? proxy.mapToProps(stateOrContext, ownProps)
|
|
41
|
+
: proxy.mapToProps(stateOrContext, undefined);
|
|
42
|
+
// allow detectFactoryAndVerify to get ownProps
|
|
43
|
+
proxy.dependsOnOwnProps = true;
|
|
44
|
+
proxy.mapToProps = function detectFactoryAndVerify(stateOrContext, ownProps) {
|
|
45
|
+
proxy.mapToProps = mapToProps;
|
|
46
|
+
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps);
|
|
47
|
+
let props = proxy(stateOrContext, ownProps);
|
|
48
|
+
if (typeof props === 'function') {
|
|
49
|
+
proxy.mapToProps = props;
|
|
50
|
+
proxy.dependsOnOwnProps = getDependsOnOwnProps(props);
|
|
51
|
+
props = proxy(stateOrContext, ownProps);
|
|
52
|
+
}
|
|
53
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
54
|
+
verifyPlainObject(props, displayName, methodName);
|
|
55
|
+
}
|
|
56
|
+
return props;
|
|
57
|
+
};
|
|
58
|
+
return proxy;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function wrapMapToPropsObject(actionsObject) {
|
|
62
|
+
function getConstant({ executeAction }, { displayName }) {
|
|
63
|
+
return mapObject((action, actionName) => {
|
|
64
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
65
|
+
verifyFunction(action, displayName, actionName);
|
|
66
|
+
}
|
|
67
|
+
return (params) => executeAction(action, params);
|
|
68
|
+
}, actionsObject);
|
|
69
|
+
}
|
|
70
|
+
return wrapMapToPropsConstant(getConstant);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { getDependsOnOwnProps, wrapMapToPropsConstant, wrapMapToPropsFunc, wrapMapToPropsObject };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var always = require('@tinkoff/utils/function/always');
|
|
6
|
+
var mapObject = require('@tinkoff/utils/object/map');
|
|
7
|
+
var verifyPlainObject = require('../utils/verifyPlainObject.js');
|
|
8
|
+
var verifyFunction = require('../utils/verifyFunction.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var always__default = /*#__PURE__*/_interopDefaultLegacy(always);
|
|
13
|
+
var mapObject__default = /*#__PURE__*/_interopDefaultLegacy(mapObject);
|
|
14
|
+
|
|
15
|
+
function wrapMapToPropsConstant(getConstant) {
|
|
16
|
+
return function initConstantSelector(context, options) {
|
|
17
|
+
const constantSelector = always__default["default"](getConstant(context, options));
|
|
18
|
+
constantSelector.dependsOnOwnProps = false;
|
|
19
|
+
return constantSelector;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
// dependsOnOwnProps is used by createMapToPropsProxy to determine whether to pass props as args
|
|
23
|
+
// to the mapToProps function being wrapped. It is also used by makePurePropsSelector to determine
|
|
24
|
+
// whether mapToProps needs to be invoked when props have changed.
|
|
25
|
+
//
|
|
26
|
+
// A length of one signals that mapToProps does not depend on props from the parent component.
|
|
27
|
+
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
|
|
28
|
+
// therefore not reporting its length accurately..
|
|
29
|
+
function getDependsOnOwnProps(mapToProps) {
|
|
30
|
+
return mapToProps.dependsOnOwnProps !== null && mapToProps.dependsOnOwnProps !== undefined
|
|
31
|
+
? Boolean(mapToProps.dependsOnOwnProps)
|
|
32
|
+
: mapToProps.length !== 1;
|
|
33
|
+
}
|
|
34
|
+
// Used by whenMapStateToPropsIsFunction and whenMapDispatchToPropsIsFunction,
|
|
35
|
+
// this function wraps mapToProps in a proxy function which does several things:
|
|
36
|
+
//
|
|
37
|
+
// * Detects whether the mapToProps function being called depends on props, which
|
|
38
|
+
// is used by selectorFactory to decide if it should reinvoke on props changes.
|
|
39
|
+
//
|
|
40
|
+
// * On first call, handles mapToProps if returns another function, and treats that
|
|
41
|
+
// new function as the true mapToProps for subsequent calls.
|
|
42
|
+
//
|
|
43
|
+
// * On first call, verifies the first result is a plain object, in order to warn
|
|
44
|
+
// the developer that their mapToProps function is not returning a valid result.
|
|
45
|
+
//
|
|
46
|
+
function wrapMapToPropsFunc(mapToProps, methodName) {
|
|
47
|
+
return function initProxySelector(context, { displayName }) {
|
|
48
|
+
const proxy = (stateOrContext, ownProps) => proxy.dependsOnOwnProps
|
|
49
|
+
? proxy.mapToProps(stateOrContext, ownProps)
|
|
50
|
+
: proxy.mapToProps(stateOrContext, undefined);
|
|
51
|
+
// allow detectFactoryAndVerify to get ownProps
|
|
52
|
+
proxy.dependsOnOwnProps = true;
|
|
53
|
+
proxy.mapToProps = function detectFactoryAndVerify(stateOrContext, ownProps) {
|
|
54
|
+
proxy.mapToProps = mapToProps;
|
|
55
|
+
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps);
|
|
56
|
+
let props = proxy(stateOrContext, ownProps);
|
|
57
|
+
if (typeof props === 'function') {
|
|
58
|
+
proxy.mapToProps = props;
|
|
59
|
+
proxy.dependsOnOwnProps = getDependsOnOwnProps(props);
|
|
60
|
+
props = proxy(stateOrContext, ownProps);
|
|
61
|
+
}
|
|
62
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
63
|
+
verifyPlainObject.verifyPlainObject(props, displayName, methodName);
|
|
64
|
+
}
|
|
65
|
+
return props;
|
|
66
|
+
};
|
|
67
|
+
return proxy;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function wrapMapToPropsObject(actionsObject) {
|
|
71
|
+
function getConstant({ executeAction }, { displayName }) {
|
|
72
|
+
return mapObject__default["default"]((action, actionName) => {
|
|
73
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
74
|
+
verifyFunction.verifyFunction(action, displayName, actionName);
|
|
75
|
+
}
|
|
76
|
+
return (params) => executeAction(action, params);
|
|
77
|
+
}, actionsObject);
|
|
78
|
+
}
|
|
79
|
+
return wrapMapToPropsConstant(getConstant);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
exports.getDependsOnOwnProps = getDependsOnOwnProps;
|
|
83
|
+
exports.wrapMapToPropsConstant = wrapMapToPropsConstant;
|
|
84
|
+
exports.wrapMapToPropsFunc = wrapMapToPropsFunc;
|
|
85
|
+
exports.wrapMapToPropsObject = wrapMapToPropsObject;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import isFunction from '@tinkoff/utils/is/function';
|
|
2
|
+
import { getLogger } from '../../logger.es.js';
|
|
3
|
+
|
|
4
|
+
function verifyFunction(value, displayName, methodName) {
|
|
5
|
+
if (!isFunction(value)) {
|
|
6
|
+
getLogger().debug(`${methodName}() in ${displayName} must be a function. Instead received ${JSON.stringify(value)}.`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { verifyFunction };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var isFunction = require('@tinkoff/utils/is/function');
|
|
6
|
+
var logger = require('../../logger.js');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
|
|
11
|
+
|
|
12
|
+
function verifyFunction(value, displayName, methodName) {
|
|
13
|
+
if (!isFunction__default["default"](value)) {
|
|
14
|
+
logger.getLogger().debug(`${methodName}() in ${displayName} must be a function. Instead received ${JSON.stringify(value)}.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.verifyFunction = verifyFunction;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import shallowEqual from '@tinkoff/utils/is/shallowEqual';
|
|
2
|
+
import { getLogger } from '../../logger.es.js';
|
|
3
|
+
|
|
4
|
+
const verifyMapToProps = (mapStateToProps, name) => {
|
|
5
|
+
function verify(state, props) {
|
|
6
|
+
const firstStateProps = mapStateToProps(state, props);
|
|
7
|
+
const secondStateProps = mapStateToProps(state, props);
|
|
8
|
+
if (!shallowEqual(firstStateProps, secondStateProps)) {
|
|
9
|
+
getLogger().warn('Component "%s" recreate equal props %s', name, Object.keys(firstStateProps)
|
|
10
|
+
.filter((key) => firstStateProps[key] !== secondStateProps[key])
|
|
11
|
+
.map((key) => `"${key}"`)
|
|
12
|
+
.join(', '));
|
|
13
|
+
}
|
|
14
|
+
verify.dependsOnOwnProps = mapStateToProps.dependsOnOwnProps;
|
|
15
|
+
return secondStateProps;
|
|
16
|
+
}
|
|
17
|
+
return verify;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { verifyMapToProps };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var shallowEqual = require('@tinkoff/utils/is/shallowEqual');
|
|
6
|
+
var logger = require('../../logger.js');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var shallowEqual__default = /*#__PURE__*/_interopDefaultLegacy(shallowEqual);
|
|
11
|
+
|
|
12
|
+
const verifyMapToProps = (mapStateToProps, name) => {
|
|
13
|
+
function verify(state, props) {
|
|
14
|
+
const firstStateProps = mapStateToProps(state, props);
|
|
15
|
+
const secondStateProps = mapStateToProps(state, props);
|
|
16
|
+
if (!shallowEqual__default["default"](firstStateProps, secondStateProps)) {
|
|
17
|
+
logger.getLogger().warn('Component "%s" recreate equal props %s', name, Object.keys(firstStateProps)
|
|
18
|
+
.filter((key) => firstStateProps[key] !== secondStateProps[key])
|
|
19
|
+
.map((key) => `"${key}"`)
|
|
20
|
+
.join(', '));
|
|
21
|
+
}
|
|
22
|
+
verify.dependsOnOwnProps = mapStateToProps.dependsOnOwnProps;
|
|
23
|
+
return secondStateProps;
|
|
24
|
+
}
|
|
25
|
+
return verify;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.verifyMapToProps = verifyMapToProps;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import isPlainObject from '@tinkoff/utils/is/plainObject';
|
|
2
|
+
import { getLogger } from '../../logger.es.js';
|
|
3
|
+
|
|
4
|
+
function verifyPlainObject(value, displayName, methodName) {
|
|
5
|
+
if (!isPlainObject(value)) {
|
|
6
|
+
getLogger().debug(`${methodName}() in ${displayName} must return a plain object. Instead received ${value}.`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { verifyPlainObject };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var isPlainObject = require('@tinkoff/utils/is/plainObject');
|
|
6
|
+
var logger = require('../../logger.js');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var isPlainObject__default = /*#__PURE__*/_interopDefaultLegacy(isPlainObject);
|
|
11
|
+
|
|
12
|
+
function verifyPlainObject(value, displayName, methodName) {
|
|
13
|
+
if (!isPlainObject__default["default"](value)) {
|
|
14
|
+
logger.getLogger().debug(`${methodName}() in ${displayName} must return a plain object. Instead received ${value}.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.verifyPlainObject = verifyPlainObject;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import isString from '@tinkoff/utils/is/string';
|
|
2
|
+
import identity from '@tinkoff/utils/function/identity';
|
|
3
|
+
|
|
4
|
+
function createEvent(eventName, payloadCreator = identity) {
|
|
5
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
6
|
+
if (!isString(eventName) || !eventName.length) {
|
|
7
|
+
throw new Error(`eventName should be not empty string, got '${eventName}'`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const type = eventName;
|
|
11
|
+
const eventCreator = (...args) => {
|
|
12
|
+
return {
|
|
13
|
+
type,
|
|
14
|
+
payload: payloadCreator(...args),
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
eventCreator.getType = () => type;
|
|
18
|
+
eventCreator.toString = () => type;
|
|
19
|
+
return eventCreator;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { createEvent };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var isString = require('@tinkoff/utils/is/string');
|
|
6
|
+
var identity = require('@tinkoff/utils/function/identity');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
|
|
11
|
+
var identity__default = /*#__PURE__*/_interopDefaultLegacy(identity);
|
|
12
|
+
|
|
13
|
+
function createEvent(eventName, payloadCreator = identity__default["default"]) {
|
|
14
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
15
|
+
if (!isString__default["default"](eventName) || !eventName.length) {
|
|
16
|
+
throw new Error(`eventName should be not empty string, got '${eventName}'`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const type = eventName;
|
|
20
|
+
const eventCreator = (...args) => {
|
|
21
|
+
return {
|
|
22
|
+
type,
|
|
23
|
+
payload: payloadCreator(...args),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
eventCreator.getType = () => type;
|
|
27
|
+
eventCreator.toString = () => type;
|
|
28
|
+
return eventCreator;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.createEvent = createEvent;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createEvent } from '../createEvent/createEvent.es.js';
|
|
2
|
+
import { SimpleEmitter } from '../stores/SimpleEmitter.es.js';
|
|
3
|
+
|
|
4
|
+
function createReducer(name, initialState) {
|
|
5
|
+
var _a;
|
|
6
|
+
const reducers = {};
|
|
7
|
+
return _a = class ReducerStore extends SimpleEmitter {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
this.state = initialState;
|
|
11
|
+
}
|
|
12
|
+
static createEvents(model) {
|
|
13
|
+
const eventNames = Object.keys(model);
|
|
14
|
+
const events = {};
|
|
15
|
+
eventNames.forEach((eventName) => {
|
|
16
|
+
const handler = model[eventName];
|
|
17
|
+
const eventCreator = createEvent(`${eventName}`);
|
|
18
|
+
events[eventName] = eventCreator;
|
|
19
|
+
ReducerStore.on(eventCreator, handler);
|
|
20
|
+
});
|
|
21
|
+
return events;
|
|
22
|
+
}
|
|
23
|
+
static on(eventOrType, reducer) {
|
|
24
|
+
if (Array.isArray(eventOrType)) {
|
|
25
|
+
eventOrType.forEach((event) => ReducerStore.on(event, reducer));
|
|
26
|
+
return ReducerStore;
|
|
27
|
+
}
|
|
28
|
+
const type = eventOrType.toString();
|
|
29
|
+
ReducerStore.handlers[type] = 'handle';
|
|
30
|
+
reducers[type] = reducer;
|
|
31
|
+
return ReducerStore;
|
|
32
|
+
}
|
|
33
|
+
getState() {
|
|
34
|
+
return this.state;
|
|
35
|
+
}
|
|
36
|
+
setState(state) {
|
|
37
|
+
this.state = state;
|
|
38
|
+
// обновление необходимо, иначе state-manager не пересчитает состояние
|
|
39
|
+
this.emit('change');
|
|
40
|
+
}
|
|
41
|
+
dehydrate() {
|
|
42
|
+
return this.state;
|
|
43
|
+
}
|
|
44
|
+
rehydrate(state) {
|
|
45
|
+
this.state = state;
|
|
46
|
+
}
|
|
47
|
+
handle(payload, eventName) {
|
|
48
|
+
const reducer = reducers[eventName];
|
|
49
|
+
if (reducer) {
|
|
50
|
+
this.state = reducer(this.state, payload);
|
|
51
|
+
this.emit('change');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
_a.storeName = name,
|
|
56
|
+
_a.handlers = {},
|
|
57
|
+
_a;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { createReducer };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var createEvent = require('../createEvent/createEvent.js');
|
|
6
|
+
var SimpleEmitter = require('../stores/SimpleEmitter.js');
|
|
7
|
+
|
|
8
|
+
function createReducer(name, initialState) {
|
|
9
|
+
var _a;
|
|
10
|
+
const reducers = {};
|
|
11
|
+
return _a = class ReducerStore extends SimpleEmitter.SimpleEmitter {
|
|
12
|
+
constructor() {
|
|
13
|
+
super();
|
|
14
|
+
this.state = initialState;
|
|
15
|
+
}
|
|
16
|
+
static createEvents(model) {
|
|
17
|
+
const eventNames = Object.keys(model);
|
|
18
|
+
const events = {};
|
|
19
|
+
eventNames.forEach((eventName) => {
|
|
20
|
+
const handler = model[eventName];
|
|
21
|
+
const eventCreator = createEvent.createEvent(`${eventName}`);
|
|
22
|
+
events[eventName] = eventCreator;
|
|
23
|
+
ReducerStore.on(eventCreator, handler);
|
|
24
|
+
});
|
|
25
|
+
return events;
|
|
26
|
+
}
|
|
27
|
+
static on(eventOrType, reducer) {
|
|
28
|
+
if (Array.isArray(eventOrType)) {
|
|
29
|
+
eventOrType.forEach((event) => ReducerStore.on(event, reducer));
|
|
30
|
+
return ReducerStore;
|
|
31
|
+
}
|
|
32
|
+
const type = eventOrType.toString();
|
|
33
|
+
ReducerStore.handlers[type] = 'handle';
|
|
34
|
+
reducers[type] = reducer;
|
|
35
|
+
return ReducerStore;
|
|
36
|
+
}
|
|
37
|
+
getState() {
|
|
38
|
+
return this.state;
|
|
39
|
+
}
|
|
40
|
+
setState(state) {
|
|
41
|
+
this.state = state;
|
|
42
|
+
// обновление необходимо, иначе state-manager не пересчитает состояние
|
|
43
|
+
this.emit('change');
|
|
44
|
+
}
|
|
45
|
+
dehydrate() {
|
|
46
|
+
return this.state;
|
|
47
|
+
}
|
|
48
|
+
rehydrate(state) {
|
|
49
|
+
this.state = state;
|
|
50
|
+
}
|
|
51
|
+
handle(payload, eventName) {
|
|
52
|
+
const reducer = reducers[eventName];
|
|
53
|
+
if (reducer) {
|
|
54
|
+
this.state = reducer(this.state, payload);
|
|
55
|
+
this.emit('change');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
_a.storeName = name,
|
|
60
|
+
_a.handlers = {},
|
|
61
|
+
_a;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.createReducer = createReducer;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import pick from '@tinkoff/utils/object/pick';
|
|
2
1
|
import noop from '@tinkoff/utils/function/noop';
|
|
2
|
+
import pick from '@tinkoff/utils/object/pick';
|
|
3
3
|
import startsWith from '@tinkoff/utils/string/startsWith';
|
|
4
4
|
import isElement from '@tinkoff/utils/is/element';
|
|
5
5
|
|
|
@@ -33,38 +33,4 @@ if (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const middleware = ({ pickState } = {}) => (api) => (next) => {
|
|
39
|
-
const getState = pickState && pickState.length ? () => pick(pickState, api.getState()) : api.getState;
|
|
40
|
-
// TODO: типизировать message
|
|
41
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
-
devTools.subscribe((message) => {
|
|
43
|
-
if (message.type === 'DISPATCH' && message.state) {
|
|
44
|
-
switch (message.payload.type) {
|
|
45
|
-
// TODO: имплементировать все возможные действия из DevTools
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
else if (message.type === 'ACTION' && message.payload) {
|
|
49
|
-
try {
|
|
50
|
-
// eslint-disable-next-line no-new-func
|
|
51
|
-
const action = new Function(`return ${message.payload}`)();
|
|
52
|
-
api.dispatch(action);
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
devTools.error(e.message);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
devTools.init(getState());
|
|
60
|
-
return (event) => {
|
|
61
|
-
const result = next(event);
|
|
62
|
-
devTools.send({
|
|
63
|
-
type: `${DISPATCH} ${event.type}`,
|
|
64
|
-
event,
|
|
65
|
-
}, getState());
|
|
66
|
-
return result;
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export { middleware };
|
|
36
|
+
export { devTools };
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var pick = require('@tinkoff/utils/object/pick');
|
|
6
5
|
var noop = require('@tinkoff/utils/function/noop');
|
|
6
|
+
var pick = require('@tinkoff/utils/object/pick');
|
|
7
7
|
var startsWith = require('@tinkoff/utils/string/startsWith');
|
|
8
8
|
var isElement = require('@tinkoff/utils/is/element');
|
|
9
9
|
|
|
10
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
11
|
|
|
12
|
-
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);
|
|
13
12
|
var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
|
|
13
|
+
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);
|
|
14
14
|
var startsWith__default = /*#__PURE__*/_interopDefaultLegacy(startsWith);
|
|
15
15
|
var isElement__default = /*#__PURE__*/_interopDefaultLegacy(isElement);
|
|
16
16
|
|
|
17
17
|
const elementReplacer = pick__default["default"](['tagName', 'id', 'className']);
|
|
18
18
|
// eslint-disable-next-line import/no-mutable-exports
|
|
19
|
-
|
|
19
|
+
exports.devTools = {
|
|
20
20
|
init: noop__default["default"],
|
|
21
21
|
send: noop__default["default"],
|
|
22
22
|
subscribe: noop__default["default"],
|
|
@@ -25,7 +25,7 @@ let devTools = {
|
|
|
25
25
|
// eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any
|
|
26
26
|
if (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
27
27
|
// eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any
|
|
28
|
-
devTools = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
|
|
28
|
+
exports.devTools = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
|
|
29
29
|
name: 'Tinkoff-client',
|
|
30
30
|
latency: 700,
|
|
31
31
|
serialize: {
|
|
@@ -43,39 +43,3 @@ if (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
|
43
43
|
},
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
const DISPATCH = 'DISPATCH';
|
|
48
|
-
|
|
49
|
-
const middleware = ({ pickState } = {}) => (api) => (next) => {
|
|
50
|
-
const getState = pickState && pickState.length ? () => pick__default["default"](pickState, api.getState()) : api.getState;
|
|
51
|
-
// TODO: типизировать message
|
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
-
devTools.subscribe((message) => {
|
|
54
|
-
if (message.type === 'DISPATCH' && message.state) {
|
|
55
|
-
switch (message.payload.type) {
|
|
56
|
-
// TODO: имплементировать все возможные действия из DevTools
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
else if (message.type === 'ACTION' && message.payload) {
|
|
60
|
-
try {
|
|
61
|
-
// eslint-disable-next-line no-new-func
|
|
62
|
-
const action = new Function(`return ${message.payload}`)();
|
|
63
|
-
api.dispatch(action);
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
devTools.error(e.message);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
devTools.init(getState());
|
|
71
|
-
return (event) => {
|
|
72
|
-
const result = next(event);
|
|
73
|
-
devTools.send({
|
|
74
|
-
type: `${DISPATCH} ${event.type}`,
|
|
75
|
-
event,
|
|
76
|
-
}, getState());
|
|
77
|
-
return result;
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
exports.middleware = middleware;
|