@tramvai/module-common 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/CommonModule.browser.js +100 -0
- package/lib/CommonModule.es.js +100 -0
- package/lib/CommonModule.js +106 -0
- package/lib/actions/ActionModule.browser.js +79 -0
- package/lib/actions/ActionModule.es.js +79 -0
- package/lib/actions/ActionModule.js +82 -0
- package/lib/actions/actionChecker.browser.js +52 -0
- package/lib/actions/actionChecker.es.js +52 -0
- package/lib/actions/actionChecker.js +56 -0
- package/lib/actions/actionExecution.browser.js +107 -0
- package/lib/actions/actionExecution.es.js +107 -0
- package/lib/actions/actionExecution.js +118 -0
- package/lib/actions/actionPageRunner.browser.browser.js +33 -0
- package/lib/actions/actionPageRunner.es.js +61 -0
- package/lib/actions/actionPageRunner.js +65 -0
- package/lib/actions/actionRegistry.browser.js +33 -0
- package/lib/actions/actionRegistry.es.js +33 -0
- package/lib/actions/actionRegistry.js +45 -0
- package/lib/actions/actionTramvaiReducer.browser.js +10 -0
- package/lib/actions/actionTramvaiReducer.es.js +10 -0
- package/lib/actions/actionTramvaiReducer.js +15 -0
- package/lib/actions/conditions/always.browser.js +10 -0
- package/lib/actions/conditions/always.es.js +10 -0
- package/lib/actions/conditions/always.js +14 -0
- package/lib/actions/conditions/helpers.browser.js +4 -0
- package/lib/actions/conditions/helpers.es.js +4 -0
- package/lib/actions/conditions/helpers.js +9 -0
- package/lib/actions/conditions/onlyBrowser.browser.js +12 -0
- package/lib/actions/conditions/onlyBrowser.es.js +12 -0
- package/lib/actions/conditions/onlyBrowser.js +16 -0
- package/lib/actions/conditions/onlyServer.browser.js +12 -0
- package/lib/actions/conditions/onlyServer.es.js +12 -0
- package/lib/actions/conditions/onlyServer.js +16 -0
- package/lib/actions/conditions/pageBrowser.browser.js +13 -0
- package/lib/actions/conditions/pageBrowser.es.js +13 -0
- package/lib/actions/conditions/pageBrowser.js +17 -0
- package/lib/actions/conditions/pageServer.browser.js +13 -0
- package/lib/actions/conditions/pageServer.es.js +13 -0
- package/lib/actions/conditions/pageServer.js +17 -0
- package/lib/actions/constants.browser.js +9 -0
- package/lib/actions/constants.es.js +9 -0
- package/lib/actions/constants.js +13 -0
- package/lib/bundleManager/bundleManager.browser.js +105 -0
- package/lib/bundleManager/bundleManager.es.js +105 -0
- package/lib/bundleManager/bundleManager.js +115 -0
- package/lib/cache/CacheModule.browser.js +58 -0
- package/lib/cache/CacheModule.es.js +58 -0
- package/lib/cache/CacheModule.js +62 -0
- package/lib/cache/cacheFactory.browser.js +7 -0
- package/lib/cache/cacheFactory.es.js +7 -0
- package/lib/cache/cacheFactory.js +15 -0
- package/lib/cache/clientProviders.browser.js +3 -0
- package/lib/cache/papi.es.js +18 -0
- package/lib/cache/papi.js +22 -0
- package/lib/cache/serverProviders.es.js +18 -0
- package/lib/cache/serverProviders.js +22 -0
- package/lib/child-app/ChildAppModule.browser.js +38 -0
- package/lib/child-app/ChildAppModule.es.js +38 -0
- package/lib/child-app/ChildAppModule.js +40 -0
- package/lib/child-app/actions.browser.js +19 -0
- package/lib/child-app/actions.es.js +19 -0
- package/lib/child-app/actions.js +23 -0
- package/lib/child-app/clientProviders.browser.js +18 -0
- package/lib/child-app/command.browser.js +35 -0
- package/lib/child-app/command.es.js +35 -0
- package/lib/child-app/command.js +40 -0
- package/lib/child-app/serverProviders.es.js +3 -0
- package/lib/child-app/serverProviders.js +7 -0
- package/lib/child-app/state.browser.js +13 -0
- package/lib/child-app/state.es.js +13 -0
- package/lib/child-app/state.js +17 -0
- package/lib/command/CommandModule.browser.js +52 -0
- package/lib/command/CommandModule.es.js +52 -0
- package/lib/command/CommandModule.js +54 -0
- package/lib/command/commandLineRunner.browser.js +147 -0
- package/lib/command/commandLineRunner.es.js +147 -0
- package/lib/command/commandLineRunner.js +151 -0
- package/lib/command/defaultLines.browser.js +25 -0
- package/lib/command/defaultLines.es.js +25 -0
- package/lib/command/defaultLines.js +29 -0
- package/lib/componentRegistry/componentRegistry.browser.js +25 -0
- package/lib/componentRegistry/componentRegistry.es.js +25 -0
- package/lib/componentRegistry/componentRegistry.js +34 -0
- package/lib/createConsumerContext/createConsumerContext.browser.js +62 -0
- package/lib/createConsumerContext/createConsumerContext.es.js +62 -0
- package/lib/createConsumerContext/createConsumerContext.js +67 -0
- package/lib/executionContext/executionContextManager.browser.js +57 -0
- package/lib/executionContext/executionContextManager.es.js +57 -0
- package/lib/executionContext/executionContextManager.js +61 -0
- package/lib/index.browser.js +13 -1300
- package/lib/index.es.js +13 -1318
- package/lib/index.js +38 -1341
- package/lib/providers/clientProviders.browser.js +22 -0
- package/lib/providers/serverProviders.es.js +3 -0
- package/lib/providers/serverProviders.js +7 -0
- package/lib/pubsub/PubSubModule.browser.js +44 -0
- package/lib/pubsub/PubSubModule.es.js +44 -0
- package/lib/pubsub/PubSubModule.js +46 -0
- package/lib/requestManager/RequestManagerModule.browser.browser.js +38 -0
- package/lib/requestManager/RequestManagerModule.es.js +35 -0
- package/lib/requestManager/RequestManagerModule.js +37 -0
- package/lib/requestManager/RequestManagerStore.browser.js +8 -0
- package/lib/requestManager/RequestManagerStore.es.js +8 -0
- package/lib/requestManager/RequestManagerStore.js +13 -0
- package/lib/requestManager/requestManager.browser.js +57 -0
- package/lib/requestManager/requestManager.es.js +57 -0
- package/lib/requestManager/requestManager.js +61 -0
- package/lib/requestManager/sharedProviders.browser.js +23 -0
- package/lib/requestManager/sharedProviders.es.js +23 -0
- package/lib/requestManager/sharedProviders.js +27 -0
- package/lib/responseManager/ResponseManagerModule.browser.browser.js +21 -0
- package/lib/responseManager/ResponseManagerModule.es.js +13 -0
- package/lib/responseManager/ResponseManagerModule.js +15 -0
- package/lib/responseManager/responseManager.browser.js +52 -0
- package/lib/responseManager/responseManager.es.js +52 -0
- package/lib/responseManager/responseManager.js +61 -0
- package/lib/responseManager/sharedProviders.browser.js +13 -0
- package/lib/responseManager/sharedProviders.es.js +13 -0
- package/lib/responseManager/sharedProviders.js +17 -0
- package/lib/state/StateModule.browser.js +54 -0
- package/lib/state/StateModule.es.js +54 -0
- package/lib/state/StateModule.js +60 -0
- package/package.json +24 -25
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var constants = require('./constants.js');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_CONDITIONS = {};
|
|
8
|
+
class ActionChecker {
|
|
9
|
+
// eslint-disable-next-line max-params
|
|
10
|
+
constructor(globalConditionals, payload, parameters, executionState, type) {
|
|
11
|
+
var _a;
|
|
12
|
+
this.globalConditionals = globalConditionals;
|
|
13
|
+
this.payload = payload;
|
|
14
|
+
this.parameters = parameters;
|
|
15
|
+
this.executionState = executionState;
|
|
16
|
+
this.type = type;
|
|
17
|
+
this.status = true;
|
|
18
|
+
this.forbiddenMarker = false;
|
|
19
|
+
this.key = null;
|
|
20
|
+
// для глобальных экшенов мы должны дедублицировать выполнение и меньше выполнять
|
|
21
|
+
if (type === constants.actionType.global) {
|
|
22
|
+
// если экшен был уже выполнен, то считаем, что его не нужно больше выполнять
|
|
23
|
+
this.status = executionState.status !== 'success';
|
|
24
|
+
}
|
|
25
|
+
this.conditions = (_a = parameters.conditions) !== null && _a !== void 0 ? _a : DEFAULT_CONDITIONS;
|
|
26
|
+
}
|
|
27
|
+
check() {
|
|
28
|
+
this.globalConditionals.forEach((filter) => {
|
|
29
|
+
this.key = filter.key;
|
|
30
|
+
filter.fn(this);
|
|
31
|
+
});
|
|
32
|
+
return this.getStatus();
|
|
33
|
+
}
|
|
34
|
+
setState(value) {
|
|
35
|
+
this.executionState.state[this.key] = value;
|
|
36
|
+
}
|
|
37
|
+
getState() {
|
|
38
|
+
return this.executionState.state[this.key];
|
|
39
|
+
}
|
|
40
|
+
forbid() {
|
|
41
|
+
this.executionState.status = 'forbidden';
|
|
42
|
+
this.executionState.forbiddenBy = this.key;
|
|
43
|
+
this.forbiddenMarker = true;
|
|
44
|
+
}
|
|
45
|
+
allow() {
|
|
46
|
+
this.status = true;
|
|
47
|
+
}
|
|
48
|
+
getStatus() {
|
|
49
|
+
if (this.forbiddenMarker) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return this.status;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.ActionChecker = ActionChecker;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import flatten from '@tinkoff/utils/array/flatten';
|
|
2
|
+
import identity from '@tinkoff/utils/function/identity';
|
|
3
|
+
import { isTramvaiAction, ACTION_PARAMETERS } from '@tramvai/core';
|
|
4
|
+
import objectMap from '@tinkoff/utils/object/map';
|
|
5
|
+
import { ConditionFailError } from '@tinkoff/errors';
|
|
6
|
+
import { ActionChecker } from './actionChecker.browser.js';
|
|
7
|
+
import { actionType } from './constants.browser.js';
|
|
8
|
+
import { actionTramvaiReducer } from './actionTramvaiReducer.browser.js';
|
|
9
|
+
|
|
10
|
+
const EMPTY_DEPS = {};
|
|
11
|
+
const DEFAULT_PAYLOAD = {};
|
|
12
|
+
const getParameters = (action) => action[ACTION_PARAMETERS];
|
|
13
|
+
class ActionExecution {
|
|
14
|
+
constructor({ store, context, di, executionContextManager, actionConditionals, transformAction, }) {
|
|
15
|
+
this.actionConditionals = flatten(actionConditionals !== null && actionConditionals !== void 0 ? actionConditionals : []);
|
|
16
|
+
this.context = context;
|
|
17
|
+
this.store = store;
|
|
18
|
+
this.di = di;
|
|
19
|
+
this.executionContextManager = executionContextManager;
|
|
20
|
+
this.execution = new Map();
|
|
21
|
+
this.transformAction = transformAction || identity;
|
|
22
|
+
const initialState = store.getState(actionTramvaiReducer);
|
|
23
|
+
if (initialState && initialState.serverState) {
|
|
24
|
+
objectMap((value, key) => {
|
|
25
|
+
this.execution.set(key, value);
|
|
26
|
+
}, initialState.serverState);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async runInContext(executionContext, action, ...params) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
let parameters;
|
|
32
|
+
const payload = (_a = params[0]) !== null && _a !== void 0 ? _a : DEFAULT_PAYLOAD;
|
|
33
|
+
// TODO: replace type with pure context usage
|
|
34
|
+
const type = (executionContext === null || executionContext === void 0 ? void 0 : executionContext.values.pageActions) === true ? actionType.global : actionType.local;
|
|
35
|
+
// TODO: remove else branch after migration to new declareAction
|
|
36
|
+
if (isTramvaiAction(action)) {
|
|
37
|
+
parameters = action;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.transformAction(action);
|
|
41
|
+
parameters = getParameters(action);
|
|
42
|
+
}
|
|
43
|
+
const executionState = this.getExecutionState(parameters.name);
|
|
44
|
+
if (!this.canExecuteAction(payload, parameters, executionState, type)) {
|
|
45
|
+
switch (parameters.conditionsFailResult) {
|
|
46
|
+
case 'reject':
|
|
47
|
+
return Promise.reject(new ConditionFailError({
|
|
48
|
+
conditionName: (_b = executionState.forbiddenBy) !== null && _b !== void 0 ? _b : 'unknown',
|
|
49
|
+
targetName: parameters.name,
|
|
50
|
+
}));
|
|
51
|
+
default:
|
|
52
|
+
return Promise.resolve();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
executionState.status = 'pending';
|
|
56
|
+
return this.executionContextManager.withContext(executionContext, {
|
|
57
|
+
name: parameters.name,
|
|
58
|
+
values: (executionContext === null || executionContext === void 0 ? void 0 : executionContext.values.pageActions) === true ? { pageActions: false } : undefined,
|
|
59
|
+
}, (executionActionContext, abortController) => {
|
|
60
|
+
const context = this.createActionContext(executionContext, executionActionContext, abortController, parameters);
|
|
61
|
+
return Promise.resolve()
|
|
62
|
+
.then(() => {
|
|
63
|
+
if (isTramvaiAction(action)) {
|
|
64
|
+
return action.fn.apply(context, params);
|
|
65
|
+
}
|
|
66
|
+
return action(this.context, payload, context.deps);
|
|
67
|
+
})
|
|
68
|
+
.then((data) => {
|
|
69
|
+
executionState.status = 'success';
|
|
70
|
+
return data;
|
|
71
|
+
})
|
|
72
|
+
.catch((err) => {
|
|
73
|
+
executionState.status = 'failed';
|
|
74
|
+
throw err;
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async run(action, ...params) {
|
|
79
|
+
return this.runInContext(null, action, ...params);
|
|
80
|
+
}
|
|
81
|
+
getExecutionState(name) {
|
|
82
|
+
let executionState = this.execution.get(name);
|
|
83
|
+
// TODO: probably do not need to create executionState on client as it is not used
|
|
84
|
+
if (!executionState) {
|
|
85
|
+
executionState = { status: 'pending', state: {} };
|
|
86
|
+
this.execution.set(name, executionState);
|
|
87
|
+
}
|
|
88
|
+
return executionState;
|
|
89
|
+
}
|
|
90
|
+
canExecuteAction(payload, parameters, executionState, type) {
|
|
91
|
+
const actionChecker = new ActionChecker(this.actionConditionals, payload, parameters, executionState, type);
|
|
92
|
+
return actionChecker.check();
|
|
93
|
+
}
|
|
94
|
+
createActionContext(parentExecutionContext, actionExecutionContext, abortController, parameters) {
|
|
95
|
+
return {
|
|
96
|
+
abortController,
|
|
97
|
+
abortSignal: actionExecutionContext === null || actionExecutionContext === void 0 ? void 0 : actionExecutionContext.abortSignal,
|
|
98
|
+
executeAction: this.runInContext.bind(this, actionExecutionContext),
|
|
99
|
+
deps: parameters.deps ? this.di.getOfDeps(parameters.deps) : EMPTY_DEPS,
|
|
100
|
+
actionType: (parentExecutionContext === null || parentExecutionContext === void 0 ? void 0 : parentExecutionContext.values.pageActions) ? 'page' : 'standalone',
|
|
101
|
+
dispatch: this.store.dispatch,
|
|
102
|
+
getState: this.store.getState,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export { ActionExecution, getParameters };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import flatten from '@tinkoff/utils/array/flatten';
|
|
2
|
+
import identity from '@tinkoff/utils/function/identity';
|
|
3
|
+
import { isTramvaiAction, ACTION_PARAMETERS } from '@tramvai/core';
|
|
4
|
+
import objectMap from '@tinkoff/utils/object/map';
|
|
5
|
+
import { ConditionFailError } from '@tinkoff/errors';
|
|
6
|
+
import { ActionChecker } from './actionChecker.es.js';
|
|
7
|
+
import { actionType } from './constants.es.js';
|
|
8
|
+
import { actionTramvaiReducer } from './actionTramvaiReducer.es.js';
|
|
9
|
+
|
|
10
|
+
const EMPTY_DEPS = {};
|
|
11
|
+
const DEFAULT_PAYLOAD = {};
|
|
12
|
+
const getParameters = (action) => action[ACTION_PARAMETERS];
|
|
13
|
+
class ActionExecution {
|
|
14
|
+
constructor({ store, context, di, executionContextManager, actionConditionals, transformAction, }) {
|
|
15
|
+
this.actionConditionals = flatten(actionConditionals !== null && actionConditionals !== void 0 ? actionConditionals : []);
|
|
16
|
+
this.context = context;
|
|
17
|
+
this.store = store;
|
|
18
|
+
this.di = di;
|
|
19
|
+
this.executionContextManager = executionContextManager;
|
|
20
|
+
this.execution = new Map();
|
|
21
|
+
this.transformAction = transformAction || identity;
|
|
22
|
+
const initialState = store.getState(actionTramvaiReducer);
|
|
23
|
+
if (initialState && initialState.serverState) {
|
|
24
|
+
objectMap((value, key) => {
|
|
25
|
+
this.execution.set(key, value);
|
|
26
|
+
}, initialState.serverState);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async runInContext(executionContext, action, ...params) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
let parameters;
|
|
32
|
+
const payload = (_a = params[0]) !== null && _a !== void 0 ? _a : DEFAULT_PAYLOAD;
|
|
33
|
+
// TODO: replace type with pure context usage
|
|
34
|
+
const type = (executionContext === null || executionContext === void 0 ? void 0 : executionContext.values.pageActions) === true ? actionType.global : actionType.local;
|
|
35
|
+
// TODO: remove else branch after migration to new declareAction
|
|
36
|
+
if (isTramvaiAction(action)) {
|
|
37
|
+
parameters = action;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.transformAction(action);
|
|
41
|
+
parameters = getParameters(action);
|
|
42
|
+
}
|
|
43
|
+
const executionState = this.getExecutionState(parameters.name);
|
|
44
|
+
if (!this.canExecuteAction(payload, parameters, executionState, type)) {
|
|
45
|
+
switch (parameters.conditionsFailResult) {
|
|
46
|
+
case 'reject':
|
|
47
|
+
return Promise.reject(new ConditionFailError({
|
|
48
|
+
conditionName: (_b = executionState.forbiddenBy) !== null && _b !== void 0 ? _b : 'unknown',
|
|
49
|
+
targetName: parameters.name,
|
|
50
|
+
}));
|
|
51
|
+
default:
|
|
52
|
+
return Promise.resolve();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
executionState.status = 'pending';
|
|
56
|
+
return this.executionContextManager.withContext(executionContext, {
|
|
57
|
+
name: parameters.name,
|
|
58
|
+
values: (executionContext === null || executionContext === void 0 ? void 0 : executionContext.values.pageActions) === true ? { pageActions: false } : undefined,
|
|
59
|
+
}, (executionActionContext, abortController) => {
|
|
60
|
+
const context = this.createActionContext(executionContext, executionActionContext, abortController, parameters);
|
|
61
|
+
return Promise.resolve()
|
|
62
|
+
.then(() => {
|
|
63
|
+
if (isTramvaiAction(action)) {
|
|
64
|
+
return action.fn.apply(context, params);
|
|
65
|
+
}
|
|
66
|
+
return action(this.context, payload, context.deps);
|
|
67
|
+
})
|
|
68
|
+
.then((data) => {
|
|
69
|
+
executionState.status = 'success';
|
|
70
|
+
return data;
|
|
71
|
+
})
|
|
72
|
+
.catch((err) => {
|
|
73
|
+
executionState.status = 'failed';
|
|
74
|
+
throw err;
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async run(action, ...params) {
|
|
79
|
+
return this.runInContext(null, action, ...params);
|
|
80
|
+
}
|
|
81
|
+
getExecutionState(name) {
|
|
82
|
+
let executionState = this.execution.get(name);
|
|
83
|
+
// TODO: probably do not need to create executionState on client as it is not used
|
|
84
|
+
if (!executionState) {
|
|
85
|
+
executionState = { status: 'pending', state: {} };
|
|
86
|
+
this.execution.set(name, executionState);
|
|
87
|
+
}
|
|
88
|
+
return executionState;
|
|
89
|
+
}
|
|
90
|
+
canExecuteAction(payload, parameters, executionState, type) {
|
|
91
|
+
const actionChecker = new ActionChecker(this.actionConditionals, payload, parameters, executionState, type);
|
|
92
|
+
return actionChecker.check();
|
|
93
|
+
}
|
|
94
|
+
createActionContext(parentExecutionContext, actionExecutionContext, abortController, parameters) {
|
|
95
|
+
return {
|
|
96
|
+
abortController,
|
|
97
|
+
abortSignal: actionExecutionContext === null || actionExecutionContext === void 0 ? void 0 : actionExecutionContext.abortSignal,
|
|
98
|
+
executeAction: this.runInContext.bind(this, actionExecutionContext),
|
|
99
|
+
deps: parameters.deps ? this.di.getOfDeps(parameters.deps) : EMPTY_DEPS,
|
|
100
|
+
actionType: (parentExecutionContext === null || parentExecutionContext === void 0 ? void 0 : parentExecutionContext.values.pageActions) ? 'page' : 'standalone',
|
|
101
|
+
dispatch: this.store.dispatch,
|
|
102
|
+
getState: this.store.getState,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export { ActionExecution, getParameters };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var flatten = require('@tinkoff/utils/array/flatten');
|
|
6
|
+
var identity = require('@tinkoff/utils/function/identity');
|
|
7
|
+
var core = require('@tramvai/core');
|
|
8
|
+
var objectMap = require('@tinkoff/utils/object/map');
|
|
9
|
+
var errors = require('@tinkoff/errors');
|
|
10
|
+
var actionChecker = require('./actionChecker.js');
|
|
11
|
+
var constants = require('./constants.js');
|
|
12
|
+
var actionTramvaiReducer = require('./actionTramvaiReducer.js');
|
|
13
|
+
|
|
14
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
|
|
17
|
+
var identity__default = /*#__PURE__*/_interopDefaultLegacy(identity);
|
|
18
|
+
var objectMap__default = /*#__PURE__*/_interopDefaultLegacy(objectMap);
|
|
19
|
+
|
|
20
|
+
const EMPTY_DEPS = {};
|
|
21
|
+
const DEFAULT_PAYLOAD = {};
|
|
22
|
+
const getParameters = (action) => action[core.ACTION_PARAMETERS];
|
|
23
|
+
class ActionExecution {
|
|
24
|
+
constructor({ store, context, di, executionContextManager, actionConditionals, transformAction, }) {
|
|
25
|
+
this.actionConditionals = flatten__default["default"](actionConditionals !== null && actionConditionals !== void 0 ? actionConditionals : []);
|
|
26
|
+
this.context = context;
|
|
27
|
+
this.store = store;
|
|
28
|
+
this.di = di;
|
|
29
|
+
this.executionContextManager = executionContextManager;
|
|
30
|
+
this.execution = new Map();
|
|
31
|
+
this.transformAction = transformAction || identity__default["default"];
|
|
32
|
+
const initialState = store.getState(actionTramvaiReducer.actionTramvaiReducer);
|
|
33
|
+
if (initialState && initialState.serverState) {
|
|
34
|
+
objectMap__default["default"]((value, key) => {
|
|
35
|
+
this.execution.set(key, value);
|
|
36
|
+
}, initialState.serverState);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async runInContext(executionContext, action, ...params) {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
let parameters;
|
|
42
|
+
const payload = (_a = params[0]) !== null && _a !== void 0 ? _a : DEFAULT_PAYLOAD;
|
|
43
|
+
// TODO: replace type with pure context usage
|
|
44
|
+
const type = (executionContext === null || executionContext === void 0 ? void 0 : executionContext.values.pageActions) === true ? constants.actionType.global : constants.actionType.local;
|
|
45
|
+
// TODO: remove else branch after migration to new declareAction
|
|
46
|
+
if (core.isTramvaiAction(action)) {
|
|
47
|
+
parameters = action;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.transformAction(action);
|
|
51
|
+
parameters = getParameters(action);
|
|
52
|
+
}
|
|
53
|
+
const executionState = this.getExecutionState(parameters.name);
|
|
54
|
+
if (!this.canExecuteAction(payload, parameters, executionState, type)) {
|
|
55
|
+
switch (parameters.conditionsFailResult) {
|
|
56
|
+
case 'reject':
|
|
57
|
+
return Promise.reject(new errors.ConditionFailError({
|
|
58
|
+
conditionName: (_b = executionState.forbiddenBy) !== null && _b !== void 0 ? _b : 'unknown',
|
|
59
|
+
targetName: parameters.name,
|
|
60
|
+
}));
|
|
61
|
+
default:
|
|
62
|
+
return Promise.resolve();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
executionState.status = 'pending';
|
|
66
|
+
return this.executionContextManager.withContext(executionContext, {
|
|
67
|
+
name: parameters.name,
|
|
68
|
+
values: (executionContext === null || executionContext === void 0 ? void 0 : executionContext.values.pageActions) === true ? { pageActions: false } : undefined,
|
|
69
|
+
}, (executionActionContext, abortController) => {
|
|
70
|
+
const context = this.createActionContext(executionContext, executionActionContext, abortController, parameters);
|
|
71
|
+
return Promise.resolve()
|
|
72
|
+
.then(() => {
|
|
73
|
+
if (core.isTramvaiAction(action)) {
|
|
74
|
+
return action.fn.apply(context, params);
|
|
75
|
+
}
|
|
76
|
+
return action(this.context, payload, context.deps);
|
|
77
|
+
})
|
|
78
|
+
.then((data) => {
|
|
79
|
+
executionState.status = 'success';
|
|
80
|
+
return data;
|
|
81
|
+
})
|
|
82
|
+
.catch((err) => {
|
|
83
|
+
executionState.status = 'failed';
|
|
84
|
+
throw err;
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async run(action, ...params) {
|
|
89
|
+
return this.runInContext(null, action, ...params);
|
|
90
|
+
}
|
|
91
|
+
getExecutionState(name) {
|
|
92
|
+
let executionState = this.execution.get(name);
|
|
93
|
+
// TODO: probably do not need to create executionState on client as it is not used
|
|
94
|
+
if (!executionState) {
|
|
95
|
+
executionState = { status: 'pending', state: {} };
|
|
96
|
+
this.execution.set(name, executionState);
|
|
97
|
+
}
|
|
98
|
+
return executionState;
|
|
99
|
+
}
|
|
100
|
+
canExecuteAction(payload, parameters, executionState, type) {
|
|
101
|
+
const actionChecker$1 = new actionChecker.ActionChecker(this.actionConditionals, payload, parameters, executionState, type);
|
|
102
|
+
return actionChecker$1.check();
|
|
103
|
+
}
|
|
104
|
+
createActionContext(parentExecutionContext, actionExecutionContext, abortController, parameters) {
|
|
105
|
+
return {
|
|
106
|
+
abortController,
|
|
107
|
+
abortSignal: actionExecutionContext === null || actionExecutionContext === void 0 ? void 0 : actionExecutionContext.abortSignal,
|
|
108
|
+
executeAction: this.runInContext.bind(this, actionExecutionContext),
|
|
109
|
+
deps: parameters.deps ? this.di.getOfDeps(parameters.deps) : EMPTY_DEPS,
|
|
110
|
+
actionType: (parentExecutionContext === null || parentExecutionContext === void 0 ? void 0 : parentExecutionContext.values.pageActions) ? 'page' : 'standalone',
|
|
111
|
+
dispatch: this.store.dispatch,
|
|
112
|
+
getState: this.store.getState,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
exports.ActionExecution = ActionExecution;
|
|
118
|
+
exports.getParameters = getParameters;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isSilentError } from '@tinkoff/errors';
|
|
2
|
+
import { isTramvaiAction, ACTION_PARAMETERS } from '@tramvai/core';
|
|
3
|
+
|
|
4
|
+
class ActionPageRunner {
|
|
5
|
+
constructor(deps) {
|
|
6
|
+
this.deps = deps;
|
|
7
|
+
this.log = deps.logger('action:action-page-runner');
|
|
8
|
+
}
|
|
9
|
+
runActions(actions, stopRunAtError = () => false) {
|
|
10
|
+
return this.deps.executionContextManager.withContext(this.deps.commandLineExecutionContext(), { name: 'pageActions', values: { pageActions: true } }, async (executionContext) => {
|
|
11
|
+
const actionMapper = (action) => {
|
|
12
|
+
return Promise.resolve()
|
|
13
|
+
.then(() => this.deps.actionExecution.runInContext(executionContext, action))
|
|
14
|
+
.catch((error) => {
|
|
15
|
+
if (!isSilentError(error)) {
|
|
16
|
+
const parameters = isTramvaiAction(action) ? action : action[ACTION_PARAMETERS];
|
|
17
|
+
this.log.error({
|
|
18
|
+
error,
|
|
19
|
+
event: `action-execution-error`,
|
|
20
|
+
message: `${parameters.name} execution error`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (stopRunAtError(error)) {
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
await Promise.all(actions.map(actionMapper));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { ActionPageRunner };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { isTramvaiAction, ACTION_PARAMETERS } from '@tramvai/core';
|
|
2
|
+
import { isSilentError } from '@tinkoff/errors';
|
|
3
|
+
import { actionServerStateEvent } from './actionTramvaiReducer.es.js';
|
|
4
|
+
|
|
5
|
+
class ActionPageRunner {
|
|
6
|
+
constructor(deps) {
|
|
7
|
+
this.deps = deps;
|
|
8
|
+
this.log = deps.logger('action:action-page-runner');
|
|
9
|
+
}
|
|
10
|
+
// TODO stopRunAtError нужен только для редиректов на стороне сервера в экшенах. И нужно пересмотреть реализацию редиректов
|
|
11
|
+
runActions(actions, stopRunAtError = () => false) {
|
|
12
|
+
return this.deps.executionContextManager.withContext(this.deps.commandLineExecutionContext(), { name: 'pageActions', values: { pageActions: true } }, (executionContext, abortController) => {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const timeoutMarker = setTimeout(() => {
|
|
15
|
+
this.log.warn(`page actions has exceeded timeout of ${this.deps.limitTime}ms, ignore some results of execution`);
|
|
16
|
+
abortController.abort();
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
18
|
+
endChecker();
|
|
19
|
+
}, this.deps.limitTime);
|
|
20
|
+
const endChecker = () => {
|
|
21
|
+
clearTimeout(timeoutMarker);
|
|
22
|
+
const result = {};
|
|
23
|
+
// TODO: dehydrate only actions on first level as inner actions are running on client despite their execution on server
|
|
24
|
+
this.deps.actionExecution.execution.forEach((value, key) => {
|
|
25
|
+
// достаем значения экшенов, которые успешно выполнились, остальные выполнятся на клиенте
|
|
26
|
+
if (value.status === 'success') {
|
|
27
|
+
result[key] = value;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
this.syncStateActions(result);
|
|
31
|
+
resolve();
|
|
32
|
+
};
|
|
33
|
+
const actionMapper = (action) => {
|
|
34
|
+
return Promise.resolve()
|
|
35
|
+
.then(() => this.deps.actionExecution.runInContext(executionContext, action))
|
|
36
|
+
.catch((error) => {
|
|
37
|
+
if (!isSilentError(error)) {
|
|
38
|
+
const parameters = isTramvaiAction(action) ? action : action[ACTION_PARAMETERS];
|
|
39
|
+
this.log.error({
|
|
40
|
+
error,
|
|
41
|
+
event: `action-execution-error`,
|
|
42
|
+
message: `${parameters.name} execution error`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (stopRunAtError(error)) {
|
|
46
|
+
clearTimeout(timeoutMarker);
|
|
47
|
+
reject(error);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
// eslint-disable-next-line promise/catch-or-return
|
|
52
|
+
Promise.all(actions.map(actionMapper)).then(endChecker);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
syncStateActions(success) {
|
|
57
|
+
return this.deps.store.dispatch(actionServerStateEvent(success));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { ActionPageRunner };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@tramvai/core');
|
|
6
|
+
var errors = require('@tinkoff/errors');
|
|
7
|
+
var actionTramvaiReducer = require('./actionTramvaiReducer.js');
|
|
8
|
+
|
|
9
|
+
class ActionPageRunner {
|
|
10
|
+
constructor(deps) {
|
|
11
|
+
this.deps = deps;
|
|
12
|
+
this.log = deps.logger('action:action-page-runner');
|
|
13
|
+
}
|
|
14
|
+
// TODO stopRunAtError нужен только для редиректов на стороне сервера в экшенах. И нужно пересмотреть реализацию редиректов
|
|
15
|
+
runActions(actions, stopRunAtError = () => false) {
|
|
16
|
+
return this.deps.executionContextManager.withContext(this.deps.commandLineExecutionContext(), { name: 'pageActions', values: { pageActions: true } }, (executionContext, abortController) => {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const timeoutMarker = setTimeout(() => {
|
|
19
|
+
this.log.warn(`page actions has exceeded timeout of ${this.deps.limitTime}ms, ignore some results of execution`);
|
|
20
|
+
abortController.abort();
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
22
|
+
endChecker();
|
|
23
|
+
}, this.deps.limitTime);
|
|
24
|
+
const endChecker = () => {
|
|
25
|
+
clearTimeout(timeoutMarker);
|
|
26
|
+
const result = {};
|
|
27
|
+
// TODO: dehydrate only actions on first level as inner actions are running on client despite their execution on server
|
|
28
|
+
this.deps.actionExecution.execution.forEach((value, key) => {
|
|
29
|
+
// достаем значения экшенов, которые успешно выполнились, остальные выполнятся на клиенте
|
|
30
|
+
if (value.status === 'success') {
|
|
31
|
+
result[key] = value;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.syncStateActions(result);
|
|
35
|
+
resolve();
|
|
36
|
+
};
|
|
37
|
+
const actionMapper = (action) => {
|
|
38
|
+
return Promise.resolve()
|
|
39
|
+
.then(() => this.deps.actionExecution.runInContext(executionContext, action))
|
|
40
|
+
.catch((error) => {
|
|
41
|
+
if (!errors.isSilentError(error)) {
|
|
42
|
+
const parameters = core.isTramvaiAction(action) ? action : action[core.ACTION_PARAMETERS];
|
|
43
|
+
this.log.error({
|
|
44
|
+
error,
|
|
45
|
+
event: `action-execution-error`,
|
|
46
|
+
message: `${parameters.name} execution error`,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (stopRunAtError(error)) {
|
|
50
|
+
clearTimeout(timeoutMarker);
|
|
51
|
+
reject(error);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
// eslint-disable-next-line promise/catch-or-return
|
|
56
|
+
Promise.all(actions.map(actionMapper)).then(endChecker);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
syncStateActions(success) {
|
|
61
|
+
return this.deps.store.dispatch(actionTramvaiReducer.actionServerStateEvent(success));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.ActionPageRunner = ActionPageRunner;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import uniq from '@tinkoff/utils/array/uniq';
|
|
2
|
+
import difference from '@tinkoff/utils/array/difference';
|
|
3
|
+
import toArray from '@tinkoff/utils/array/toArray';
|
|
4
|
+
import flatten from '@tinkoff/utils/array/flatten';
|
|
5
|
+
|
|
6
|
+
const GLOBAL_PARAMETER = '@@global';
|
|
7
|
+
class ActionRegistry {
|
|
8
|
+
constructor({ actionsList }) {
|
|
9
|
+
this.actions = new Map([[GLOBAL_PARAMETER, flatten(actionsList)]]);
|
|
10
|
+
}
|
|
11
|
+
add(type, actions) {
|
|
12
|
+
const normalized = toArray(actions);
|
|
13
|
+
this.actions.set(type, uniq(this.actions.has(type) ? [...this.actions.get(type), ...normalized] : normalized));
|
|
14
|
+
}
|
|
15
|
+
get(type, addingActions) {
|
|
16
|
+
return uniq([...(this.actions.get(type) || []), ...(addingActions || [])]);
|
|
17
|
+
}
|
|
18
|
+
getGlobal() {
|
|
19
|
+
return this.actions.get(GLOBAL_PARAMETER);
|
|
20
|
+
}
|
|
21
|
+
remove(type, actions) {
|
|
22
|
+
if (!actions) {
|
|
23
|
+
this.actions.delete(type);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const normalized = toArray(actions);
|
|
27
|
+
if (this.actions.has(type)) {
|
|
28
|
+
this.actions.set(type, difference(this.actions.get(type), normalized));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { ActionRegistry, GLOBAL_PARAMETER };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import uniq from '@tinkoff/utils/array/uniq';
|
|
2
|
+
import difference from '@tinkoff/utils/array/difference';
|
|
3
|
+
import toArray from '@tinkoff/utils/array/toArray';
|
|
4
|
+
import flatten from '@tinkoff/utils/array/flatten';
|
|
5
|
+
|
|
6
|
+
const GLOBAL_PARAMETER = '@@global';
|
|
7
|
+
class ActionRegistry {
|
|
8
|
+
constructor({ actionsList }) {
|
|
9
|
+
this.actions = new Map([[GLOBAL_PARAMETER, flatten(actionsList)]]);
|
|
10
|
+
}
|
|
11
|
+
add(type, actions) {
|
|
12
|
+
const normalized = toArray(actions);
|
|
13
|
+
this.actions.set(type, uniq(this.actions.has(type) ? [...this.actions.get(type), ...normalized] : normalized));
|
|
14
|
+
}
|
|
15
|
+
get(type, addingActions) {
|
|
16
|
+
return uniq([...(this.actions.get(type) || []), ...(addingActions || [])]);
|
|
17
|
+
}
|
|
18
|
+
getGlobal() {
|
|
19
|
+
return this.actions.get(GLOBAL_PARAMETER);
|
|
20
|
+
}
|
|
21
|
+
remove(type, actions) {
|
|
22
|
+
if (!actions) {
|
|
23
|
+
this.actions.delete(type);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const normalized = toArray(actions);
|
|
27
|
+
if (this.actions.has(type)) {
|
|
28
|
+
this.actions.set(type, difference(this.actions.get(type), normalized));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { ActionRegistry, GLOBAL_PARAMETER };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var uniq = require('@tinkoff/utils/array/uniq');
|
|
6
|
+
var difference = require('@tinkoff/utils/array/difference');
|
|
7
|
+
var toArray = require('@tinkoff/utils/array/toArray');
|
|
8
|
+
var flatten = require('@tinkoff/utils/array/flatten');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var uniq__default = /*#__PURE__*/_interopDefaultLegacy(uniq);
|
|
13
|
+
var difference__default = /*#__PURE__*/_interopDefaultLegacy(difference);
|
|
14
|
+
var toArray__default = /*#__PURE__*/_interopDefaultLegacy(toArray);
|
|
15
|
+
var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
|
|
16
|
+
|
|
17
|
+
const GLOBAL_PARAMETER = '@@global';
|
|
18
|
+
class ActionRegistry {
|
|
19
|
+
constructor({ actionsList }) {
|
|
20
|
+
this.actions = new Map([[GLOBAL_PARAMETER, flatten__default["default"](actionsList)]]);
|
|
21
|
+
}
|
|
22
|
+
add(type, actions) {
|
|
23
|
+
const normalized = toArray__default["default"](actions);
|
|
24
|
+
this.actions.set(type, uniq__default["default"](this.actions.has(type) ? [...this.actions.get(type), ...normalized] : normalized));
|
|
25
|
+
}
|
|
26
|
+
get(type, addingActions) {
|
|
27
|
+
return uniq__default["default"]([...(this.actions.get(type) || []), ...(addingActions || [])]);
|
|
28
|
+
}
|
|
29
|
+
getGlobal() {
|
|
30
|
+
return this.actions.get(GLOBAL_PARAMETER);
|
|
31
|
+
}
|
|
32
|
+
remove(type, actions) {
|
|
33
|
+
if (!actions) {
|
|
34
|
+
this.actions.delete(type);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const normalized = toArray__default["default"](actions);
|
|
38
|
+
if (this.actions.has(type)) {
|
|
39
|
+
this.actions.set(type, difference__default["default"](this.actions.get(type), normalized));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.ActionRegistry = ActionRegistry;
|
|
45
|
+
exports.GLOBAL_PARAMETER = GLOBAL_PARAMETER;
|