@tramvai/module-common 2.70.1 → 2.72.3

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.
Files changed (123) hide show
  1. package/lib/CommonModule.browser.js +100 -0
  2. package/lib/CommonModule.es.js +100 -0
  3. package/lib/CommonModule.js +106 -0
  4. package/lib/actions/ActionModule.browser.js +79 -0
  5. package/lib/actions/ActionModule.es.js +79 -0
  6. package/lib/actions/ActionModule.js +82 -0
  7. package/lib/actions/actionChecker.browser.js +52 -0
  8. package/lib/actions/actionChecker.es.js +52 -0
  9. package/lib/actions/actionChecker.js +56 -0
  10. package/lib/actions/actionExecution.browser.js +107 -0
  11. package/lib/actions/actionExecution.es.js +107 -0
  12. package/lib/actions/actionExecution.js +118 -0
  13. package/lib/actions/actionPageRunner.browser.browser.js +33 -0
  14. package/lib/actions/actionPageRunner.es.js +61 -0
  15. package/lib/actions/actionPageRunner.js +65 -0
  16. package/lib/actions/actionRegistry.browser.js +33 -0
  17. package/lib/actions/actionRegistry.es.js +33 -0
  18. package/lib/actions/actionRegistry.js +45 -0
  19. package/lib/actions/actionTramvaiReducer.browser.js +10 -0
  20. package/lib/actions/actionTramvaiReducer.es.js +10 -0
  21. package/lib/actions/actionTramvaiReducer.js +15 -0
  22. package/lib/actions/conditions/always.browser.js +10 -0
  23. package/lib/actions/conditions/always.es.js +10 -0
  24. package/lib/actions/conditions/always.js +14 -0
  25. package/lib/actions/conditions/helpers.browser.js +4 -0
  26. package/lib/actions/conditions/helpers.es.js +4 -0
  27. package/lib/actions/conditions/helpers.js +9 -0
  28. package/lib/actions/conditions/onlyBrowser.browser.js +12 -0
  29. package/lib/actions/conditions/onlyBrowser.es.js +12 -0
  30. package/lib/actions/conditions/onlyBrowser.js +16 -0
  31. package/lib/actions/conditions/onlyServer.browser.js +12 -0
  32. package/lib/actions/conditions/onlyServer.es.js +12 -0
  33. package/lib/actions/conditions/onlyServer.js +16 -0
  34. package/lib/actions/conditions/pageBrowser.browser.js +13 -0
  35. package/lib/actions/conditions/pageBrowser.es.js +13 -0
  36. package/lib/actions/conditions/pageBrowser.js +17 -0
  37. package/lib/actions/conditions/pageServer.browser.js +13 -0
  38. package/lib/actions/conditions/pageServer.es.js +13 -0
  39. package/lib/actions/conditions/pageServer.js +17 -0
  40. package/lib/actions/constants.browser.js +9 -0
  41. package/lib/actions/constants.es.js +9 -0
  42. package/lib/actions/constants.js +13 -0
  43. package/lib/bundleManager/bundleManager.browser.js +105 -0
  44. package/lib/bundleManager/bundleManager.es.js +105 -0
  45. package/lib/bundleManager/bundleManager.js +115 -0
  46. package/lib/cache/CacheModule.browser.js +58 -0
  47. package/lib/cache/CacheModule.es.js +58 -0
  48. package/lib/cache/CacheModule.js +62 -0
  49. package/lib/cache/cacheFactory.browser.js +7 -0
  50. package/lib/cache/cacheFactory.es.js +7 -0
  51. package/lib/cache/cacheFactory.js +15 -0
  52. package/lib/cache/clientProviders.browser.js +3 -0
  53. package/lib/cache/papi.es.js +18 -0
  54. package/lib/cache/papi.js +22 -0
  55. package/lib/cache/serverProviders.es.js +18 -0
  56. package/lib/cache/serverProviders.js +22 -0
  57. package/lib/child-app/ChildAppModule.browser.js +38 -0
  58. package/lib/child-app/ChildAppModule.es.js +38 -0
  59. package/lib/child-app/ChildAppModule.js +40 -0
  60. package/lib/child-app/actions.browser.js +19 -0
  61. package/lib/child-app/actions.es.js +19 -0
  62. package/lib/child-app/actions.js +23 -0
  63. package/lib/child-app/clientProviders.browser.js +18 -0
  64. package/lib/child-app/command.browser.js +35 -0
  65. package/lib/child-app/command.es.js +35 -0
  66. package/lib/child-app/command.js +40 -0
  67. package/lib/child-app/serverProviders.es.js +3 -0
  68. package/lib/child-app/serverProviders.js +7 -0
  69. package/lib/child-app/state.browser.js +13 -0
  70. package/lib/child-app/state.es.js +13 -0
  71. package/lib/child-app/state.js +17 -0
  72. package/lib/command/CommandModule.browser.js +52 -0
  73. package/lib/command/CommandModule.es.js +52 -0
  74. package/lib/command/CommandModule.js +54 -0
  75. package/lib/command/commandLineRunner.browser.js +147 -0
  76. package/lib/command/commandLineRunner.es.js +147 -0
  77. package/lib/command/commandLineRunner.js +151 -0
  78. package/lib/command/defaultLines.browser.js +25 -0
  79. package/lib/command/defaultLines.es.js +25 -0
  80. package/lib/command/defaultLines.js +29 -0
  81. package/lib/componentRegistry/componentRegistry.browser.js +25 -0
  82. package/lib/componentRegistry/componentRegistry.es.js +25 -0
  83. package/lib/componentRegistry/componentRegistry.js +34 -0
  84. package/lib/createConsumerContext/createConsumerContext.browser.js +62 -0
  85. package/lib/createConsumerContext/createConsumerContext.es.js +62 -0
  86. package/lib/createConsumerContext/createConsumerContext.js +67 -0
  87. package/lib/executionContext/executionContextManager.browser.js +57 -0
  88. package/lib/executionContext/executionContextManager.es.js +57 -0
  89. package/lib/executionContext/executionContextManager.js +61 -0
  90. package/lib/index.browser.js +13 -1300
  91. package/lib/index.es.js +13 -1318
  92. package/lib/index.js +38 -1341
  93. package/lib/providers/clientProviders.browser.js +22 -0
  94. package/lib/providers/serverProviders.es.js +3 -0
  95. package/lib/providers/serverProviders.js +7 -0
  96. package/lib/pubsub/PubSubModule.browser.js +44 -0
  97. package/lib/pubsub/PubSubModule.es.js +44 -0
  98. package/lib/pubsub/PubSubModule.js +46 -0
  99. package/lib/requestManager/RequestManagerModule.browser.browser.js +38 -0
  100. package/lib/requestManager/RequestManagerModule.es.js +35 -0
  101. package/lib/requestManager/RequestManagerModule.js +37 -0
  102. package/lib/requestManager/RequestManagerStore.browser.js +8 -0
  103. package/lib/requestManager/RequestManagerStore.es.js +8 -0
  104. package/lib/requestManager/RequestManagerStore.js +13 -0
  105. package/lib/requestManager/requestManager.browser.js +57 -0
  106. package/lib/requestManager/requestManager.es.js +57 -0
  107. package/lib/requestManager/requestManager.js +61 -0
  108. package/lib/requestManager/sharedProviders.browser.js +23 -0
  109. package/lib/requestManager/sharedProviders.es.js +23 -0
  110. package/lib/requestManager/sharedProviders.js +27 -0
  111. package/lib/responseManager/ResponseManagerModule.browser.browser.js +21 -0
  112. package/lib/responseManager/ResponseManagerModule.es.js +13 -0
  113. package/lib/responseManager/ResponseManagerModule.js +15 -0
  114. package/lib/responseManager/responseManager.browser.js +52 -0
  115. package/lib/responseManager/responseManager.es.js +52 -0
  116. package/lib/responseManager/responseManager.js +61 -0
  117. package/lib/responseManager/sharedProviders.browser.js +13 -0
  118. package/lib/responseManager/sharedProviders.es.js +13 -0
  119. package/lib/responseManager/sharedProviders.js +17 -0
  120. package/lib/state/StateModule.browser.js +54 -0
  121. package/lib/state/StateModule.es.js +54 -0
  122. package/lib/state/StateModule.js +60 -0
  123. package/package.json +24 -25
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var core = require('@tramvai/core');
6
+ var tokensChildApp = require('@tramvai/tokens-child-app');
7
+ var tokensCommon = require('@tramvai/tokens-common');
8
+
9
+ const stateProviders = [
10
+ core.provide({
11
+ provide: tokensChildApp.CHILD_APP_INTERNAL_ROOT_DI_BORROW_TOKEN,
12
+ multi: true,
13
+ useValue: [tokensCommon.DISPATCHER_TOKEN, tokensCommon.STORE_TOKEN, tokensCommon.CONTEXT_TOKEN],
14
+ }),
15
+ ];
16
+
17
+ exports.stateProviders = stateProviders;
@@ -0,0 +1,52 @@
1
+ import { __decorate } from 'tslib';
2
+ import { Module, COMMAND_LINE_RUNNER_TOKEN, COMMAND_LINES_TOKEN, DI_TOKEN } from '@tramvai/core';
3
+ import { COMMAND_LINE_EXECUTION_END_TOKEN } from '@tramvai/tokens-core-private';
4
+ import { LOGGER_TOKEN, EXECUTION_CONTEXT_MANAGER_TOKEN, COMMAND_LINE_EXECUTION_CONTEXT_TOKEN } from '@tramvai/tokens-common';
5
+ import { provide, Scope } from '@tinkoff/dippy';
6
+ import { CommandLineRunner } from './commandLineRunner.browser.js';
7
+ import { lines } from './defaultLines.browser.js';
8
+
9
+ let CommandModule = class CommandModule {
10
+ };
11
+ CommandModule = __decorate([
12
+ Module({
13
+ providers: [
14
+ provide({
15
+ // Раннер процессов
16
+ provide: COMMAND_LINE_RUNNER_TOKEN,
17
+ scope: Scope.SINGLETON,
18
+ useClass: CommandLineRunner,
19
+ deps: {
20
+ lines: COMMAND_LINES_TOKEN,
21
+ rootDi: DI_TOKEN,
22
+ logger: LOGGER_TOKEN,
23
+ executionContextManager: EXECUTION_CONTEXT_MANAGER_TOKEN,
24
+ executionEndHandlers: {
25
+ token: COMMAND_LINE_EXECUTION_END_TOKEN,
26
+ optional: true,
27
+ },
28
+ },
29
+ }),
30
+ provide({
31
+ provide: COMMAND_LINE_EXECUTION_CONTEXT_TOKEN,
32
+ useFactory: ({ di, commandLineRunner }) => {
33
+ return () => {
34
+ return commandLineRunner.resolveExecutionContextFromDi(di);
35
+ };
36
+ },
37
+ deps: {
38
+ di: DI_TOKEN,
39
+ commandLineRunner: COMMAND_LINE_RUNNER_TOKEN,
40
+ },
41
+ }),
42
+ provide({
43
+ // Дефолтный список команл
44
+ provide: COMMAND_LINES_TOKEN,
45
+ scope: Scope.SINGLETON,
46
+ useValue: lines,
47
+ }),
48
+ ],
49
+ })
50
+ ], CommandModule);
51
+
52
+ export { CommandModule };
@@ -0,0 +1,52 @@
1
+ import { __decorate } from 'tslib';
2
+ import { Module, COMMAND_LINE_RUNNER_TOKEN, COMMAND_LINES_TOKEN, DI_TOKEN } from '@tramvai/core';
3
+ import { COMMAND_LINE_EXECUTION_END_TOKEN } from '@tramvai/tokens-core-private';
4
+ import { LOGGER_TOKEN, EXECUTION_CONTEXT_MANAGER_TOKEN, COMMAND_LINE_EXECUTION_CONTEXT_TOKEN } from '@tramvai/tokens-common';
5
+ import { provide, Scope } from '@tinkoff/dippy';
6
+ import { CommandLineRunner } from './commandLineRunner.es.js';
7
+ import { lines } from './defaultLines.es.js';
8
+
9
+ let CommandModule = class CommandModule {
10
+ };
11
+ CommandModule = __decorate([
12
+ Module({
13
+ providers: [
14
+ provide({
15
+ // Раннер процессов
16
+ provide: COMMAND_LINE_RUNNER_TOKEN,
17
+ scope: Scope.SINGLETON,
18
+ useClass: CommandLineRunner,
19
+ deps: {
20
+ lines: COMMAND_LINES_TOKEN,
21
+ rootDi: DI_TOKEN,
22
+ logger: LOGGER_TOKEN,
23
+ executionContextManager: EXECUTION_CONTEXT_MANAGER_TOKEN,
24
+ executionEndHandlers: {
25
+ token: COMMAND_LINE_EXECUTION_END_TOKEN,
26
+ optional: true,
27
+ },
28
+ },
29
+ }),
30
+ provide({
31
+ provide: COMMAND_LINE_EXECUTION_CONTEXT_TOKEN,
32
+ useFactory: ({ di, commandLineRunner }) => {
33
+ return () => {
34
+ return commandLineRunner.resolveExecutionContextFromDi(di);
35
+ };
36
+ },
37
+ deps: {
38
+ di: DI_TOKEN,
39
+ commandLineRunner: COMMAND_LINE_RUNNER_TOKEN,
40
+ },
41
+ }),
42
+ provide({
43
+ // Дефолтный список команл
44
+ provide: COMMAND_LINES_TOKEN,
45
+ scope: Scope.SINGLETON,
46
+ useValue: lines,
47
+ }),
48
+ ],
49
+ })
50
+ ], CommandModule);
51
+
52
+ export { CommandModule };
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var core = require('@tramvai/core');
7
+ var tokensCorePrivate = require('@tramvai/tokens-core-private');
8
+ var tokensCommon = require('@tramvai/tokens-common');
9
+ var dippy = require('@tinkoff/dippy');
10
+ var commandLineRunner = require('./commandLineRunner.js');
11
+ var defaultLines = require('./defaultLines.js');
12
+
13
+ exports.CommandModule = class CommandModule {
14
+ };
15
+ exports.CommandModule = tslib.__decorate([
16
+ core.Module({
17
+ providers: [
18
+ dippy.provide({
19
+ // Раннер процессов
20
+ provide: core.COMMAND_LINE_RUNNER_TOKEN,
21
+ scope: dippy.Scope.SINGLETON,
22
+ useClass: commandLineRunner.CommandLineRunner,
23
+ deps: {
24
+ lines: core.COMMAND_LINES_TOKEN,
25
+ rootDi: core.DI_TOKEN,
26
+ logger: tokensCommon.LOGGER_TOKEN,
27
+ executionContextManager: tokensCommon.EXECUTION_CONTEXT_MANAGER_TOKEN,
28
+ executionEndHandlers: {
29
+ token: tokensCorePrivate.COMMAND_LINE_EXECUTION_END_TOKEN,
30
+ optional: true,
31
+ },
32
+ },
33
+ }),
34
+ dippy.provide({
35
+ provide: tokensCommon.COMMAND_LINE_EXECUTION_CONTEXT_TOKEN,
36
+ useFactory: ({ di, commandLineRunner }) => {
37
+ return () => {
38
+ return commandLineRunner.resolveExecutionContextFromDi(di);
39
+ };
40
+ },
41
+ deps: {
42
+ di: core.DI_TOKEN,
43
+ commandLineRunner: core.COMMAND_LINE_RUNNER_TOKEN,
44
+ },
45
+ }),
46
+ dippy.provide({
47
+ // Дефолтный список команл
48
+ provide: core.COMMAND_LINES_TOKEN,
49
+ scope: dippy.Scope.SINGLETON,
50
+ useValue: defaultLines.lines,
51
+ }),
52
+ ],
53
+ })
54
+ ], exports.CommandModule);
@@ -0,0 +1,147 @@
1
+ import { isSilentError } from '@tinkoff/errors';
2
+ import { COMMAND_LINE_TIMING_INFO_TOKEN } from '@tramvai/tokens-core-private';
3
+ import { createChildContainer } from '@tinkoff/dippy';
4
+ import { ROOT_EXECUTION_CONTEXT_TOKEN } from '@tramvai/tokens-common';
5
+
6
+ const resolveDi = (type, status, diContainer, providers) => {
7
+ let di = diContainer;
8
+ if (status === 'customer' && type !== 'client') {
9
+ di = createChildContainer(di);
10
+ }
11
+ if (providers) {
12
+ providers.forEach((item) => {
13
+ return di.register(item);
14
+ });
15
+ }
16
+ return di;
17
+ };
18
+ class CommandLineRunner {
19
+ constructor({ lines, rootDi, logger, executionContextManager, executionEndHandlers }) {
20
+ this.executionContextByDi = new WeakMap();
21
+ this.abortControllerByDi = new WeakMap();
22
+ this.lines = lines;
23
+ this.rootDi = rootDi;
24
+ this.log = logger('command:command-line-runner');
25
+ this.executionContextManager = executionContextManager;
26
+ this.executionEndHandlers = executionEndHandlers;
27
+ }
28
+ run(type, status, providers, customDi) {
29
+ const di = customDi !== null && customDi !== void 0 ? customDi : resolveDi(type, status, this.rootDi, providers);
30
+ const rootExecutionContext = di.get({ token: ROOT_EXECUTION_CONTEXT_TOKEN, optional: true });
31
+ this.log.debug({
32
+ event: 'command-run',
33
+ type,
34
+ status,
35
+ });
36
+ const timingInfo = {};
37
+ di.register({ provide: COMMAND_LINE_TIMING_INFO_TOKEN, useValue: timingInfo });
38
+ return (this.lines[type][status]
39
+ .reduce((chain, line) => {
40
+ return chain.then(() => {
41
+ const lineName = line.toString();
42
+ timingInfo[lineName] = { start: performance.now() };
43
+ // eslint-disable-next-line promise/no-nesting
44
+ return Promise.resolve()
45
+ .then(() => {
46
+ return this.executionContextManager.withContext(rootExecutionContext, `command-line:${lineName}`, async (executionContext, abortController) => {
47
+ this.executionContextByDi.set(di, executionContext);
48
+ this.abortControllerByDi.set(di, abortController);
49
+ await this.createLineChain(di, line);
50
+ });
51
+ })
52
+ .finally(() => {
53
+ timingInfo[lineName].end = performance.now();
54
+ });
55
+ });
56
+ }, Promise.resolve())
57
+ // После завершения цепочки отдаем context выполнения
58
+ .finally(() => {
59
+ this.executionContextByDi.delete(di);
60
+ this.abortControllerByDi.delete(di);
61
+ if (this.executionEndHandlers) {
62
+ for (const executionEndHandler of this.executionEndHandlers) {
63
+ executionEndHandler(di, type, status, timingInfo);
64
+ }
65
+ }
66
+ })
67
+ .then(() => di));
68
+ }
69
+ resolveExecutionContextFromDi(di) {
70
+ var _a;
71
+ return (_a = this.executionContextByDi.get(di)) !== null && _a !== void 0 ? _a : null;
72
+ }
73
+ createLineChain(di, line) {
74
+ let lineInstance;
75
+ try {
76
+ lineInstance = di.get({ token: line, optional: true });
77
+ // Пропускаем step. Так как нет действий
78
+ if (lineInstance === null) {
79
+ return Promise.resolve();
80
+ }
81
+ }
82
+ catch (e) {
83
+ // Логируем ошибку и дальше падаем
84
+ this.log.error(e);
85
+ return this.throwError(e, di);
86
+ }
87
+ if (!Array.isArray(lineInstance)) {
88
+ return this.instanceExecute(lineInstance, line, di);
89
+ }
90
+ return Promise.all(lineInstance.map((instance) => {
91
+ return this.instanceExecute(instance, line, di);
92
+ }));
93
+ }
94
+ instanceExecute(instance, line, di) {
95
+ if (!(instance instanceof Function)) {
96
+ const error = new TypeError(`Expected function in line processing "commandLineListTokens.${line.toString()}", received "${instance}".
97
+ Check that all commandLineListTokens subscribers return functions`);
98
+ if (process.env.NODE_ENV !== 'production') {
99
+ const instances = di.get(line);
100
+ const record = di.getRecord(line.toString());
101
+ // пробегаемся по всем инстансам и для текущего получаем его запись, из которой можно получить стек
102
+ for (let i = 0; i < instances.length; i++) {
103
+ if (instances[i] === instance) {
104
+ // @ts-expect-error
105
+ error.stack = `${error.stack}\n---- caused by: ----\n${record.multi[i].stack || ''}`;
106
+ }
107
+ }
108
+ }
109
+ this.log.error({
110
+ event: 'line-error',
111
+ error,
112
+ line: line.toString(),
113
+ });
114
+ return;
115
+ }
116
+ const { name = '' } = instance;
117
+ this.log.debug({
118
+ event: 'line-run',
119
+ line: line.toString(),
120
+ command: name,
121
+ });
122
+ return Promise.resolve()
123
+ .then(() => instance())
124
+ .catch((err) => {
125
+ var _a;
126
+ this.log[isSilentError(err) ? 'debug' : 'error']({
127
+ event: 'line-error',
128
+ error: err,
129
+ line: line.toString(),
130
+ command: name,
131
+ });
132
+ // in case if any error happens during line execution results from other line handlers will not be used anyway
133
+ (_a = this.abortControllerByDi.get(di)) === null || _a === void 0 ? void 0 : _a.abort();
134
+ this.throwError(err, di);
135
+ });
136
+ }
137
+ // eslint-disable-next-line class-methods-use-this
138
+ throwError(err, di) {
139
+ if (typeof err === 'object') {
140
+ // eslint-disable-next-line no-param-reassign
141
+ err.di = di;
142
+ }
143
+ throw err;
144
+ }
145
+ }
146
+
147
+ export { CommandLineRunner };
@@ -0,0 +1,147 @@
1
+ import { isSilentError } from '@tinkoff/errors';
2
+ import { COMMAND_LINE_TIMING_INFO_TOKEN } from '@tramvai/tokens-core-private';
3
+ import { createChildContainer } from '@tinkoff/dippy';
4
+ import { ROOT_EXECUTION_CONTEXT_TOKEN } from '@tramvai/tokens-common';
5
+
6
+ const resolveDi = (type, status, diContainer, providers) => {
7
+ let di = diContainer;
8
+ if (status === 'customer' && type !== 'client') {
9
+ di = createChildContainer(di);
10
+ }
11
+ if (providers) {
12
+ providers.forEach((item) => {
13
+ return di.register(item);
14
+ });
15
+ }
16
+ return di;
17
+ };
18
+ class CommandLineRunner {
19
+ constructor({ lines, rootDi, logger, executionContextManager, executionEndHandlers }) {
20
+ this.executionContextByDi = new WeakMap();
21
+ this.abortControllerByDi = new WeakMap();
22
+ this.lines = lines;
23
+ this.rootDi = rootDi;
24
+ this.log = logger('command:command-line-runner');
25
+ this.executionContextManager = executionContextManager;
26
+ this.executionEndHandlers = executionEndHandlers;
27
+ }
28
+ run(type, status, providers, customDi) {
29
+ const di = customDi !== null && customDi !== void 0 ? customDi : resolveDi(type, status, this.rootDi, providers);
30
+ const rootExecutionContext = di.get({ token: ROOT_EXECUTION_CONTEXT_TOKEN, optional: true });
31
+ this.log.debug({
32
+ event: 'command-run',
33
+ type,
34
+ status,
35
+ });
36
+ const timingInfo = {};
37
+ di.register({ provide: COMMAND_LINE_TIMING_INFO_TOKEN, useValue: timingInfo });
38
+ return (this.lines[type][status]
39
+ .reduce((chain, line) => {
40
+ return chain.then(() => {
41
+ const lineName = line.toString();
42
+ timingInfo[lineName] = { start: performance.now() };
43
+ // eslint-disable-next-line promise/no-nesting
44
+ return Promise.resolve()
45
+ .then(() => {
46
+ return this.executionContextManager.withContext(rootExecutionContext, `command-line:${lineName}`, async (executionContext, abortController) => {
47
+ this.executionContextByDi.set(di, executionContext);
48
+ this.abortControllerByDi.set(di, abortController);
49
+ await this.createLineChain(di, line);
50
+ });
51
+ })
52
+ .finally(() => {
53
+ timingInfo[lineName].end = performance.now();
54
+ });
55
+ });
56
+ }, Promise.resolve())
57
+ // После завершения цепочки отдаем context выполнения
58
+ .finally(() => {
59
+ this.executionContextByDi.delete(di);
60
+ this.abortControllerByDi.delete(di);
61
+ if (this.executionEndHandlers) {
62
+ for (const executionEndHandler of this.executionEndHandlers) {
63
+ executionEndHandler(di, type, status, timingInfo);
64
+ }
65
+ }
66
+ })
67
+ .then(() => di));
68
+ }
69
+ resolveExecutionContextFromDi(di) {
70
+ var _a;
71
+ return (_a = this.executionContextByDi.get(di)) !== null && _a !== void 0 ? _a : null;
72
+ }
73
+ createLineChain(di, line) {
74
+ let lineInstance;
75
+ try {
76
+ lineInstance = di.get({ token: line, optional: true });
77
+ // Пропускаем step. Так как нет действий
78
+ if (lineInstance === null) {
79
+ return Promise.resolve();
80
+ }
81
+ }
82
+ catch (e) {
83
+ // Логируем ошибку и дальше падаем
84
+ this.log.error(e);
85
+ return this.throwError(e, di);
86
+ }
87
+ if (!Array.isArray(lineInstance)) {
88
+ return this.instanceExecute(lineInstance, line, di);
89
+ }
90
+ return Promise.all(lineInstance.map((instance) => {
91
+ return this.instanceExecute(instance, line, di);
92
+ }));
93
+ }
94
+ instanceExecute(instance, line, di) {
95
+ if (!(instance instanceof Function)) {
96
+ const error = new TypeError(`Expected function in line processing "commandLineListTokens.${line.toString()}", received "${instance}".
97
+ Check that all commandLineListTokens subscribers return functions`);
98
+ if (process.env.NODE_ENV !== 'production') {
99
+ const instances = di.get(line);
100
+ const record = di.getRecord(line.toString());
101
+ // пробегаемся по всем инстансам и для текущего получаем его запись, из которой можно получить стек
102
+ for (let i = 0; i < instances.length; i++) {
103
+ if (instances[i] === instance) {
104
+ // @ts-expect-error
105
+ error.stack = `${error.stack}\n---- caused by: ----\n${record.multi[i].stack || ''}`;
106
+ }
107
+ }
108
+ }
109
+ this.log.error({
110
+ event: 'line-error',
111
+ error,
112
+ line: line.toString(),
113
+ });
114
+ return;
115
+ }
116
+ const { name = '' } = instance;
117
+ this.log.debug({
118
+ event: 'line-run',
119
+ line: line.toString(),
120
+ command: name,
121
+ });
122
+ return Promise.resolve()
123
+ .then(() => instance())
124
+ .catch((err) => {
125
+ var _a;
126
+ this.log[isSilentError(err) ? 'debug' : 'error']({
127
+ event: 'line-error',
128
+ error: err,
129
+ line: line.toString(),
130
+ command: name,
131
+ });
132
+ // in case if any error happens during line execution results from other line handlers will not be used anyway
133
+ (_a = this.abortControllerByDi.get(di)) === null || _a === void 0 ? void 0 : _a.abort();
134
+ this.throwError(err, di);
135
+ });
136
+ }
137
+ // eslint-disable-next-line class-methods-use-this
138
+ throwError(err, di) {
139
+ if (typeof err === 'object') {
140
+ // eslint-disable-next-line no-param-reassign
141
+ err.di = di;
142
+ }
143
+ throw err;
144
+ }
145
+ }
146
+
147
+ export { CommandLineRunner };
@@ -0,0 +1,151 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var errors = require('@tinkoff/errors');
6
+ var tokensCorePrivate = require('@tramvai/tokens-core-private');
7
+ var dippy = require('@tinkoff/dippy');
8
+ var tokensCommon = require('@tramvai/tokens-common');
9
+
10
+ const resolveDi = (type, status, diContainer, providers) => {
11
+ let di = diContainer;
12
+ if (status === 'customer' && type !== 'client') {
13
+ di = dippy.createChildContainer(di);
14
+ }
15
+ if (providers) {
16
+ providers.forEach((item) => {
17
+ return di.register(item);
18
+ });
19
+ }
20
+ return di;
21
+ };
22
+ class CommandLineRunner {
23
+ constructor({ lines, rootDi, logger, executionContextManager, executionEndHandlers }) {
24
+ this.executionContextByDi = new WeakMap();
25
+ this.abortControllerByDi = new WeakMap();
26
+ this.lines = lines;
27
+ this.rootDi = rootDi;
28
+ this.log = logger('command:command-line-runner');
29
+ this.executionContextManager = executionContextManager;
30
+ this.executionEndHandlers = executionEndHandlers;
31
+ }
32
+ run(type, status, providers, customDi) {
33
+ const di = customDi !== null && customDi !== void 0 ? customDi : resolveDi(type, status, this.rootDi, providers);
34
+ const rootExecutionContext = di.get({ token: tokensCommon.ROOT_EXECUTION_CONTEXT_TOKEN, optional: true });
35
+ this.log.debug({
36
+ event: 'command-run',
37
+ type,
38
+ status,
39
+ });
40
+ const timingInfo = {};
41
+ di.register({ provide: tokensCorePrivate.COMMAND_LINE_TIMING_INFO_TOKEN, useValue: timingInfo });
42
+ return (this.lines[type][status]
43
+ .reduce((chain, line) => {
44
+ return chain.then(() => {
45
+ const lineName = line.toString();
46
+ timingInfo[lineName] = { start: performance.now() };
47
+ // eslint-disable-next-line promise/no-nesting
48
+ return Promise.resolve()
49
+ .then(() => {
50
+ return this.executionContextManager.withContext(rootExecutionContext, `command-line:${lineName}`, async (executionContext, abortController) => {
51
+ this.executionContextByDi.set(di, executionContext);
52
+ this.abortControllerByDi.set(di, abortController);
53
+ await this.createLineChain(di, line);
54
+ });
55
+ })
56
+ .finally(() => {
57
+ timingInfo[lineName].end = performance.now();
58
+ });
59
+ });
60
+ }, Promise.resolve())
61
+ // После завершения цепочки отдаем context выполнения
62
+ .finally(() => {
63
+ this.executionContextByDi.delete(di);
64
+ this.abortControllerByDi.delete(di);
65
+ if (this.executionEndHandlers) {
66
+ for (const executionEndHandler of this.executionEndHandlers) {
67
+ executionEndHandler(di, type, status, timingInfo);
68
+ }
69
+ }
70
+ })
71
+ .then(() => di));
72
+ }
73
+ resolveExecutionContextFromDi(di) {
74
+ var _a;
75
+ return (_a = this.executionContextByDi.get(di)) !== null && _a !== void 0 ? _a : null;
76
+ }
77
+ createLineChain(di, line) {
78
+ let lineInstance;
79
+ try {
80
+ lineInstance = di.get({ token: line, optional: true });
81
+ // Пропускаем step. Так как нет действий
82
+ if (lineInstance === null) {
83
+ return Promise.resolve();
84
+ }
85
+ }
86
+ catch (e) {
87
+ // Логируем ошибку и дальше падаем
88
+ this.log.error(e);
89
+ return this.throwError(e, di);
90
+ }
91
+ if (!Array.isArray(lineInstance)) {
92
+ return this.instanceExecute(lineInstance, line, di);
93
+ }
94
+ return Promise.all(lineInstance.map((instance) => {
95
+ return this.instanceExecute(instance, line, di);
96
+ }));
97
+ }
98
+ instanceExecute(instance, line, di) {
99
+ if (!(instance instanceof Function)) {
100
+ const error = new TypeError(`Expected function in line processing "commandLineListTokens.${line.toString()}", received "${instance}".
101
+ Check that all commandLineListTokens subscribers return functions`);
102
+ if (process.env.NODE_ENV !== 'production') {
103
+ const instances = di.get(line);
104
+ const record = di.getRecord(line.toString());
105
+ // пробегаемся по всем инстансам и для текущего получаем его запись, из которой можно получить стек
106
+ for (let i = 0; i < instances.length; i++) {
107
+ if (instances[i] === instance) {
108
+ // @ts-expect-error
109
+ error.stack = `${error.stack}\n---- caused by: ----\n${record.multi[i].stack || ''}`;
110
+ }
111
+ }
112
+ }
113
+ this.log.error({
114
+ event: 'line-error',
115
+ error,
116
+ line: line.toString(),
117
+ });
118
+ return;
119
+ }
120
+ const { name = '' } = instance;
121
+ this.log.debug({
122
+ event: 'line-run',
123
+ line: line.toString(),
124
+ command: name,
125
+ });
126
+ return Promise.resolve()
127
+ .then(() => instance())
128
+ .catch((err) => {
129
+ var _a;
130
+ this.log[errors.isSilentError(err) ? 'debug' : 'error']({
131
+ event: 'line-error',
132
+ error: err,
133
+ line: line.toString(),
134
+ command: name,
135
+ });
136
+ // in case if any error happens during line execution results from other line handlers will not be used anyway
137
+ (_a = this.abortControllerByDi.get(di)) === null || _a === void 0 ? void 0 : _a.abort();
138
+ this.throwError(err, di);
139
+ });
140
+ }
141
+ // eslint-disable-next-line class-methods-use-this
142
+ throwError(err, di) {
143
+ if (typeof err === 'object') {
144
+ // eslint-disable-next-line no-param-reassign
145
+ err.di = di;
146
+ }
147
+ throw err;
148
+ }
149
+ }
150
+
151
+ exports.CommandLineRunner = CommandLineRunner;
@@ -0,0 +1,25 @@
1
+ import { commandLineListTokens } from '@tramvai/core';
2
+
3
+ const command = {
4
+ init: [commandLineListTokens.init, commandLineListTokens.listen],
5
+ close: [commandLineListTokens.close],
6
+ customer: [
7
+ commandLineListTokens.customerStart,
8
+ commandLineListTokens.resolveUserDeps,
9
+ commandLineListTokens.resolvePageDeps,
10
+ commandLineListTokens.generatePage,
11
+ commandLineListTokens.clear,
12
+ ],
13
+ spa: [
14
+ commandLineListTokens.resolveUserDeps,
15
+ commandLineListTokens.resolvePageDeps,
16
+ commandLineListTokens.spaTransition,
17
+ ],
18
+ afterSpa: [commandLineListTokens.afterSpaTransition],
19
+ };
20
+ const lines = {
21
+ server: command,
22
+ client: command,
23
+ };
24
+
25
+ export { lines };
@@ -0,0 +1,25 @@
1
+ import { commandLineListTokens } from '@tramvai/core';
2
+
3
+ const command = {
4
+ init: [commandLineListTokens.init, commandLineListTokens.listen],
5
+ close: [commandLineListTokens.close],
6
+ customer: [
7
+ commandLineListTokens.customerStart,
8
+ commandLineListTokens.resolveUserDeps,
9
+ commandLineListTokens.resolvePageDeps,
10
+ commandLineListTokens.generatePage,
11
+ commandLineListTokens.clear,
12
+ ],
13
+ spa: [
14
+ commandLineListTokens.resolveUserDeps,
15
+ commandLineListTokens.resolvePageDeps,
16
+ commandLineListTokens.spaTransition,
17
+ ],
18
+ afterSpa: [commandLineListTokens.afterSpaTransition],
19
+ };
20
+ const lines = {
21
+ server: command,
22
+ client: command,
23
+ };
24
+
25
+ export { lines };