UNPKG

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