Pass module context to type factories

master
Sven Slootweg 10 months ago
parent 4dcac61862
commit cee788c553

@ -293,7 +293,7 @@ module.exports = function createDLayer(options) {
}
async function make(typeID, args, existenceRequired) {
let type = loaded.types[typeID].func;
let type = loaded.types[typeID];
if (type == null) {
if (existenceRequired === true) {
@ -302,7 +302,7 @@ module.exports = function createDLayer(options) {
return;
}
} else {
let instance = await type(args);
let instance = await type.func(args, getContextForModule(type.__moduleID));
if (loaded.extensions[typeID] != null && instance !== InvalidObject) {
for (let [ key, extension ] of Object.entries(loaded.extensions[typeID])) {

@ -32,12 +32,12 @@ function createTypeTracker() {
throw new Error(`Type '${name}' already exists (from module '${module.name}', already defined by module '${existingEntry.source.name}')`);
} else {
typeFactories[name] = {
__moduleID: getModuleID(module),
source: module,
// 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: async function (... args) {
let instance = await factory(... args);
func: async function (args, context) {
// NOTE: We pass through the context here without modifying it; because a module-specific context will be injected from the main runtime
let instance = await factory(args, context);
if (instance !== InvalidObject) {
// We need to patch every created object, so that the correct context gets injected into its type-specific methods when they are called

Loading…
Cancel
Save