oak-domain/lib/utils/module/combine.server.js

56 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const combine_common_1 = tslib_1.__importDefault(require("./combine.common"));
const lodash_1 = require("../../utils/lodash");
const assert_1 = tslib_1.__importDefault(require("assert"));
function combineModuleServer(...modules) {
const { checkers, common } = (0, combine_common_1.default)(...modules);
const others = modules.map((module) => ({
triggers: require(`${module}/lib/triggers`).default,
aspectDict: require(`${module}/lib/aspects`).default,
watchers: require(`${module}/lib/watchers`).default,
timers: require(`${module}/lib/timers`).default,
startRoutines: require(`${module}/lib/routines/start`).default,
importations: require(`${module}/lib/ports`).importations,
exportations: require(`${module}/lib/ports`).exportations,
data: require(`${module}/lib/data`).default,
})).reduce((prev, current, index) => {
const check = (module, name) => {
(0, assert_1.default)(typeof module.aspectDict === 'object', `${name}模块中的aspectDict不是对象`);
(0, assert_1.default)(typeof module.data === 'object', `${name}模块中的data不是对象`);
(0, assert_1.default)(module.exportations instanceof Array, `${name}模块中的exportations不是数组`);
(0, assert_1.default)(module.importations instanceof Array, `${name}模块中的importations不是数组`);
(0, assert_1.default)(module.watchers instanceof Array, `${name}模块中的watchers不是数组`);
(0, assert_1.default)(module.timers instanceof Array, `${name}模块中的timers不是数组`);
(0, assert_1.default)(module.triggers instanceof Array, `${name}模块中的triggers不是数组`);
(0, assert_1.default)(module.startRoutines instanceof Array, `${name}模块中的startRoutines不是数组`);
};
if (index === 1) {
check(prev, modules[0]);
}
check(current, modules[index]);
// aspectDict中不应当有同名对象
const its = (0, lodash_1.intersection)(Object.keys(prev.aspectDict), Object.keys(current.aspectDict));
if (its.length > 0) {
throw new Error(`模块${modules[index]}的aspectDict中存在和其它模块同步的aspect【${its.join(',')}】,请正确处理`);
}
return {
aspectDict: (0, lodash_1.mergeConcatArray)(prev.aspectDict, current.aspectDict),
data: (0, lodash_1.mergeConcatArray)(prev.data, current.data),
importations: (0, lodash_1.mergeConcatArray)(prev.importations, current.importations),
exportations: (0, lodash_1.mergeConcatArray)(prev.exportations, current.exportations),
watchers: (0, lodash_1.mergeConcatArray)(prev.watchers, current.watchers),
timers: (0, lodash_1.merge)(prev.timers, current.timers),
startRoutines: (0, lodash_1.merge)(prev.startRoutines, current.startRoutines),
triggers: (0, lodash_1.merge)(prev.triggers, current.triggers),
};
});
return {
checkers,
common,
...others,
};
}
exports.default = combineModuleServer;