Compare commits

...

2 Commits

@ -2,6 +2,7 @@
// const mergeByTemplate = require("merge-by-template"); // const mergeByTemplate = require("merge-by-template");
const syncpipe = require("syncpipe"); const syncpipe = require("syncpipe");
const mapObject = require("map-obj");
const deepMergeAndMap = require("./deep-merge-and-map"); 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 // 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? // 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: 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 }; return { __moduleID: getModuleID(module), func: func };
} }
function wrapModuleValue(module, value) {
if (typeof value === "function") {
return wrapModuleFunction(module, value);
} else {
return value;
}
}
function defaultContext() { function defaultContext() {
// Fallback function that generates an empty context, for when a module doesn't specify a makeContext handler // Fallback function that generates an empty context, for when a module doesn't specify a makeContext handler
return {}; return {};
@ -93,13 +109,7 @@ module.exports = function (modules) {
]); ]);
let schema = modules.reduce((schema, module) => { let schema = modules.reduce((schema, module) => {
return deepMergeAndMap(schema, module.root, (value) => { return deepMergeAndMap(schema, module.root, (value) => wrapModuleValue(module, value));
if (typeof value === "function") {
return wrapModuleFunction(module, value);
} else {
return value;
}
});
}, {}); }, {});
for (let module of modules) { for (let module of modules) {

@ -24,7 +24,11 @@ let moduleDrives = {
types: { types: {
"sysquery.core.Drive": function ({ name }) { "sysquery.core.Drive": function ({ name }) {
return { return {
name: name name: name,
testContext: function (_, { counter }) {
console.log(`[context ${counter}] Drive::testContext`);
return true;
}
}; };
} }
}, },
@ -66,7 +70,12 @@ let moduleBlockDevices = {
types: { types: {
"sysquery.core.BlockDevice": function ({ path }) { "sysquery.core.BlockDevice": function ({ path }) {
return { 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: { blockDevices: {
$arguments: { paths: [ "/dev/1b" ] }, $arguments: { paths: [ "/dev/1b" ] },
path: true, path: true,
testContext: true,
drive: { drive: {
name: true, name: true,
testContext: true,
blockDevices: { blockDevices: {
path: true path: true
} }

@ -1,6 +1,6 @@
{ {
"name": "dlayer", "name": "dlayer",
"version": "0.1.1", "version": "0.1.2",
"main": "index.js", "main": "index.js",
"repository": "https://git.cryto.net/joepie91/dlayer.git", "repository": "https://git.cryto.net/joepie91/dlayer.git",
"author": "Sven Slootweg <admin@cryto.net>", "author": "Sven Slootweg <admin@cryto.net>",

Loading…
Cancel
Save