|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
// const mergeByTemplate = require("merge-by-template");
|
|
|
|
|
const syncpipe = require("syncpipe");
|
|
|
|
|
const mapObject = require("map-obj");
|
|
|
|
|
const deepMergeAndMap = require("./deep-merge-and-map");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -34,7 +35,14 @@ function createTypeTracker() {
|
|
|
|
|
// No context provided to type factory functions for now, since they are not allowed to be async for now anyway
|
|
|
|
|
// FIXME: Maybe add a warning if the user returns a Promise from a factory, asking them to file a bug if they really need it?
|
|
|
|
|
// func: wrapModuleFunction(module, factory)
|
|
|
|
|
func: factory
|
|
|
|
|
func: function (... args) {
|
|
|
|
|
let instance = factory(... args);
|
|
|
|
|
|
|
|
|
|
// We need to patch every created object, so that the correct context gets injected into its type-specific methods when they are called
|
|
|
|
|
return mapObject(instance, (key, value) => {
|
|
|
|
|
return [ key, wrapModuleValue(module, value) ];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
@ -75,6 +83,14 @@ function wrapModuleFunction(module, func) {
|
|
|
|
|
return { __moduleID: getModuleID(module), func: func };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wrapModuleValue(module, value) {
|
|
|
|
|
if (typeof value === "function") {
|
|
|
|
|
return wrapModuleFunction(module, value);
|
|
|
|
|
} else {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function defaultContext() {
|
|
|
|
|
// Fallback function that generates an empty context, for when a module doesn't specify a makeContext handler
|
|
|
|
|
return {};
|
|
|
|
@ -93,13 +109,7 @@ module.exports = function (modules) {
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
let schema = modules.reduce((schema, module) => {
|
|
|
|
|
return deepMergeAndMap(schema, module.root, (value) => {
|
|
|
|
|
if (typeof value === "function") {
|
|
|
|
|
return wrapModuleFunction(module, value);
|
|
|
|
|
} else {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return deepMergeAndMap(schema, module.root, (value) => wrapModuleValue(module, value));
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
for (let module of modules) {
|
|
|
|
|