@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.
- 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,10 @@
|
|
|
1
|
+
import { createEvent, createReducer } from '@tramvai/state';
|
|
2
|
+
|
|
3
|
+
const actionServerStateEvent = createEvent('action state execution in server');
|
|
4
|
+
const initalState = { serverState: {} };
|
|
5
|
+
const actionTramvaiReducer = createReducer('actionTramvai', initalState).on(actionServerStateEvent, (state, payload) => ({
|
|
6
|
+
...state,
|
|
7
|
+
serverState: payload,
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
export { actionServerStateEvent, actionTramvaiReducer };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createEvent, createReducer } from '@tramvai/state';
|
|
2
|
+
|
|
3
|
+
const actionServerStateEvent = createEvent('action state execution in server');
|
|
4
|
+
const initalState = { serverState: {} };
|
|
5
|
+
const actionTramvaiReducer = createReducer('actionTramvai', initalState).on(actionServerStateEvent, (state, payload) => ({
|
|
6
|
+
...state,
|
|
7
|
+
serverState: payload,
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
export { actionServerStateEvent, actionTramvaiReducer };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var state = require('@tramvai/state');
|
|
6
|
+
|
|
7
|
+
const actionServerStateEvent = state.createEvent('action state execution in server');
|
|
8
|
+
const initalState = { serverState: {} };
|
|
9
|
+
const actionTramvaiReducer = state.createReducer('actionTramvai', initalState).on(actionServerStateEvent, (state, payload) => ({
|
|
10
|
+
...state,
|
|
11
|
+
serverState: payload,
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
exports.actionServerStateEvent = actionServerStateEvent;
|
|
15
|
+
exports.actionTramvaiReducer = actionTramvaiReducer;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const alwaysCondition = {
|
|
6
|
+
key: 'alwaysCondition',
|
|
7
|
+
fn: (checker) => {
|
|
8
|
+
if (checker.conditions.always) {
|
|
9
|
+
checker.allow();
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.alwaysCondition = alwaysCondition;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var helpers = require('./helpers.js');
|
|
6
|
+
|
|
7
|
+
const onlyBrowser = {
|
|
8
|
+
key: 'onlyBrowser',
|
|
9
|
+
fn: (checker) => {
|
|
10
|
+
if (checker.conditions.onlyBrowser && helpers.isServer) {
|
|
11
|
+
checker.forbid();
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.onlyBrowser = onlyBrowser;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var helpers = require('./helpers.js');
|
|
6
|
+
|
|
7
|
+
const onlyServer = {
|
|
8
|
+
key: 'onlyServer',
|
|
9
|
+
fn: (checker) => {
|
|
10
|
+
if (checker.conditions.onlyServer && helpers.isBrowser) {
|
|
11
|
+
checker.forbid();
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.onlyServer = onlyServer;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isServer } from './helpers.browser.js';
|
|
2
|
+
import { actionType } from '../constants.browser.js';
|
|
3
|
+
|
|
4
|
+
const pageBrowser = {
|
|
5
|
+
key: 'pageBrowser',
|
|
6
|
+
fn: (checker) => {
|
|
7
|
+
if (checker.conditions.pageBrowser && checker.type === actionType.global && isServer) {
|
|
8
|
+
checker.forbid();
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { pageBrowser };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isServer } from './helpers.es.js';
|
|
2
|
+
import { actionType } from '../constants.es.js';
|
|
3
|
+
|
|
4
|
+
const pageBrowser = {
|
|
5
|
+
key: 'pageBrowser',
|
|
6
|
+
fn: (checker) => {
|
|
7
|
+
if (checker.conditions.pageBrowser && checker.type === actionType.global && isServer) {
|
|
8
|
+
checker.forbid();
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { pageBrowser };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var helpers = require('./helpers.js');
|
|
6
|
+
var constants = require('../constants.js');
|
|
7
|
+
|
|
8
|
+
const pageBrowser = {
|
|
9
|
+
key: 'pageBrowser',
|
|
10
|
+
fn: (checker) => {
|
|
11
|
+
if (checker.conditions.pageBrowser && checker.type === constants.actionType.global && helpers.isServer) {
|
|
12
|
+
checker.forbid();
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
exports.pageBrowser = pageBrowser;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isBrowser } from './helpers.browser.js';
|
|
2
|
+
import { actionType } from '../constants.browser.js';
|
|
3
|
+
|
|
4
|
+
const pageServer = {
|
|
5
|
+
key: 'pageServer',
|
|
6
|
+
fn: (checker) => {
|
|
7
|
+
if (checker.conditions.pageServer && checker.type === actionType.global && isBrowser) {
|
|
8
|
+
checker.forbid();
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { pageServer };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isBrowser } from './helpers.es.js';
|
|
2
|
+
import { actionType } from '../constants.es.js';
|
|
3
|
+
|
|
4
|
+
const pageServer = {
|
|
5
|
+
key: 'pageServer',
|
|
6
|
+
fn: (checker) => {
|
|
7
|
+
if (checker.conditions.pageServer && checker.type === actionType.global && isBrowser) {
|
|
8
|
+
checker.forbid();
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { pageServer };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var helpers = require('./helpers.js');
|
|
6
|
+
var constants = require('../constants.js');
|
|
7
|
+
|
|
8
|
+
const pageServer = {
|
|
9
|
+
key: 'pageServer',
|
|
10
|
+
fn: (checker) => {
|
|
11
|
+
if (checker.conditions.pageServer && checker.type === constants.actionType.global && helpers.isBrowser) {
|
|
12
|
+
checker.forbid();
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
exports.pageServer = pageServer;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated only for compatibility with legacy createAction
|
|
7
|
+
*/
|
|
8
|
+
const actionType = {
|
|
9
|
+
global: 'global',
|
|
10
|
+
local: 'local',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
exports.actionType = actionType;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import eachObj from '@tinkoff/utils/object/each';
|
|
2
|
+
import isObject from '@tinkoff/utils/is/object';
|
|
3
|
+
import isArray from '@tinkoff/utils/is/array';
|
|
4
|
+
import { createBundle } from '@tramvai/core';
|
|
5
|
+
import { resolveLazyComponent, __lazyErrorHandler } from '@tramvai/react';
|
|
6
|
+
import { fileSystemPagesEnabled, getAllFileSystemPages, getAllFileSystemLayouts, getAllFileSystemErrorBoundaries, isFileSystemPageComponent, fileSystemPageToLayoutKey, fileSystemPageToErrorBoundaryKey } from '@tramvai/experiments';
|
|
7
|
+
|
|
8
|
+
const FS_PAGES_DEFAULT_BUNDLE = '__default';
|
|
9
|
+
class BundleManager {
|
|
10
|
+
constructor({ bundleList, componentRegistry, actionRegistry, dispatcher, logger }) {
|
|
11
|
+
this.bundles = bundleList;
|
|
12
|
+
this.componentRegistry = componentRegistry;
|
|
13
|
+
this.actionRegistry = actionRegistry;
|
|
14
|
+
this.dispatcher = dispatcher;
|
|
15
|
+
if (fileSystemPagesEnabled()) {
|
|
16
|
+
const log = logger('file-system-pages:bundle-manager');
|
|
17
|
+
const components = getAllFileSystemPages();
|
|
18
|
+
const layouts = getAllFileSystemLayouts();
|
|
19
|
+
const errorBoundaries = getAllFileSystemErrorBoundaries();
|
|
20
|
+
const getComponentsFor = (mapping, getKey) => Object.keys(mapping).reduce((result, key) => {
|
|
21
|
+
// eslint-disable-next-line no-param-reassign
|
|
22
|
+
result[getKey(key)] = mapping[key];
|
|
23
|
+
return result;
|
|
24
|
+
}, {});
|
|
25
|
+
const componentsDefaultBundle = createBundle({
|
|
26
|
+
name: FS_PAGES_DEFAULT_BUNDLE,
|
|
27
|
+
components: {
|
|
28
|
+
...getComponentsFor(layouts, fileSystemPageToLayoutKey),
|
|
29
|
+
...getComponentsFor(errorBoundaries, fileSystemPageToErrorBoundaryKey),
|
|
30
|
+
...components,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
this.bundles[FS_PAGES_DEFAULT_BUNDLE] = () => Promise.resolve({
|
|
34
|
+
default: componentsDefaultBundle,
|
|
35
|
+
});
|
|
36
|
+
for (const key in componentsDefaultBundle.components) {
|
|
37
|
+
this.componentRegistry.add(key, componentsDefaultBundle.components[key], FS_PAGES_DEFAULT_BUNDLE);
|
|
38
|
+
}
|
|
39
|
+
log.debug({
|
|
40
|
+
event: 'create default bundle with file-system pages',
|
|
41
|
+
components: Object.keys(components),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
get(name, pageComponent) {
|
|
46
|
+
// use fake bundle with file-system pages
|
|
47
|
+
if (isFileSystemPageComponent(pageComponent)) {
|
|
48
|
+
// eslint-disable-next-line no-param-reassign
|
|
49
|
+
name = FS_PAGES_DEFAULT_BUNDLE;
|
|
50
|
+
}
|
|
51
|
+
return this.loadBundle(name, pageComponent).then((bundle) => this.resolve(bundle.default, pageComponent));
|
|
52
|
+
}
|
|
53
|
+
has(name, pageComponent) {
|
|
54
|
+
// use fake bundle with file-system pages
|
|
55
|
+
if (isFileSystemPageComponent(pageComponent)) {
|
|
56
|
+
// eslint-disable-next-line no-param-reassign
|
|
57
|
+
name = FS_PAGES_DEFAULT_BUNDLE;
|
|
58
|
+
}
|
|
59
|
+
return !!this.bundles[name];
|
|
60
|
+
}
|
|
61
|
+
async resolve(bundle, pageComponent) {
|
|
62
|
+
// preload `lazy` components then register actions and reducers
|
|
63
|
+
if (pageComponent && bundle.components[pageComponent]) {
|
|
64
|
+
const componentOrLoader = bundle.components[pageComponent];
|
|
65
|
+
const component = await resolveLazyComponent(componentOrLoader);
|
|
66
|
+
if (!component) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// allow page components to register any other components
|
|
70
|
+
if ('components' in component && isObject(component.components)) {
|
|
71
|
+
eachObj((cmp, name) => {
|
|
72
|
+
this.componentRegistry.add(name, cmp, pageComponent);
|
|
73
|
+
}, component.components);
|
|
74
|
+
}
|
|
75
|
+
if ('actions' in component && isArray(component.actions)) {
|
|
76
|
+
this.actionRegistry.add(pageComponent, component.actions);
|
|
77
|
+
}
|
|
78
|
+
if ('reducers' in component && isArray(component.reducers)) {
|
|
79
|
+
component.reducers.forEach((reducer) => {
|
|
80
|
+
this.dispatcher.registerStore(reducer);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
eachObj((component, name) => {
|
|
85
|
+
this.componentRegistry.add(name, component, bundle.name);
|
|
86
|
+
}, bundle.components);
|
|
87
|
+
if (bundle.actions) {
|
|
88
|
+
this.actionRegistry.add(bundle.name, bundle.actions);
|
|
89
|
+
}
|
|
90
|
+
if (bundle.reducers) {
|
|
91
|
+
bundle.reducers.forEach((reducer) => {
|
|
92
|
+
this.dispatcher.registerStore(reducer);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return bundle;
|
|
96
|
+
}
|
|
97
|
+
loadBundle(name, pageComponent) {
|
|
98
|
+
if (!this.has(name, pageComponent)) {
|
|
99
|
+
return Promise.reject(new Error(`Bundle "${name}" not found`));
|
|
100
|
+
}
|
|
101
|
+
return this.bundles[name]().catch((e) => __lazyErrorHandler(e, this.bundles[name]));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { BundleManager };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import eachObj from '@tinkoff/utils/object/each';
|
|
2
|
+
import isObject from '@tinkoff/utils/is/object';
|
|
3
|
+
import isArray from '@tinkoff/utils/is/array';
|
|
4
|
+
import { createBundle } from '@tramvai/core';
|
|
5
|
+
import { resolveLazyComponent, __lazyErrorHandler } from '@tramvai/react';
|
|
6
|
+
import { fileSystemPagesEnabled, getAllFileSystemPages, getAllFileSystemLayouts, getAllFileSystemErrorBoundaries, isFileSystemPageComponent, fileSystemPageToLayoutKey, fileSystemPageToErrorBoundaryKey } from '@tramvai/experiments';
|
|
7
|
+
|
|
8
|
+
const FS_PAGES_DEFAULT_BUNDLE = '__default';
|
|
9
|
+
class BundleManager {
|
|
10
|
+
constructor({ bundleList, componentRegistry, actionRegistry, dispatcher, logger }) {
|
|
11
|
+
this.bundles = bundleList;
|
|
12
|
+
this.componentRegistry = componentRegistry;
|
|
13
|
+
this.actionRegistry = actionRegistry;
|
|
14
|
+
this.dispatcher = dispatcher;
|
|
15
|
+
if (fileSystemPagesEnabled()) {
|
|
16
|
+
const log = logger('file-system-pages:bundle-manager');
|
|
17
|
+
const components = getAllFileSystemPages();
|
|
18
|
+
const layouts = getAllFileSystemLayouts();
|
|
19
|
+
const errorBoundaries = getAllFileSystemErrorBoundaries();
|
|
20
|
+
const getComponentsFor = (mapping, getKey) => Object.keys(mapping).reduce((result, key) => {
|
|
21
|
+
// eslint-disable-next-line no-param-reassign
|
|
22
|
+
result[getKey(key)] = mapping[key];
|
|
23
|
+
return result;
|
|
24
|
+
}, {});
|
|
25
|
+
const componentsDefaultBundle = createBundle({
|
|
26
|
+
name: FS_PAGES_DEFAULT_BUNDLE,
|
|
27
|
+
components: {
|
|
28
|
+
...getComponentsFor(layouts, fileSystemPageToLayoutKey),
|
|
29
|
+
...getComponentsFor(errorBoundaries, fileSystemPageToErrorBoundaryKey),
|
|
30
|
+
...components,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
this.bundles[FS_PAGES_DEFAULT_BUNDLE] = () => Promise.resolve({
|
|
34
|
+
default: componentsDefaultBundle,
|
|
35
|
+
});
|
|
36
|
+
for (const key in componentsDefaultBundle.components) {
|
|
37
|
+
this.componentRegistry.add(key, componentsDefaultBundle.components[key], FS_PAGES_DEFAULT_BUNDLE);
|
|
38
|
+
}
|
|
39
|
+
log.debug({
|
|
40
|
+
event: 'create default bundle with file-system pages',
|
|
41
|
+
components: Object.keys(components),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
get(name, pageComponent) {
|
|
46
|
+
// use fake bundle with file-system pages
|
|
47
|
+
if (isFileSystemPageComponent(pageComponent)) {
|
|
48
|
+
// eslint-disable-next-line no-param-reassign
|
|
49
|
+
name = FS_PAGES_DEFAULT_BUNDLE;
|
|
50
|
+
}
|
|
51
|
+
return this.loadBundle(name, pageComponent).then((bundle) => this.resolve(bundle.default, pageComponent));
|
|
52
|
+
}
|
|
53
|
+
has(name, pageComponent) {
|
|
54
|
+
// use fake bundle with file-system pages
|
|
55
|
+
if (isFileSystemPageComponent(pageComponent)) {
|
|
56
|
+
// eslint-disable-next-line no-param-reassign
|
|
57
|
+
name = FS_PAGES_DEFAULT_BUNDLE;
|
|
58
|
+
}
|
|
59
|
+
return !!this.bundles[name];
|
|
60
|
+
}
|
|
61
|
+
async resolve(bundle, pageComponent) {
|
|
62
|
+
// preload `lazy` components then register actions and reducers
|
|
63
|
+
if (pageComponent && bundle.components[pageComponent]) {
|
|
64
|
+
const componentOrLoader = bundle.components[pageComponent];
|
|
65
|
+
const component = await resolveLazyComponent(componentOrLoader);
|
|
66
|
+
if (!component) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// allow page components to register any other components
|
|
70
|
+
if ('components' in component && isObject(component.components)) {
|
|
71
|
+
eachObj((cmp, name) => {
|
|
72
|
+
this.componentRegistry.add(name, cmp, pageComponent);
|
|
73
|
+
}, component.components);
|
|
74
|
+
}
|
|
75
|
+
if ('actions' in component && isArray(component.actions)) {
|
|
76
|
+
this.actionRegistry.add(pageComponent, component.actions);
|
|
77
|
+
}
|
|
78
|
+
if ('reducers' in component && isArray(component.reducers)) {
|
|
79
|
+
component.reducers.forEach((reducer) => {
|
|
80
|
+
this.dispatcher.registerStore(reducer);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
eachObj((component, name) => {
|
|
85
|
+
this.componentRegistry.add(name, component, bundle.name);
|
|
86
|
+
}, bundle.components);
|
|
87
|
+
if (bundle.actions) {
|
|
88
|
+
this.actionRegistry.add(bundle.name, bundle.actions);
|
|
89
|
+
}
|
|
90
|
+
if (bundle.reducers) {
|
|
91
|
+
bundle.reducers.forEach((reducer) => {
|
|
92
|
+
this.dispatcher.registerStore(reducer);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return bundle;
|
|
96
|
+
}
|
|
97
|
+
loadBundle(name, pageComponent) {
|
|
98
|
+
if (!this.has(name, pageComponent)) {
|
|
99
|
+
return Promise.reject(new Error(`Bundle "${name}" not found`));
|
|
100
|
+
}
|
|
101
|
+
return this.bundles[name]().catch((e) => __lazyErrorHandler(e, this.bundles[name]));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { BundleManager };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var eachObj = require('@tinkoff/utils/object/each');
|
|
6
|
+
var isObject = require('@tinkoff/utils/is/object');
|
|
7
|
+
var isArray = require('@tinkoff/utils/is/array');
|
|
8
|
+
var core = require('@tramvai/core');
|
|
9
|
+
var react = require('@tramvai/react');
|
|
10
|
+
var experiments = require('@tramvai/experiments');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var eachObj__default = /*#__PURE__*/_interopDefaultLegacy(eachObj);
|
|
15
|
+
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
|
|
16
|
+
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
|
|
17
|
+
|
|
18
|
+
const FS_PAGES_DEFAULT_BUNDLE = '__default';
|
|
19
|
+
class BundleManager {
|
|
20
|
+
constructor({ bundleList, componentRegistry, actionRegistry, dispatcher, logger }) {
|
|
21
|
+
this.bundles = bundleList;
|
|
22
|
+
this.componentRegistry = componentRegistry;
|
|
23
|
+
this.actionRegistry = actionRegistry;
|
|
24
|
+
this.dispatcher = dispatcher;
|
|
25
|
+
if (experiments.fileSystemPagesEnabled()) {
|
|
26
|
+
const log = logger('file-system-pages:bundle-manager');
|
|
27
|
+
const components = experiments.getAllFileSystemPages();
|
|
28
|
+
const layouts = experiments.getAllFileSystemLayouts();
|
|
29
|
+
const errorBoundaries = experiments.getAllFileSystemErrorBoundaries();
|
|
30
|
+
const getComponentsFor = (mapping, getKey) => Object.keys(mapping).reduce((result, key) => {
|
|
31
|
+
// eslint-disable-next-line no-param-reassign
|
|
32
|
+
result[getKey(key)] = mapping[key];
|
|
33
|
+
return result;
|
|
34
|
+
}, {});
|
|
35
|
+
const componentsDefaultBundle = core.createBundle({
|
|
36
|
+
name: FS_PAGES_DEFAULT_BUNDLE,
|
|
37
|
+
components: {
|
|
38
|
+
...getComponentsFor(layouts, experiments.fileSystemPageToLayoutKey),
|
|
39
|
+
...getComponentsFor(errorBoundaries, experiments.fileSystemPageToErrorBoundaryKey),
|
|
40
|
+
...components,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
this.bundles[FS_PAGES_DEFAULT_BUNDLE] = () => Promise.resolve({
|
|
44
|
+
default: componentsDefaultBundle,
|
|
45
|
+
});
|
|
46
|
+
for (const key in componentsDefaultBundle.components) {
|
|
47
|
+
this.componentRegistry.add(key, componentsDefaultBundle.components[key], FS_PAGES_DEFAULT_BUNDLE);
|
|
48
|
+
}
|
|
49
|
+
log.debug({
|
|
50
|
+
event: 'create default bundle with file-system pages',
|
|
51
|
+
components: Object.keys(components),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
get(name, pageComponent) {
|
|
56
|
+
// use fake bundle with file-system pages
|
|
57
|
+
if (experiments.isFileSystemPageComponent(pageComponent)) {
|
|
58
|
+
// eslint-disable-next-line no-param-reassign
|
|
59
|
+
name = FS_PAGES_DEFAULT_BUNDLE;
|
|
60
|
+
}
|
|
61
|
+
return this.loadBundle(name, pageComponent).then((bundle) => this.resolve(bundle.default, pageComponent));
|
|
62
|
+
}
|
|
63
|
+
has(name, pageComponent) {
|
|
64
|
+
// use fake bundle with file-system pages
|
|
65
|
+
if (experiments.isFileSystemPageComponent(pageComponent)) {
|
|
66
|
+
// eslint-disable-next-line no-param-reassign
|
|
67
|
+
name = FS_PAGES_DEFAULT_BUNDLE;
|
|
68
|
+
}
|
|
69
|
+
return !!this.bundles[name];
|
|
70
|
+
}
|
|
71
|
+
async resolve(bundle, pageComponent) {
|
|
72
|
+
// preload `lazy` components then register actions and reducers
|
|
73
|
+
if (pageComponent && bundle.components[pageComponent]) {
|
|
74
|
+
const componentOrLoader = bundle.components[pageComponent];
|
|
75
|
+
const component = await react.resolveLazyComponent(componentOrLoader);
|
|
76
|
+
if (!component) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// allow page components to register any other components
|
|
80
|
+
if ('components' in component && isObject__default["default"](component.components)) {
|
|
81
|
+
eachObj__default["default"]((cmp, name) => {
|
|
82
|
+
this.componentRegistry.add(name, cmp, pageComponent);
|
|
83
|
+
}, component.components);
|
|
84
|
+
}
|
|
85
|
+
if ('actions' in component && isArray__default["default"](component.actions)) {
|
|
86
|
+
this.actionRegistry.add(pageComponent, component.actions);
|
|
87
|
+
}
|
|
88
|
+
if ('reducers' in component && isArray__default["default"](component.reducers)) {
|
|
89
|
+
component.reducers.forEach((reducer) => {
|
|
90
|
+
this.dispatcher.registerStore(reducer);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
eachObj__default["default"]((component, name) => {
|
|
95
|
+
this.componentRegistry.add(name, component, bundle.name);
|
|
96
|
+
}, bundle.components);
|
|
97
|
+
if (bundle.actions) {
|
|
98
|
+
this.actionRegistry.add(bundle.name, bundle.actions);
|
|
99
|
+
}
|
|
100
|
+
if (bundle.reducers) {
|
|
101
|
+
bundle.reducers.forEach((reducer) => {
|
|
102
|
+
this.dispatcher.registerStore(reducer);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return bundle;
|
|
106
|
+
}
|
|
107
|
+
loadBundle(name, pageComponent) {
|
|
108
|
+
if (!this.has(name, pageComponent)) {
|
|
109
|
+
return Promise.reject(new Error(`Bundle "${name}" not found`));
|
|
110
|
+
}
|
|
111
|
+
return this.bundles[name]().catch((e) => react.__lazyErrorHandler(e, this.bundles[name]));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
exports.BundleManager = BundleManager;
|