@tramvai/core 2.24.3 → 2.26.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/README.md CHANGED
@@ -47,42 +47,6 @@ createApp({
47
47
 
48
48
  After calling createApp, [СommandLineRunner](concepts/command-line-runner.md) is started which performs the chain of actions necessary to initialize the application.
49
49
 
50
- ### Module
51
-
52
- `Module` - Decorator for configuring and creating a module.
53
-
54
- [Read more about modules](concepts/module.md)
55
-
56
- #### @Module({ providers, deps, imports })(class)
57
-
58
- - `providers` - [Providers](concepts/provider.md), which will be added to the root DI container and become available in other modules
59
- - `deps` - List of dependencies from the DI container, necessary to initialize the module
60
- - `imports` - A list of modules from which providers will be obtained and added to the DI. Allows you to create modules that combine many other modules
61
-
62
- #### Usage
63
-
64
- ```tsx
65
- import { Module, provide } from '@tramvai/core';
66
-
67
- @Module({
68
- providers: [
69
- provide({
70
- provide: 'token',
71
- useValue: 'value-in-token',
72
- }),
73
- ],
74
- deps: {
75
- logger: 'logger',
76
- },
77
- imports: [ModuleLogger],
78
- })
79
- class ModulePubSub {
80
- constructor({ logger }) {
81
- logger.info('Module create');
82
- }
83
- }
84
- ```
85
-
86
50
  ### declareAction
87
51
 
88
52
  `declareAction` - Method for creating asynchronous actions. It is used both for building chains of sagas and for performing global actions when building a response to a client
@@ -1,6 +1,5 @@
1
- import type { Container, Provider } from '@tinkoff/dippy';
1
+ import type { Container, Provider, ModuleType, ExtendedModule } from '@tinkoff/dippy';
2
2
  import type { Bundle } from '@tramvai/tokens-core';
3
- import type { ModuleType, ExtendedModule } from './modules/module.h';
4
3
  interface AppOptions {
5
4
  name: string;
6
5
  modules?: (ModuleType | ExtendedModule)[];
@@ -15,10 +14,9 @@ export declare class App {
15
14
  private modulesToResolve;
16
15
  constructor({ name, modules, bundles, actions, providers }: AppOptions);
17
16
  initialization(env: 'server' | 'client', type?: "init"): Promise<Container>;
17
+ private walkOfProviders;
18
18
  private resolveModules;
19
19
  private resolveModuleDeps;
20
- private walkOfProviders;
21
- private walkOfModules;
22
20
  }
23
21
  export declare function createApp(options: AppOptions): Promise<App>;
24
22
  export {};
package/lib/index.d.ts CHANGED
@@ -2,10 +2,5 @@ export { createApp, App } from './createApp';
2
2
  export { createBundle } from './bundles/createBundle';
3
3
  export { createAction } from './actions/createActions';
4
4
  export * from './actions/declareAction';
5
- export { Module, deprecatedModule as module, MODULE_PARAMETERS } from './modules/module';
6
- export { getModuleParameters } from './modules/getModuleParameters';
7
- export { walkOfModules } from './modules/walkOfModules';
8
- export { isExtendedModule } from './modules/isExtendedModule';
9
- export { ModuleType, ExtendedModule } from './modules/module.h';
10
5
  export * from '@tramvai/tokens-core';
11
- export { DI_TOKEN, IS_DI_CHILD_CONTAINER_TOKEN, Scope, Provider, createToken, provide, optional, ExtractTokenType, ExtractDependencyType, } from '@tinkoff/dippy';
6
+ export { DI_TOKEN, IS_DI_CHILD_CONTAINER_TOKEN, Scope, Provider, createToken, provide, optional, ExtractTokenType, ExtractDependencyType, Module, MODULE_PARAMETERS, getModuleParameters, walkOfModules, isExtendedModule, ModuleType, ExtendedModule, } from '@tinkoff/dippy';
package/lib/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
- import { createContainer, Scope } from '@tinkoff/dippy';
2
- export { DI_TOKEN, IS_DI_CHILD_CONTAINER_TOKEN, Scope, createToken, optional, provide } from '@tinkoff/dippy';
1
+ import { createContainer, walkOfModules, getModuleParameters, isExtendedModule, MODULE_PARAMETERS, Scope } from '@tinkoff/dippy';
2
+ export { DI_TOKEN, IS_DI_CHILD_CONTAINER_TOKEN, MODULE_PARAMETERS, Module, Scope, createToken, getModuleParameters, isExtendedModule, optional, provide, walkOfModules } from '@tinkoff/dippy';
3
3
  import { COMMAND_LINE_RUNNER_TOKEN, APP_INFO_TOKEN, BUNDLE_LIST_TOKEN, ACTIONS_LIST_TOKEN, MODULES_LIST_TOKEN, ACTION_PARAMETERS } from '@tramvai/tokens-core';
4
4
  export * from '@tramvai/tokens-core';
5
5
  import { LOGGER_TOKEN } from '@tramvai/tokens-common';
@@ -7,91 +7,6 @@ import concat from '@tinkoff/utils/array/concat';
7
7
  import mergeWith from '@tinkoff/utils/object/mergeWith';
8
8
  import omit from '@tinkoff/utils/object/omit';
9
9
 
10
- const MODULE_PARAMETERS = '_module_parameters_';
11
- function Module({ providers, imports, deps = {}, }) {
12
- return (target) => {
13
- return Object.assign(target, {
14
- [MODULE_PARAMETERS]: {
15
- providers,
16
- deps,
17
- imports: imports,
18
- name: target.name,
19
- id: `${target.name}-${Math.random()}`, // Math.random так как возможно использование несколько версий библиотеки одновременно
20
- },
21
- });
22
- };
23
- }
24
- // TODO: удалить этот импорт вообще 02-10-2021
25
- /**
26
- * @deprecated Используйте Module вместо module - `import { Module } from '@tramvai/core';` Иначе не будет работать hot reload
27
- */ const deprecatedModule = Module;
28
-
29
- const isValidModule = (module) => {
30
- // Если у нас undefined или null
31
- if (Boolean(module) === false)
32
- return false;
33
- // Если это модуль
34
- if (MODULE_PARAMETERS in module)
35
- return true;
36
- // Если это модуль который расширяет другой модуль
37
- if ('mainModule' in module)
38
- return isValidModule(module.mainModule);
39
- return false;
40
- };
41
-
42
- const isExtendedModule = (module) => {
43
- return !!module.mainModule;
44
- };
45
-
46
- const getModuleParameters = (module) => {
47
- let moduleParameters;
48
- if (isExtendedModule(module)) {
49
- const main = module.mainModule[MODULE_PARAMETERS];
50
- moduleParameters = { ...main };
51
- moduleParameters.providers = moduleParameters.providers.concat(module.providers || []);
52
- const importsParam = moduleParameters.imports || []; // imports не обязательный параметр
53
- moduleParameters.imports = importsParam.concat(module.imports || []);
54
- }
55
- else {
56
- moduleParameters = module[MODULE_PARAMETERS];
57
- }
58
- return moduleParameters;
59
- };
60
-
61
- const INVALID_MODULE_ERROR = 'An invalid module was passed in the list of modules';
62
- const walkOfModules = (modules) => {
63
- const result = [];
64
- const modulesIdInitialized = new Set();
65
- const modulesNameInitialized = new Set();
66
- const innerWalkOfModules = (module) => {
67
- if (!isValidModule(module)) {
68
- throw new Error(INVALID_MODULE_ERROR);
69
- }
70
- const moduleParameters = getModuleParameters(module);
71
- if (!modulesIdInitialized.has(moduleParameters.id)) {
72
- if (process.env.NODE_ENV !== 'production') {
73
- if (modulesNameInitialized.has(moduleParameters.name)) {
74
- // eslint-disable-next-line no-console
75
- console.error(`Module ${moduleParameters.id} has already been initialized. Most likely there are duplicate dependencies in the project:`, module, moduleParameters);
76
- }
77
- }
78
- modulesIdInitialized.add(moduleParameters.id);
79
- modulesNameInitialized.add(moduleParameters.name);
80
- // Если модуль импортирует другие модули, то инициализируем их провайдеры
81
- if (moduleParameters.imports) {
82
- moduleParameters.imports.forEach((item) => {
83
- innerWalkOfModules(item);
84
- });
85
- }
86
- result.push(module);
87
- }
88
- };
89
- modules.forEach((mod) => {
90
- innerWalkOfModules(mod);
91
- });
92
- return result;
93
- };
94
-
95
10
  function appProviders(name, bundles, actions, modules) {
96
11
  return [
97
12
  {
@@ -127,11 +42,12 @@ class App {
127
42
  constructor({ name, modules = [], bundles = {}, actions = [], providers }) {
128
43
  this.di = createContainer();
129
44
  this.modulesToResolve = new Set();
130
- // Закидываем в di пришедшшие данные в app
131
45
  this.walkOfProviders(appProviders(name, bundles, actions, modules));
132
- // Инициализуем провайдеры переданные в модули
133
- this.walkOfModules(modules);
134
- // Инициализируем провайдеры добавленные в приложении
46
+ walkOfModules(modules).forEach((mod) => {
47
+ const moduleParameters = getModuleParameters(mod);
48
+ this.modulesToResolve.add(isExtendedModule(mod) ? mod.mainModule : mod);
49
+ this.walkOfProviders(moduleParameters.providers);
50
+ });
135
51
  if (providers) {
136
52
  this.walkOfProviders(providers);
137
53
  }
@@ -159,6 +75,11 @@ class App {
159
75
  });
160
76
  return di;
161
77
  }
78
+ walkOfProviders(providers) {
79
+ providers.forEach((provide) => {
80
+ this.di.register(provide);
81
+ });
82
+ }
162
83
  resolveModules() {
163
84
  this.modulesToResolve.forEach((ModuleToResolve) => {
164
85
  // eslint-disable-next-line no-new
@@ -171,18 +92,6 @@ class App {
171
92
  return this.di.getOfDeps(deps);
172
93
  }
173
94
  }
174
- walkOfProviders(providers) {
175
- providers.forEach((provide) => {
176
- this.di.register(provide);
177
- });
178
- }
179
- walkOfModules(modules) {
180
- walkOfModules(modules).forEach((mod) => {
181
- const moduleParameters = getModuleParameters(mod);
182
- this.modulesToResolve.add(isExtendedModule(mod) ? mod.mainModule : mod);
183
- this.walkOfProviders(moduleParameters.providers);
184
- });
185
- }
186
95
  }
187
96
  function createApp(options) {
188
97
  let app;
@@ -249,4 +158,4 @@ function isTramvaiAction(action) {
249
158
  return 'tramvaiActionVersion' in action && action.tramvaiActionVersion === 2;
250
159
  }
251
160
 
252
- export { App, MODULE_PARAMETERS, Module, createAction, createApp, createBundle, declareAction, getModuleParameters, isExtendedModule, isTramvaiAction, deprecatedModule as module, walkOfModules };
161
+ export { App, createAction, createApp, createBundle, declareAction, isTramvaiAction };
package/lib/index.js CHANGED
@@ -15,91 +15,6 @@ var concat__default = /*#__PURE__*/_interopDefaultLegacy(concat);
15
15
  var mergeWith__default = /*#__PURE__*/_interopDefaultLegacy(mergeWith);
16
16
  var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
17
17
 
18
- const MODULE_PARAMETERS = '_module_parameters_';
19
- function Module({ providers, imports, deps = {}, }) {
20
- return (target) => {
21
- return Object.assign(target, {
22
- [MODULE_PARAMETERS]: {
23
- providers,
24
- deps,
25
- imports: imports,
26
- name: target.name,
27
- id: `${target.name}-${Math.random()}`, // Math.random так как возможно использование несколько версий библиотеки одновременно
28
- },
29
- });
30
- };
31
- }
32
- // TODO: удалить этот импорт вообще 02-10-2021
33
- /**
34
- * @deprecated Используйте Module вместо module - `import { Module } from '@tramvai/core';` Иначе не будет работать hot reload
35
- */ const deprecatedModule = Module;
36
-
37
- const isValidModule = (module) => {
38
- // Если у нас undefined или null
39
- if (Boolean(module) === false)
40
- return false;
41
- // Если это модуль
42
- if (MODULE_PARAMETERS in module)
43
- return true;
44
- // Если это модуль который расширяет другой модуль
45
- if ('mainModule' in module)
46
- return isValidModule(module.mainModule);
47
- return false;
48
- };
49
-
50
- const isExtendedModule = (module) => {
51
- return !!module.mainModule;
52
- };
53
-
54
- const getModuleParameters = (module) => {
55
- let moduleParameters;
56
- if (isExtendedModule(module)) {
57
- const main = module.mainModule[MODULE_PARAMETERS];
58
- moduleParameters = { ...main };
59
- moduleParameters.providers = moduleParameters.providers.concat(module.providers || []);
60
- const importsParam = moduleParameters.imports || []; // imports не обязательный параметр
61
- moduleParameters.imports = importsParam.concat(module.imports || []);
62
- }
63
- else {
64
- moduleParameters = module[MODULE_PARAMETERS];
65
- }
66
- return moduleParameters;
67
- };
68
-
69
- const INVALID_MODULE_ERROR = 'An invalid module was passed in the list of modules';
70
- const walkOfModules = (modules) => {
71
- const result = [];
72
- const modulesIdInitialized = new Set();
73
- const modulesNameInitialized = new Set();
74
- const innerWalkOfModules = (module) => {
75
- if (!isValidModule(module)) {
76
- throw new Error(INVALID_MODULE_ERROR);
77
- }
78
- const moduleParameters = getModuleParameters(module);
79
- if (!modulesIdInitialized.has(moduleParameters.id)) {
80
- if (process.env.NODE_ENV !== 'production') {
81
- if (modulesNameInitialized.has(moduleParameters.name)) {
82
- // eslint-disable-next-line no-console
83
- console.error(`Module ${moduleParameters.id} has already been initialized. Most likely there are duplicate dependencies in the project:`, module, moduleParameters);
84
- }
85
- }
86
- modulesIdInitialized.add(moduleParameters.id);
87
- modulesNameInitialized.add(moduleParameters.name);
88
- // Если модуль импортирует другие модули, то инициализируем их провайдеры
89
- if (moduleParameters.imports) {
90
- moduleParameters.imports.forEach((item) => {
91
- innerWalkOfModules(item);
92
- });
93
- }
94
- result.push(module);
95
- }
96
- };
97
- modules.forEach((mod) => {
98
- innerWalkOfModules(mod);
99
- });
100
- return result;
101
- };
102
-
103
18
  function appProviders(name, bundles, actions, modules) {
104
19
  return [
105
20
  {
@@ -135,11 +50,12 @@ class App {
135
50
  constructor({ name, modules = [], bundles = {}, actions = [], providers }) {
136
51
  this.di = dippy.createContainer();
137
52
  this.modulesToResolve = new Set();
138
- // Закидываем в di пришедшшие данные в app
139
53
  this.walkOfProviders(appProviders(name, bundles, actions, modules));
140
- // Инициализуем провайдеры переданные в модули
141
- this.walkOfModules(modules);
142
- // Инициализируем провайдеры добавленные в приложении
54
+ dippy.walkOfModules(modules).forEach((mod) => {
55
+ const moduleParameters = dippy.getModuleParameters(mod);
56
+ this.modulesToResolve.add(dippy.isExtendedModule(mod) ? mod.mainModule : mod);
57
+ this.walkOfProviders(moduleParameters.providers);
58
+ });
143
59
  if (providers) {
144
60
  this.walkOfProviders(providers);
145
61
  }
@@ -167,6 +83,11 @@ class App {
167
83
  });
168
84
  return di;
169
85
  }
86
+ walkOfProviders(providers) {
87
+ providers.forEach((provide) => {
88
+ this.di.register(provide);
89
+ });
90
+ }
170
91
  resolveModules() {
171
92
  this.modulesToResolve.forEach((ModuleToResolve) => {
172
93
  // eslint-disable-next-line no-new
@@ -174,23 +95,11 @@ class App {
174
95
  });
175
96
  }
176
97
  resolveModuleDeps(module) {
177
- const { deps } = module[MODULE_PARAMETERS];
98
+ const { deps } = module[dippy.MODULE_PARAMETERS];
178
99
  if (deps) {
179
100
  return this.di.getOfDeps(deps);
180
101
  }
181
102
  }
182
- walkOfProviders(providers) {
183
- providers.forEach((provide) => {
184
- this.di.register(provide);
185
- });
186
- }
187
- walkOfModules(modules) {
188
- walkOfModules(modules).forEach((mod) => {
189
- const moduleParameters = getModuleParameters(mod);
190
- this.modulesToResolve.add(isExtendedModule(mod) ? mod.mainModule : mod);
191
- this.walkOfProviders(moduleParameters.providers);
192
- });
193
- }
194
103
  }
195
104
  function createApp(options) {
196
105
  let app;
@@ -265,6 +174,14 @@ Object.defineProperty(exports, 'IS_DI_CHILD_CONTAINER_TOKEN', {
265
174
  enumerable: true,
266
175
  get: function () { return dippy.IS_DI_CHILD_CONTAINER_TOKEN; }
267
176
  });
177
+ Object.defineProperty(exports, 'MODULE_PARAMETERS', {
178
+ enumerable: true,
179
+ get: function () { return dippy.MODULE_PARAMETERS; }
180
+ });
181
+ Object.defineProperty(exports, 'Module', {
182
+ enumerable: true,
183
+ get: function () { return dippy.Module; }
184
+ });
268
185
  Object.defineProperty(exports, 'Scope', {
269
186
  enumerable: true,
270
187
  get: function () { return dippy.Scope; }
@@ -273,6 +190,14 @@ Object.defineProperty(exports, 'createToken', {
273
190
  enumerable: true,
274
191
  get: function () { return dippy.createToken; }
275
192
  });
193
+ Object.defineProperty(exports, 'getModuleParameters', {
194
+ enumerable: true,
195
+ get: function () { return dippy.getModuleParameters; }
196
+ });
197
+ Object.defineProperty(exports, 'isExtendedModule', {
198
+ enumerable: true,
199
+ get: function () { return dippy.isExtendedModule; }
200
+ });
276
201
  Object.defineProperty(exports, 'optional', {
277
202
  enumerable: true,
278
203
  get: function () { return dippy.optional; }
@@ -281,18 +206,16 @@ Object.defineProperty(exports, 'provide', {
281
206
  enumerable: true,
282
207
  get: function () { return dippy.provide; }
283
208
  });
209
+ Object.defineProperty(exports, 'walkOfModules', {
210
+ enumerable: true,
211
+ get: function () { return dippy.walkOfModules; }
212
+ });
284
213
  exports.App = App;
285
- exports.MODULE_PARAMETERS = MODULE_PARAMETERS;
286
- exports.Module = Module;
287
214
  exports.createAction = createAction;
288
215
  exports.createApp = createApp;
289
216
  exports.createBundle = createBundle;
290
217
  exports.declareAction = declareAction;
291
- exports.getModuleParameters = getModuleParameters;
292
- exports.isExtendedModule = isExtendedModule;
293
218
  exports.isTramvaiAction = isTramvaiAction;
294
- exports.module = deprecatedModule;
295
- exports.walkOfModules = walkOfModules;
296
219
  Object.keys(tokensCore).forEach(function (k) {
297
220
  if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
298
221
  enumerable: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/core",
3
- "version": "2.24.3",
3
+ "version": "2.26.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -20,11 +20,11 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@tinkoff/utils": "^2.1.2",
23
- "@tramvai/tokens-common": "2.24.3",
24
- "@tramvai/tokens-core": "2.24.3",
25
- "@tramvai/types-actions-state-context": "2.24.3",
26
- "@tinkoff/dippy": "0.8.3",
27
- "tslib": "^2.0.3"
23
+ "@tramvai/tokens-common": "2.26.0",
24
+ "@tramvai/tokens-core": "2.26.0",
25
+ "@tramvai/types-actions-state-context": "2.26.0",
26
+ "@tinkoff/dippy": "0.8.5",
27
+ "tslib": "^2.4.0"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "jscodeshift": "^0.7.0"
@@ -1,2 +0,0 @@
1
- import type { ModuleType, ModuleParameters, ExtendedModule } from './module.h';
2
- export declare const getModuleParameters: (module: ModuleType | ExtendedModule) => ModuleParameters;
@@ -1,2 +0,0 @@
1
- import type { ModuleType, ExtendedModule } from './module.h';
2
- export declare const isExtendedModule: (module: ModuleType | ExtendedModule) => module is ExtendedModule;
@@ -1,2 +0,0 @@
1
- import type { ModuleType, ExtendedModule } from './module.h';
2
- export declare const isValidModule: (module: ModuleType | ExtendedModule) => module is ModuleType<import("./module.h").ModuleClass> | ExtendedModule;
@@ -1,7 +0,0 @@
1
- import type { Provider } from '@tinkoff/dippy';
2
- import type { ModuleOptions, ModuleConstructor } from './module.h';
3
- export declare const MODULE_PARAMETERS = "_module_parameters_";
4
- export declare function Module<Providers extends Provider[]>({ providers, imports, deps, }: ModuleOptions<Providers>): ModuleConstructor;
5
- /**
6
- * @deprecated Используйте Module вместо module - `import { Module } from '@tramvai/core';` Иначе не будет работать hot reload
7
- */ export declare const deprecatedModule: typeof Module;
@@ -1,28 +0,0 @@
1
- import type { Provider, ProviderDeps } from '@tinkoff/dippy';
2
- import type { MODULE_PARAMETERS } from './module';
3
- export interface ModuleOptions<Providers extends Provider[]> {
4
- providers: Providers;
5
- deps?: ProviderDeps;
6
- imports?: ModuleType[];
7
- }
8
- export interface ModuleParameters {
9
- providers: Provider[];
10
- deps: ProviderDeps;
11
- imports?: ModuleType[];
12
- id: string;
13
- name: string;
14
- }
15
- interface ModuleSecretParameters {
16
- [MODULE_PARAMETERS]: ModuleParameters;
17
- }
18
- export declare type ExtendedModule = {
19
- mainModule: ModuleType;
20
- providers?: Provider[];
21
- imports?: ModuleType[];
22
- };
23
- export interface ModuleClass {
24
- new (...args: any[]): any;
25
- }
26
- export declare type ModuleType<Class extends ModuleClass = ModuleClass> = Class & Partial<ModuleSecretParameters>;
27
- export declare type ModuleConstructor = <Class extends ModuleClass = ModuleClass>(target: Class) => ModuleType<Class>;
28
- export {};
@@ -1,3 +0,0 @@
1
- import type { ExtendedModule, ModuleType } from './module.h';
2
- export declare const INVALID_MODULE_ERROR = "An invalid module was passed in the list of modules";
3
- export declare const walkOfModules: (modules: Array<ModuleType | ExtendedModule>) => (ModuleType<import("./module.h").ModuleClass> | ExtendedModule)[];