@equinor/fusion-framework-module
Advanced tools
Comparing version 0.1.0-alpha.7 to 0.1.0-alpha.9
export const initializeModules = async (configure, modules, ref) => { | ||
const afterConfiguration = []; | ||
const afterInit = []; | ||
const config = await Object.values(modules).reduce(async (acc, module) => { | ||
@@ -6,6 +8,13 @@ const obj = await acc; | ||
return Object.assign(obj, { [module.name]: res }); | ||
}, Promise.resolve({})); | ||
}, Promise.resolve({ | ||
onAfterConfiguration(cb) { | ||
afterConfiguration.push(cb); | ||
}, | ||
onAfterInit(cb) { | ||
afterInit.push(cb); | ||
}, | ||
})); | ||
Object.seal(config); | ||
configure && (await configure(config, ref)); | ||
await Promise.all(Object.values(modules).map((x) => Promise.resolve(x.postConfigure?.(config)))); | ||
await Promise.all([...modules.map((x) => x.postConfigure), ...afterConfiguration].map((x) => Promise.resolve(x?.(config)))); | ||
const instance = await modules.reduce(async (acc, module) => { | ||
@@ -17,3 +26,3 @@ const obj = await acc; | ||
Object.seal(ref); | ||
await Promise.all(modules.map((x) => Promise.resolve(x.postInitialize?.(instance)))); | ||
await Promise.all([...modules.map((x) => x.postInitialize), ...afterInit].map((x) => Promise.resolve(x?.(instance)))); | ||
return instance; | ||
@@ -20,0 +29,0 @@ }; |
@@ -19,6 +19,11 @@ export interface Module<TKey extends string, TType, TConfig, TDeps extends Array<AnyModule> = []> { | ||
export interface ModulesConfigurator<TModules extends Array<AnyModule>, TRef extends any = ModuleInstance> { | ||
(config: ModulesObjectConfigType<ModulesType<TModules>>, ref?: TRef): void | Promise<void>; | ||
(config: ModulesConfig<ModulesType<TModules>>, ref?: TRef): void | Promise<void>; | ||
} | ||
export declare type ModulesConfigType<TModules extends Array<AnyModule> | Record<string, AnyModule>> = TModules extends Array<AnyModule> ? ModulesObjectConfigType<ModulesType<TModules>> : TModules extends Record<string, AnyModule> ? ModulesObjectConfigType<TModules> : never; | ||
export declare type ModulesInstanceType<TModules extends Array<AnyModule> | Record<string, AnyModule>> = TModules extends Array<AnyModule> ? ModulesObjectInstanceType<ModulesType<TModules>> : TModules extends Record<string, AnyModule> ? ModulesObjectInstanceType<TModules> : never; | ||
export interface IModulesConfig<M extends Array<AnyModule> | Record<string, AnyModule>> { | ||
onAfterConfiguration: (cb: (config: ModulesConfigType<M>) => void | Promise<void>) => void; | ||
onAfterInit: (cb: (instance: ModulesInstanceType<M>) => void | Promise<void>) => void; | ||
} | ||
export declare type ModulesConfig<M extends Array<AnyModule> | Record<string, AnyModule>> = ModulesConfigType<M> & IModulesConfig<M>; | ||
declare type ModulesObjectInstanceType<M extends Record<string, AnyModule>> = { | ||
@@ -25,0 +30,0 @@ [K in keyof M as Extract<K, string>]: ModuleType<M[K]>; |
{ | ||
"name": "@equinor/fusion-framework-module", | ||
"version": "0.1.0-alpha.7", | ||
"version": "0.1.0-alpha.9", | ||
"description": "", | ||
@@ -31,3 +31,3 @@ "main": "./dist/esm/index.js", | ||
}, | ||
"gitHead": "207608e4f11f66119db73a722325048e12487a0e" | ||
"gitHead": "b12e2a8caae0b987080b7cab2aa09e9423393e54" | ||
} |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { | ||
AnyModule, | ||
ModulesConfig, | ||
ModulesConfigType, | ||
@@ -24,8 +25,21 @@ ModulesConfigurator, | ||
): Promise<ModulesInstanceType<TModules>> => { | ||
const afterConfiguration: Array<(config: ModulesConfigType<TModules>) => void> = []; | ||
const afterInit: Array<(instance: ModulesInstanceType<TModules>) => void> = []; | ||
/** initialize config providers for all modules */ | ||
const config = await Object.values(modules).reduce(async (acc, module) => { | ||
const obj = await acc; | ||
const res = await Promise.resolve(module.configure?.(ref)); | ||
return Object.assign(obj, { [module.name]: res }); | ||
}, Promise.resolve({} as ModulesConfigType<TModules>)); | ||
const config: ModulesConfig<TModules> = await Object.values(modules).reduce( | ||
async (acc, module) => { | ||
const obj = await acc; | ||
const res = await Promise.resolve(module.configure?.(ref)); | ||
return Object.assign(obj, { [module.name]: res }); | ||
}, | ||
Promise.resolve({ | ||
onAfterConfiguration(cb: (config: ModulesConfigType<TModules>) => void) { | ||
afterConfiguration.push(cb); | ||
}, | ||
onAfterInit(cb: (instance: ModulesInstanceType<TModules>) => void) { | ||
afterInit.push(cb); | ||
}, | ||
} as ModulesConfig<TModules>) | ||
); | ||
@@ -38,5 +52,7 @@ /** protected config instance */ | ||
/** call all post config hooks */ | ||
/** call all added post config hooks */ | ||
await Promise.all( | ||
Object.values(modules).map((x) => Promise.resolve(x.postConfigure?.(config))) | ||
[...modules.map((x) => x.postConfigure), ...afterConfiguration].map((x) => | ||
Promise.resolve(x?.(config)) | ||
) | ||
); | ||
@@ -47,3 +63,5 @@ | ||
const obj = await acc; | ||
const res = await Promise.resolve(module.initialize(config, obj)); | ||
const res = await Promise.resolve( | ||
module.initialize(config as unknown as ModulesConfigType<TModules>, obj) | ||
); | ||
return Object.assign(obj, { [module.name]: res }); | ||
@@ -55,4 +73,8 @@ }, Promise.resolve({} as ModulesInstanceType<TModules>)); | ||
/** call all post initializers hooks */ | ||
await Promise.all(modules.map((x) => Promise.resolve(x.postInitialize?.(instance)))); | ||
/** call all added post config hooks */ | ||
await Promise.all( | ||
[...modules.map((x) => x.postInitialize), ...afterInit].map((x) => | ||
Promise.resolve(x?.(instance)) | ||
) | ||
); | ||
@@ -59,0 +81,0 @@ return instance; |
@@ -40,3 +40,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
> { | ||
(config: ModulesObjectConfigType<ModulesType<TModules>>, ref?: TRef): void | Promise<void>; | ||
(config: ModulesConfig<ModulesType<TModules>>, ref?: TRef): void | Promise<void>; | ||
} | ||
@@ -59,2 +59,10 @@ | ||
export interface IModulesConfig<M extends Array<AnyModule> | Record<string, AnyModule>> { | ||
onAfterConfiguration: (cb: (config: ModulesConfigType<M>) => void | Promise<void>) => void; | ||
onAfterInit: (cb: (instance: ModulesInstanceType<M>) => void | Promise<void>) => void; | ||
} | ||
export type ModulesConfig<M extends Array<AnyModule> | Record<string, AnyModule>> = | ||
ModulesConfigType<M> & IModulesConfig<M>; | ||
/** === Internal helpers === */ | ||
@@ -61,0 +69,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32602
223