UNPKG

2.02 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _typeof2 = require('babel-runtime/helpers/typeof');
8
9var _typeof3 = _interopRequireDefault(_typeof2);
10
11var _appContext = require('./appContext');
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
15/*
16 A module should be defined as default export as a function the takes (appContext).
17
18 A module can add to the appContext by returning an object from this function where
19 the properties are the names and objects to add to the appContext. For example:
20
21 export default ({ existingService }) => { newService: new Service(existingService) }
22
23 The order is important as each module adds new services into the context, and then
24 the context is passed into the next module.
25 */
26
27var sanityCheckModules = function sanityCheckModules(modules) {
28 modules.forEach(function (m, i) {
29 if (typeof m !== 'function') {
30 throw new Error('initModules() module index ' + i + ' (of ' + modules.length + ') is ' + (typeof m === 'undefined' ? 'undefined' : (0, _typeof3.default)(m)) + ', should be function');
31 }
32 });
33};
34
35var sanityCheckDuplicateContextName = function sanityCheckDuplicateContextName(newNames) {
36 var existingContextNames = new Set(Object.keys(_appContext.appContext));
37 newNames.forEach(function (newName) {
38 if (existingContextNames.has(newName)) {
39 throw new ReferenceError('initModules() with more than one context member called "' + newName + '"');
40 }
41 });
42};
43
44exports.default = function (modules) {
45 sanityCheckModules(modules);
46 modules.forEach(function (module, i) {
47 try {
48 var context = module(_appContext.appContext);
49 if (context) {
50 sanityCheckDuplicateContextName(Object.keys(context));
51 Object.assign(_appContext.appContext, context);
52 }
53 } catch (e) {
54 console.info('initModules() module index ' + i + ' (of ' + modules.length + ') threw...'); // eslint-disable-line no-console
55 throw e;
56 }
57 });
58};
\No newline at end of file