@equinor/fusion-framework-module
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -6,2 +6,11 @@ # Change Log | ||
## [1.1.1](https://github.com/equinor/fusion-framework/compare/@equinor/fusion-framework-module@1.1.0...@equinor/fusion-framework-module@1.1.1) (2022-09-13) | ||
### Bug Fixes | ||
* update typings and linting ([7d2056b](https://github.com/equinor/fusion-framework/commit/7d2056b7866850b7efdfd4567385b5dbbcdf8761)) | ||
## [1.1.0](https://github.com/equinor/fusion-framework/compare/@equinor/fusion-framework-module@1.0.1...@equinor/fusion-framework-module@1.1.0) (2022-09-13) | ||
@@ -8,0 +17,0 @@ |
@@ -42,3 +42,5 @@ import { BehaviorSubject, firstValueFrom, from, lastValueFrom, throwError } from 'rxjs'; | ||
await this._postInitialize(instance, ref); | ||
return Object.seal(Object.assign({}, instance, { dispose: () => this.dispose(instance) })); | ||
return Object.seal(Object.assign({}, instance, { | ||
dispose: () => this.dispose(instance), | ||
})); | ||
} | ||
@@ -45,0 +47,0 @@ async _configure(ref) { |
@@ -1,3 +0,3 @@ | ||
export const initializeModules = async (configurator, ref) => configurator.initialize(ref); | ||
export const initializeModules = (configurator, ref) => configurator.initialize(ref); | ||
export default initializeModules; | ||
//# sourceMappingURL=initialize-modules.js.map |
import { ModuleConsoleLogger } from './logger'; | ||
import type { AnyModule, ModuleConfigType, ModuleInstance, ModulesConfig, ModulesConfigType, ModulesInstance, ModulesInstanceType, ModuleType } from './types'; | ||
import type { AnyModule, CombinedModules, ModuleConfigType, ModuleInstance, ModulesConfig, ModulesConfigType, ModulesInstance, ModulesInstanceType, ModuleType } from './types'; | ||
export interface IModulesConfigurator<TModules extends Array<AnyModule> = [], TRef = any> { | ||
configure<T extends Array<AnyModule> = TModules>(...configs: Array<IModuleConfigurator<T[number]>>): void; | ||
addConfig<T extends AnyModule = TModules[number]>(config: IModuleConfigurator<T>): void; | ||
initialize<T extends Array<AnyModule> = TModules>(ref?: TRef): Promise<ModulesInstance<T>>; | ||
onConfigured<T extends Array<AnyModule> = TModules>(cb: (config: ModulesConfigType<T>) => void | Promise<void>): void; | ||
onInitialized<T extends Array<AnyModule> = TModules>(cb: (instance: ModulesInstanceType<T>) => void): void; | ||
dispose<T extends Array<AnyModule> = TModules>(instance: ModulesInstanceType<T>, ref?: TRef): Promise<void>; | ||
logger: ModuleConsoleLogger; | ||
configure(...configs: Array<IModuleConfigurator>): void; | ||
addConfig<T extends AnyModule>(config: IModuleConfigurator<T>): void; | ||
initialize<T extends Array<AnyModule> | unknown>(ref?: TRef): Promise<ModulesInstance<CombinedModules<T, TModules>>>; | ||
onConfigured<T>(cb: (config: ModulesConfigType<CombinedModules<T, TModules>>) => void | Promise<void>): void; | ||
onInitialized<T extends Array<AnyModule> | unknown>(cb: (instance: ModulesInstanceType<CombinedModules<T, TModules>>) => void): void; | ||
dispose(instance: ModulesInstanceType<TModules>, ref?: TRef): Promise<void>; | ||
} | ||
export interface IModuleConfigurator<TModule extends AnyModule, TRef = ModuleInstance> { | ||
export interface IModuleConfigurator<TModule extends AnyModule = AnyModule, TRef = ModuleInstance> { | ||
module: TModule; | ||
@@ -17,3 +18,3 @@ configure?: (config: ModuleConfigType<TModule>, ref?: TRef) => void | Promise<void>; | ||
} | ||
export declare class ModulesConfigurator<TModules extends Array<AnyModule>, TRef = any> implements IModulesConfigurator<TModules, TRef> { | ||
export declare class ModulesConfigurator<TModules extends Array<AnyModule> = Array<AnyModule>, TRef = any> implements IModulesConfigurator<TModules, TRef> { | ||
logger: ModuleConsoleLogger; | ||
@@ -26,13 +27,13 @@ protected _configs: Array<(config: ModulesConfig<[AnyModule]>) => void | Promise<void>>; | ||
get modules(): Array<AnyModule>; | ||
configure<T extends Array<AnyModule> = TModules>(...configs: Array<IModuleConfigurator<T[number]>>): void; | ||
configure(...configs: Array<IModuleConfigurator>): void; | ||
addConfig<T extends AnyModule>(config: IModuleConfigurator<T>): void; | ||
onConfigured<T extends Array<AnyModule> = TModules>(cb: (config: ModulesConfigType<T>) => void | Promise<void>): void; | ||
onInitialized<T extends Array<AnyModule> = TModules>(cb: (instance: ModulesInstanceType<T>) => void): void; | ||
initialize<T extends Array<AnyModule> = TModules, R = TRef>(ref?: R): Promise<ModulesInstance<T>>; | ||
protected _configure<T extends Array<AnyModule> = TModules, R = TRef>(ref?: R): Promise<ModulesConfig<T>>; | ||
protected _createConfig<T extends Array<AnyModule> = TModules, R = TRef>(ref?: R): Promise<ModulesConfig<T>>; | ||
protected _postConfigure<T extends Array<AnyModule> = TModules>(config: ModulesConfigType<T>): Promise<void>; | ||
protected _initialize<T extends Array<AnyModule> = TModules, R = TRef>(config: ModulesConfigType<T>, ref?: R): Promise<ModulesInstanceType<T>>; | ||
protected _postInitialize<T extends Array<AnyModule> = TModules, R = TRef>(instance: ModulesInstanceType<T>, ref?: R): Promise<void>; | ||
dispose<T extends Array<AnyModule> = TModules, R = TRef>(instance: ModulesInstanceType<T>, ref?: R): Promise<void>; | ||
onConfigured<T>(cb: (config: ModulesConfigType<CombinedModules<T, TModules>>) => void | Promise<void>): void; | ||
onInitialized<T>(cb: (instance: ModulesInstanceType<CombinedModules<T, TModules>>) => void): void; | ||
initialize<T, R = TRef>(ref?: R): Promise<ModulesInstance<CombinedModules<T, TModules>>>; | ||
protected _configure<T, R = TRef>(ref?: R): Promise<ModulesConfig<CombinedModules<T, TModules>>>; | ||
protected _createConfig<T, R = TRef>(ref?: R): Promise<ModulesConfig<CombinedModules<T, TModules>>>; | ||
protected _postConfigure<T>(config: ModulesConfigType<CombinedModules<T, TModules>>): Promise<void>; | ||
protected _initialize<T, R = TRef>(config: ModulesConfigType<CombinedModules<T, TModules>>, ref?: R): Promise<ModulesInstanceType<CombinedModules<T, TModules>>>; | ||
protected _postInitialize<T, R = TRef>(instance: ModulesInstanceType<CombinedModules<T, TModules>>, ref?: R): Promise<void>; | ||
dispose(instance: ModulesInstanceType<TModules>, ref?: TRef): Promise<void>; | ||
} |
import { IModulesConfigurator } from './configurator'; | ||
import type { AnyModule, ModulesInstanceType } from './types'; | ||
export declare const initializeModules: <TModules extends AnyModule[], TInstance = any>(configurator: IModulesConfigurator<TModules, TInstance>, ref?: TInstance | undefined) => Promise<ModulesInstanceType<TModules> & { | ||
dispose: VoidFunction; | ||
}>; | ||
import type { AnyModule } from './types'; | ||
export declare const initializeModules: <TModules extends AnyModule[], TInstance = any>(configurator: IModulesConfigurator<TModules, TInstance>, ref?: TInstance | undefined) => Promise<import("./types").ModulesInstance<TModules extends AnyModule[] ? TModules : never>>; | ||
export default initializeModules; |
@@ -22,2 +22,3 @@ export interface Module<TKey extends string, TType, TConfig, TDeps extends Array<AnyModule> = []> { | ||
export declare type AnyModule = Module<any, any, any, any>; | ||
export declare type CombinedModules<T1, T2> = T1 extends Array<AnyModule> ? T2 extends Array<AnyModule> ? [...T1, ...T2] : T1 : T2 extends Array<AnyModule> ? T2 : never; | ||
export declare type AnyModuleInstance = Record<string, AnyModule>; | ||
@@ -24,0 +25,0 @@ export declare type ModuleKey<M> = M extends Module<infer TKey, any, any, any> ? TKey : never; |
{ | ||
"name": "@equinor/fusion-framework-module", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "", | ||
@@ -31,3 +31,3 @@ "main": "dist/esm/index.js", | ||
}, | ||
"gitHead": "e4865f46e0e01bb8efdbe0a542e052255ec570a8" | ||
"gitHead": "ed86940ac2091df63390cebd4c34d54ad02ed3fc" | ||
} |
@@ -9,2 +9,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
AnyModule, | ||
CombinedModules, | ||
ModuleConfigType, | ||
@@ -20,25 +21,23 @@ ModuleInstance, | ||
export interface IModulesConfigurator<TModules extends Array<AnyModule> = [], TRef = any> { | ||
configure<T extends Array<AnyModule> = TModules>( | ||
...configs: Array<IModuleConfigurator<T[number]>> | ||
): void; | ||
logger: ModuleConsoleLogger; | ||
configure(...configs: Array<IModuleConfigurator>): void; | ||
addConfig<T extends AnyModule = TModules[number]>(config: IModuleConfigurator<T>): void; | ||
addConfig<T extends AnyModule>(config: IModuleConfigurator<T>): void; | ||
initialize<T extends Array<AnyModule> = TModules>(ref?: TRef): Promise<ModulesInstance<T>>; | ||
initialize<T extends Array<AnyModule> | unknown>( | ||
ref?: TRef | ||
): Promise<ModulesInstance<CombinedModules<T, TModules>>>; | ||
onConfigured<T extends Array<AnyModule> = TModules>( | ||
cb: (config: ModulesConfigType<T>) => void | Promise<void> | ||
onConfigured<T>( | ||
cb: (config: ModulesConfigType<CombinedModules<T, TModules>>) => void | Promise<void> | ||
): void; | ||
onInitialized<T extends Array<AnyModule> = TModules>( | ||
cb: (instance: ModulesInstanceType<T>) => void | ||
onInitialized<T extends Array<AnyModule> | unknown>( | ||
cb: (instance: ModulesInstanceType<CombinedModules<T, TModules>>) => void | ||
): void; | ||
dispose<T extends Array<AnyModule> = TModules>( | ||
instance: ModulesInstanceType<T>, | ||
ref?: TRef | ||
): Promise<void>; | ||
dispose(instance: ModulesInstanceType<TModules>, ref?: TRef): Promise<void>; | ||
} | ||
export interface IModuleConfigurator<TModule extends AnyModule, TRef = ModuleInstance> { | ||
export interface IModuleConfigurator<TModule extends AnyModule = AnyModule, TRef = ModuleInstance> { | ||
module: TModule; | ||
@@ -59,3 +58,3 @@ configure?: (config: ModuleConfigType<TModule>, ref?: TRef) => void | Promise<void>; | ||
export class ModulesConfigurator<TModules extends Array<AnyModule>, TRef = any> | ||
export class ModulesConfigurator<TModules extends Array<AnyModule> = Array<AnyModule>, TRef = any> | ||
implements IModulesConfigurator<TModules, TRef> | ||
@@ -79,6 +78,4 @@ { | ||
public configure<T extends Array<AnyModule> = TModules>( | ||
...configs: Array<IModuleConfigurator<T[number]>> | ||
) { | ||
configs.forEach((x) => this.addConfig<T[number]>(x)); | ||
public configure(...configs: Array<IModuleConfigurator>) { | ||
configs.forEach((x) => this.addConfig(x)); | ||
} | ||
@@ -94,4 +91,4 @@ | ||
public onConfigured<T extends Array<AnyModule> = TModules>( | ||
cb: (config: ModulesConfigType<T>) => void | Promise<void> | ||
public onConfigured<T>( | ||
cb: (config: ModulesConfigType<CombinedModules<T, TModules>>) => void | Promise<void> | ||
) { | ||
@@ -101,4 +98,4 @@ this._afterConfiguration.push(cb); | ||
public onInitialized<T extends Array<AnyModule> = TModules>( | ||
cb: (instance: ModulesInstanceType<T>) => void | ||
public onInitialized<T>( | ||
cb: (instance: ModulesInstanceType<CombinedModules<T, TModules>>) => void | ||
): void { | ||
@@ -108,14 +105,18 @@ this._afterInit.push(cb); | ||
public async initialize<T extends Array<AnyModule> = TModules, R = TRef>( | ||
public async initialize<T, R = TRef>( | ||
ref?: R | ||
): Promise<ModulesInstance<T>> { | ||
): Promise<ModulesInstance<CombinedModules<T, TModules>>> { | ||
const config = await this._configure<T, R>(ref); | ||
const instance = await this._initialize<T, R>(config, ref); | ||
await this._postInitialize<T, R>(instance, ref); | ||
return Object.seal(Object.assign({}, instance, { dispose: () => this.dispose(instance) })); | ||
return Object.seal( | ||
Object.assign({}, instance, { | ||
dispose: () => this.dispose(instance as unknown as ModulesInstance<TModules>), | ||
}) | ||
); | ||
} | ||
protected async _configure<T extends Array<AnyModule> = TModules, R = TRef>( | ||
protected async _configure<T, R = TRef>( | ||
ref?: R | ||
): Promise<ModulesConfig<T>> { | ||
): Promise<ModulesConfig<CombinedModules<T, TModules>>> { | ||
const config = await this._createConfig<T, R>(ref); | ||
@@ -127,5 +128,5 @@ await Promise.all(this._configs.map((x) => Promise.resolve(x(config)))); | ||
protected _createConfig<T extends Array<AnyModule> = TModules, R = TRef>( | ||
protected _createConfig<T, R = TRef>( | ||
ref?: R | ||
): Promise<ModulesConfig<T>> { | ||
): Promise<ModulesConfig<CombinedModules<T, TModules>>> { | ||
const { modules, logger, _afterConfiguration, _afterInit } = this; | ||
@@ -158,10 +159,10 @@ const config$ = from(modules).pipe( | ||
}, | ||
} as ModulesConfig<T>) | ||
} as ModulesConfig<CombinedModules<T, TModules>>) | ||
); | ||
return lastValueFrom(config$) as Promise<ModulesConfig<T>>; | ||
return lastValueFrom(config$); | ||
} | ||
protected async _postConfigure<T extends Array<AnyModule> = TModules>( | ||
config: ModulesConfigType<T> | ||
protected async _postConfigure<T>( | ||
config: ModulesConfigType<CombinedModules<T, TModules>> | ||
): Promise<void> { | ||
@@ -197,15 +198,19 @@ const { modules, logger, _afterConfiguration: afterConfiguration } = this; | ||
protected async _initialize<T extends Array<AnyModule> = TModules, R = TRef>( | ||
config: ModulesConfigType<T>, | ||
protected async _initialize<T, R = TRef>( | ||
config: ModulesConfigType<CombinedModules<T, TModules>>, | ||
ref?: R | ||
): Promise<ModulesInstanceType<T>> { | ||
): Promise<ModulesInstanceType<CombinedModules<T, TModules>>> { | ||
const { modules, logger } = this; | ||
const moduleNames = modules.map((m) => m.name); | ||
const instance$ = new BehaviorSubject<ModulesInstanceType<T>>({} as ModulesInstanceType<T>); | ||
const instance$ = new BehaviorSubject<ModulesInstanceType<CombinedModules<T, TModules>>>( | ||
{} as ModulesInstanceType<CombinedModules<T, TModules>> | ||
); | ||
const requireInstance = <TKey extends keyof ModulesInstanceType<T>>( | ||
const requireInstance = < | ||
TKey extends keyof ModulesInstanceType<CombinedModules<T, TModules>> | ||
>( | ||
name: TKey, | ||
wait = 60 | ||
): Promise<ModulesInstanceType<T>[TKey]> => { | ||
): Promise<ModulesInstanceType<CombinedModules<T, TModules>>[TKey]> => { | ||
if (!moduleNames.includes(name)) { | ||
@@ -276,4 +281,4 @@ logger.error( | ||
protected async _postInitialize<T extends Array<AnyModule> = TModules, R = TRef>( | ||
instance: ModulesInstanceType<T>, | ||
protected async _postInitialize<T, R = TRef>( | ||
instance: ModulesInstanceType<CombinedModules<T, TModules>>, | ||
ref?: R | ||
@@ -294,3 +299,8 @@ ) { | ||
modules: instance, | ||
instance: instance[module.name as keyof ModulesInstanceType<T>], | ||
instance: | ||
instance[ | ||
module.name as keyof ModulesInstanceType< | ||
CombinedModules<T, TModules> | ||
> | ||
], | ||
}); | ||
@@ -322,6 +332,3 @@ logger.debug( | ||
public async dispose<T extends Array<AnyModule> = TModules, R = TRef>( | ||
instance: ModulesInstanceType<T>, | ||
ref?: R | ||
): Promise<void> { | ||
public async dispose(instance: ModulesInstanceType<TModules>, ref?: TRef): Promise<void> { | ||
const { modules } = this; | ||
@@ -335,3 +342,3 @@ await Promise.allSettled( | ||
modules: instance, | ||
instance: instance[module.name], | ||
instance: instance[module.name as keyof typeof instance], | ||
}); | ||
@@ -338,0 +345,0 @@ }) |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { IModulesConfigurator } from './configurator'; | ||
import type { AnyModule, ModulesInstanceType } from './types'; | ||
import type { AnyModule } from './types'; | ||
@@ -13,8 +13,7 @@ /** | ||
*/ | ||
export const initializeModules = async <TModules extends Array<AnyModule>, TInstance = any>( | ||
export const initializeModules = <TModules extends Array<AnyModule>, TInstance = any>( | ||
configurator: IModulesConfigurator<TModules, TInstance>, | ||
ref?: TInstance | ||
): Promise<ModulesInstanceType<TModules> & { dispose: VoidFunction }> => | ||
configurator.initialize(ref); | ||
) => configurator.initialize(ref); | ||
export default initializeModules; |
@@ -30,2 +30,9 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export type AnyModule = Module<any, any, any, any>; | ||
export type CombinedModules<T1, T2> = T1 extends Array<AnyModule> | ||
? T2 extends Array<AnyModule> | ||
? [...T1, ...T2] | ||
: T1 | ||
: T2 extends Array<AnyModule> | ||
? T2 | ||
: never; | ||
export type AnyModuleInstance = Record<string, AnyModule>; | ||
@@ -32,0 +39,0 @@ export type ModuleKey<M> = M extends Module<infer TKey, any, any, any> ? TKey : never; |
Sorry, the diff of this file is not supported yet
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
116750
793