Skip to content

Commit 182a4ee

Browse files
authored
Add load-dependency early hook to allow control over what application modules get loaded at runtime. (#191)
1 parent 3ab578d commit 182a4ee

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

.core/dependencies/index.js

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
import op from 'object-path';
2+
import _ from 'underscore';
23
import Reactium, { isBrowserWindow } from 'reactium-core/sdk';
34
import manifestLoader from 'manifest';
45

56
class ReactiumDependencies {
67
constructor() {
78
this.loaded = false;
89
this.loadedModules = {};
9-
this.actions = {};
10-
this.actionTypes = {
11-
DOMAIN_UPDATE: 'DOMAIN_UPDATE',
12-
};
1310
this.services = {};
14-
this.reducers = {};
1511
this.plugins = {};
1612
this.plugableConfig = {};
1713

1814
// Just used to determine if is a custom type
19-
this.coreTypes = [
20-
'allActionTypes',
21-
'allActions',
22-
'allReducers',
23-
'allInitialStates',
24-
'allServices',
25-
'allMiddleware',
26-
'allEnhancers',
27-
'allPlugins',
28-
'allHooks',
29-
];
15+
this.coreTypes = ['allServices', 'allPlugins', 'allHooks'];
3016

3117
// Things to be mapped on deps now
3218
this.coreTypeMap = {
33-
allActionTypes: 'actionTypes',
34-
allActions: 'actions',
3519
allServices: 'services',
3620
allPlugins: 'plugins',
3721
};
@@ -56,29 +40,48 @@ class ReactiumDependencies {
5640
}
5741

5842
async loadAll(type) {
59-
return Promise.all(
60-
op.get(this.manifest, [type], []).map(dep => {
61-
const { name, domain, loader } = dep;
62-
if (op.has(this, ['loadedModules', name, domain])) {
63-
const loadedModule = op.get(this, [
64-
'loadedModules',
65-
name,
66-
domain,
67-
]);
68-
return Promise.resolve(loadedModule);
69-
} else {
70-
return dep.loader().then(loadedModule => {
71-
const { name, domain, module } = loadedModule;
72-
73-
op.set(
74-
this,
75-
['loadedModules', name, domain],
76-
loadedModule,
43+
return _.compact(
44+
await Promise.all(
45+
op.get(this.manifest, [type], []).map(async dep => {
46+
try {
47+
const { name, domain, loader } = dep;
48+
const { load = true } = await Reactium.Hook.run(
49+
'load-dependency',
50+
{ name, domain, module },
51+
Reactium.Enums.priority.highest,
52+
'REACTIUM-DEP-MODULE-LOAD',
7753
);
78-
return loadedModule;
79-
});
80-
}
81-
}),
54+
55+
if (load) {
56+
if (op.has(this, ['loadedModules', name, domain])) {
57+
const loadedModule = op.get(this, [
58+
'loadedModules',
59+
name,
60+
domain,
61+
]);
62+
63+
return loadedModule;
64+
} else {
65+
const loadedModule = await loader();
66+
const { name, domain } = loadedModule;
67+
68+
op.set(
69+
this,
70+
['loadedModules', name, domain],
71+
loadedModule,
72+
);
73+
74+
return loadedModule;
75+
}
76+
}
77+
} catch (error) {
78+
console.error('loadAll error', error);
79+
return Promise.resolve(undefined);
80+
}
81+
82+
return;
83+
}),
84+
),
8285
);
8386
}
8487

0 commit comments

Comments
 (0)