@equinor/fusion-framework-module
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -6,2 +6,10 @@ # Change Log | ||
## 1.2.3 (2022-10-03) | ||
**Note:** Version bump only for package @equinor/fusion-framework-module | ||
## [1.2.2](https://github.com/equinor/fusion-framework/compare/@equinor/fusion-framework-module@1.2.1...@equinor/fusion-framework-module@1.2.2) (2022-09-29) | ||
@@ -8,0 +16,0 @@ |
@@ -0,1 +1,10 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { BehaviorSubject, firstValueFrom, from, lastValueFrom, throwError } from 'rxjs'; | ||
@@ -11,8 +20,7 @@ import { filter, map, mergeMap, scan, timeout } from 'rxjs/operators'; | ||
export class ModulesConfigurator { | ||
logger = new ModuleConsoleLogger('ModulesConfigurator'); | ||
_configs = []; | ||
_afterConfiguration = []; | ||
_afterInit = []; | ||
_modules; | ||
constructor(modules) { | ||
this.logger = new ModuleConsoleLogger('ModulesConfigurator'); | ||
this._configs = []; | ||
this._afterConfiguration = []; | ||
this._afterInit = []; | ||
this._modules = new Set(modules); | ||
@@ -39,22 +47,27 @@ } | ||
} | ||
async initialize(ref) { | ||
const config = await this._configure(ref); | ||
const instance = await this._initialize(config, ref); | ||
await this._postInitialize(instance, ref); | ||
return Object.seal(Object.assign({}, instance, { | ||
dispose: () => this.dispose(instance), | ||
})); | ||
initialize(ref) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const config = yield this._configure(ref); | ||
const instance = yield this._initialize(config, ref); | ||
yield this._postInitialize(instance, ref); | ||
return Object.seal(Object.assign({}, instance, { | ||
dispose: () => this.dispose(instance), | ||
})); | ||
}); | ||
} | ||
async _configure(ref) { | ||
const config = await this._createConfig(ref); | ||
await Promise.all(this._configs.map((x) => Promise.resolve(x(config, ref)))); | ||
await this._postConfigure(config); | ||
return config; | ||
_configure(ref) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const config = yield this._createConfig(ref); | ||
yield Promise.all(this._configs.map((x) => Promise.resolve(x(config, ref)))); | ||
yield this._postConfigure(config); | ||
return config; | ||
}); | ||
} | ||
_createConfig(ref) { | ||
const { modules, logger, _afterConfiguration, _afterInit } = this; | ||
const config$ = from(modules).pipe(mergeMap(async (module) => { | ||
const config$ = from(modules).pipe(mergeMap((module) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
logger.debug(`🛠 creating configurator ${logger.formatModuleName(module)}`); | ||
try { | ||
const configurator = await module.configure?.(ref); | ||
const configurator = yield ((_a = module.configure) === null || _a === void 0 ? void 0 : _a.call(module, ref)); | ||
logger.debug(`🛠 created configurator for ${logger.formatModuleName(module)}`, configurator); | ||
@@ -67,3 +80,3 @@ return { [module.name]: configurator }; | ||
} | ||
}), scan((acc, module) => Object.assign(acc, module), { | ||
})), scan((acc, module) => Object.assign(acc, module), { | ||
onAfterConfiguration(cb) { | ||
@@ -78,114 +91,125 @@ _afterConfiguration.push(cb); | ||
} | ||
async _postConfigure(config) { | ||
const { modules, logger, _afterConfiguration: afterConfiguration } = this; | ||
await Promise.allSettled(modules | ||
.filter((module) => !!module.postConfigure) | ||
.map(async (module) => { | ||
try { | ||
await module.postConfigure?.(config); | ||
logger.debug(`🏗📌 post configured ${logger.formatModuleName(module)}`, module); | ||
_postConfigure(config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { modules, logger, _afterConfiguration: afterConfiguration } = this; | ||
yield Promise.allSettled(modules | ||
.filter((module) => !!module.postConfigure) | ||
.map((module) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
try { | ||
yield ((_a = module.postConfigure) === null || _a === void 0 ? void 0 : _a.call(module, config)); | ||
logger.debug(`🏗📌 post configured ${logger.formatModuleName(module)}`, module); | ||
} | ||
catch (err) { | ||
logger.warn(`🏗📌 post configure failed ${logger.formatModuleName(module)}`); | ||
} | ||
}))); | ||
if (afterConfiguration.length) { | ||
try { | ||
logger.debug(`🏗📌 post configure hooks [${afterConfiguration.length}]`); | ||
yield Promise.allSettled(afterConfiguration.map((x) => Promise.resolve(x(config)))); | ||
logger.debug(`🏗📌 post configure hooks complete`); | ||
} | ||
catch (err) { | ||
logger.warn(`🏗📌 post configure hook failed`, err); | ||
} | ||
} | ||
catch (err) { | ||
logger.warn(`🏗📌 post configure failed ${logger.formatModuleName(module)}`); | ||
} | ||
})); | ||
if (afterConfiguration.length) { | ||
try { | ||
logger.debug(`🏗📌 post configure hooks [${afterConfiguration.length}]`); | ||
await Promise.allSettled(afterConfiguration.map((x) => Promise.resolve(x(config)))); | ||
logger.debug(`🏗📌 post configure hooks complete`); | ||
} | ||
catch (err) { | ||
logger.warn(`🏗📌 post configure hook failed`, err); | ||
} | ||
} | ||
}); | ||
} | ||
async _initialize(config, ref) { | ||
const { modules, logger } = this; | ||
const moduleNames = modules.map((m) => m.name); | ||
const instance$ = new BehaviorSubject({}); | ||
const hasModule = (name) => moduleNames.includes(name); | ||
const requireInstance = (name, wait = 60) => { | ||
if (!moduleNames.includes(name)) { | ||
logger.error(`🚀⌛️ Cannot not require ${logger.formatModuleName(String(name))} since module is not defined!`); | ||
throw Error(`cannot not require [${String(name)}] since module is not defined!`); | ||
_initialize(config, ref) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { modules, logger } = this; | ||
const moduleNames = modules.map((m) => m.name); | ||
const instance$ = new BehaviorSubject({}); | ||
const hasModule = (name) => moduleNames.includes(name); | ||
const requireInstance = (name, wait = 60) => { | ||
if (!moduleNames.includes(name)) { | ||
logger.error(`🚀⌛️ Cannot not require ${logger.formatModuleName(String(name))} since module is not defined!`); | ||
throw Error(`cannot not require [${String(name)}] since module is not defined!`); | ||
} | ||
if (instance$.value[name]) { | ||
logger.debug(`🚀⌛️ ${logger.formatModuleName(String(name))} is initiated, skipping queue`); | ||
return Promise.resolve(instance$.value[name]); | ||
} | ||
logger.debug(`🚀⌛️ Awaiting init ${logger.formatModuleName(String(name))}, timeout ${wait}s`); | ||
return firstValueFrom(instance$.pipe(filter((x) => !!x[name]), map((x) => x[name]), timeout({ | ||
each: wait, | ||
with: () => throwError(() => new RequiredModuleTimeoutError()), | ||
}))); | ||
}; | ||
from(modules) | ||
.pipe(mergeMap((module) => { | ||
const key = module.name; | ||
logger.debug(`🚀 initializing ${logger.formatModuleName(module)}`); | ||
return from(Promise.resolve(module.initialize({ | ||
ref, | ||
config: config[key], | ||
requireInstance, | ||
hasModule, | ||
}))).pipe(map((instance) => { | ||
logger.debug(`🚀 initialized ${logger.formatModuleName(module)}`); | ||
return [key, instance]; | ||
})); | ||
})) | ||
.subscribe({ | ||
next: ([name, module]) => { | ||
instance$.next(Object.assign(instance$.value, { [name]: module })); | ||
}, | ||
complete: () => instance$.complete(), | ||
}); | ||
const instance = yield lastValueFrom(instance$); | ||
Object.seal(instance); | ||
return instance; | ||
}); | ||
} | ||
_postInitialize(instance, ref) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { modules, logger, _afterInit: afterInit } = this; | ||
yield Promise.allSettled(modules | ||
.filter((x) => !!x.postInitialize) | ||
.map((module) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
try { | ||
logger.debug(`🚀📌 post initializing moule ${logger.formatModuleName(module)}`); | ||
yield ((_a = module.postInitialize) === null || _a === void 0 ? void 0 : _a.call(module, { | ||
ref, | ||
modules: instance, | ||
instance: instance[module.name], | ||
})); | ||
logger.debug(`🚀📌 post initialized moule ${logger.formatModuleName(module)}`); | ||
} | ||
catch (err) { | ||
logger.warn(`🚀📌 post initialize failed moule ${logger.formatModuleName(module)}`); | ||
} | ||
}))); | ||
if (afterInit.length) { | ||
try { | ||
logger.debug(`🚀📌 post configure hooks [${afterInit.length}]`); | ||
yield Promise.allSettled(afterInit.map((x) => Promise.resolve(x(instance)))); | ||
logger.debug(`🚀📌 post configure hooks complete`); | ||
} | ||
catch (err) { | ||
logger.warn(`🚀📌 post configure hook failed`, err); | ||
} | ||
} | ||
if (instance$.value[name]) { | ||
logger.debug(`🚀⌛️ ${logger.formatModuleName(String(name))} is initiated, skipping queue`); | ||
return Promise.resolve(instance$.value[name]); | ||
} | ||
logger.debug(`🚀⌛️ Awaiting init ${logger.formatModuleName(String(name))}, timeout ${wait}s`); | ||
return firstValueFrom(instance$.pipe(filter((x) => !!x[name]), map((x) => x[name]), timeout({ | ||
each: wait, | ||
with: () => throwError(() => new RequiredModuleTimeoutError()), | ||
}))); | ||
}; | ||
from(modules) | ||
.pipe(mergeMap((module) => { | ||
const key = module.name; | ||
logger.debug(`🚀 initializing ${logger.formatModuleName(module)}`); | ||
return from(Promise.resolve(module.initialize({ | ||
ref, | ||
config: config[key], | ||
requireInstance, | ||
hasModule, | ||
}))).pipe(map((instance) => { | ||
logger.debug(`🚀 initialized ${logger.formatModuleName(module)}`); | ||
return [key, instance]; | ||
})); | ||
})) | ||
.subscribe({ | ||
next: ([name, module]) => { | ||
instance$.next(Object.assign(instance$.value, { [name]: module })); | ||
}, | ||
complete: () => instance$.complete(), | ||
logger.debug(`🎉 Modules initialized ${modules.map(logger.formatModuleName)}`, instance); | ||
logger.info('🟢 Modules initialized'); | ||
}); | ||
const instance = await lastValueFrom(instance$); | ||
Object.seal(instance); | ||
return instance; | ||
} | ||
async _postInitialize(instance, ref) { | ||
const { modules, logger, _afterInit: afterInit } = this; | ||
await Promise.allSettled(modules | ||
.filter((x) => !!x.postInitialize) | ||
.map(async (module) => { | ||
try { | ||
logger.debug(`🚀📌 post initializing moule ${logger.formatModuleName(module)}`); | ||
await module.postInitialize?.({ | ||
dispose(instance, ref) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { modules } = this; | ||
yield Promise.allSettled(modules | ||
.filter((module) => !!module.dispose) | ||
.map((module) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
yield ((_a = module.dispose) === null || _a === void 0 ? void 0 : _a.call(module, { | ||
ref, | ||
modules: instance, | ||
instance: instance[module.name], | ||
}); | ||
logger.debug(`🚀📌 post initialized moule ${logger.formatModuleName(module)}`); | ||
} | ||
catch (err) { | ||
logger.warn(`🚀📌 post initialize failed moule ${logger.formatModuleName(module)}`); | ||
} | ||
})); | ||
if (afterInit.length) { | ||
try { | ||
logger.debug(`🚀📌 post configure hooks [${afterInit.length}]`); | ||
await Promise.allSettled(afterInit.map((x) => Promise.resolve(x(instance)))); | ||
logger.debug(`🚀📌 post configure hooks complete`); | ||
} | ||
catch (err) { | ||
logger.warn(`🚀📌 post configure hook failed`, err); | ||
} | ||
} | ||
logger.debug(`🎉 Modules initialized ${modules.map(logger.formatModuleName)}`, instance); | ||
logger.info('🟢 Modules initialized'); | ||
})); | ||
}))); | ||
}); | ||
} | ||
async dispose(instance, ref) { | ||
const { modules } = this; | ||
await Promise.allSettled(modules | ||
.filter((module) => !!module.dispose) | ||
.map(async (module) => { | ||
await module.dispose?.({ | ||
ref, | ||
modules: instance, | ||
instance: instance[module.name], | ||
}); | ||
})); | ||
} | ||
} | ||
//# sourceMappingURL=configurator.js.map |
export class ConsoleLogger { | ||
domain; | ||
level = 1; | ||
constructor(domain) { | ||
this.domain = domain; | ||
this.level = 1; | ||
} | ||
@@ -7,0 +6,0 @@ _createMessage(msg) { |
{ | ||
"name": "@equinor/fusion-framework-module", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "", | ||
@@ -29,5 +29,5 @@ "main": "dist/esm/index.js", | ||
"devDependencies": { | ||
"typescript": "^4.8.2" | ||
"typescript": "^4.8.4" | ||
}, | ||
"gitHead": "81198e7772ed6e1f0c318fc37c5b4f2a9fe72ada" | ||
"gitHead": "573a97b7b0938a4cfad7063480ad88b5adab6ba1" | ||
} |
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
118241
857