Patch type methods with module context

master
Sven Slootweg 1 year ago
parent 325f239026
commit 16d6f8abb2

@ -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) {

@ -24,7 +24,11 @@ let moduleDrives = {
types: {
"sysquery.core.Drive": function ({ name }) {
return {
name: name
name: name,
testContext: function (_, { counter }) {
console.log(`[context ${counter}] Drive::testContext`);
return true;
}
};
}
},
@ -66,7 +70,12 @@ let moduleBlockDevices = {
types: {
"sysquery.core.BlockDevice": function ({ path }) {
return {
path: path
path: path,
testContext: function (_, { counter }) {
// MARKER: Patch context on type methods, and continue testing LVM implementation in CVM/sysquery
console.log(`[context ${counter}] BlockDevice::testContext`);
return true;
}
};
}
},
@ -119,8 +128,10 @@ return Promise.try(() => {
blockDevices: {
$arguments: { paths: [ "/dev/1b" ] },
path: true,
testContext: true,
drive: {
name: true,
testContext: true,
blockDevices: {
path: true
}

Loading…
Cancel
Save