@bettercorp/service-base
Advanced tools
Comparing version 8.5.62 to 8.5.63
@@ -1,12 +0,2 @@ | ||
import { BSBService, DEBUG_MODE, SBConfig, SBEvents, SBLogging, SBPlugins, IPluginDefinition, BSBServiceClient, LoadedPlugin } from "../"; | ||
interface SortedPlugin { | ||
type: "service" | "client"; | ||
srcPluginName: string; | ||
pluginName: string; | ||
initBeforePlugins?: string[]; | ||
initAfterPlugins?: string[]; | ||
runBeforePlugins?: string[]; | ||
runAfterPlugins?: string[]; | ||
reference: BSBService | BSBServiceClient<any>; | ||
} | ||
import { DEBUG_MODE, SBConfig, SBEvents, SBLogging, SBPlugins, IPluginDefinition, LoadedPlugin } from "../"; | ||
export declare class SBServices { | ||
@@ -27,7 +17,8 @@ private _activeServices; | ||
private addService; | ||
private sortByInitDependencies; | ||
sortAndRunOrInitPlugins(type: "init" | "run"): Promise<void>; | ||
private getTextReportedDeps; | ||
private gatherListOfPlugins; | ||
init(): Promise<void>; | ||
sortAndRunOrInitPlugins(plugins: SortedPlugin[], type: "init" | "run"): Promise<void>; | ||
private getTextReportedDeps; | ||
run(): Promise<void>; | ||
} | ||
export {}; |
@@ -66,3 +66,3 @@ "use strict"; | ||
setupPluginClient = async (sbConfig, sbLogging, sbEvents, context, clientContext) => { | ||
this.log.debug('Create plugin client {clientName} in {serviceName}', { | ||
this.log.debug("Create plugin client {clientName} in {serviceName}", { | ||
clientName: clientContext.pluginName, | ||
@@ -180,90 +180,40 @@ serviceName: context.pluginName, | ||
} | ||
async init() { | ||
this.log.info("Init all services"); | ||
const list = []; | ||
for (const service of this._activeServices) { | ||
this.log.debug("Mapping required plugins list for {plugin} [initBeforePlugins:{initBeforePlugins}][initAfterPlugins:{initAfterPlugins}]", { | ||
plugin: service.pluginName, | ||
initBeforePlugins: this.getTextReportedDeps(service.initBeforePlugins), | ||
initAfterPlugins: this.getTextReportedDeps(service.initAfterPlugins), | ||
}); | ||
list.push({ | ||
type: "service", | ||
srcPluginName: service.pluginName, | ||
pluginName: service.pluginName, | ||
initBeforePlugins: service.initBeforePlugins, | ||
initAfterPlugins: service.initAfterPlugins, | ||
runBeforePlugins: service.runBeforePlugins, | ||
runAfterPlugins: service.runAfterPlugins, | ||
reference: service, | ||
}); | ||
for (const client of service._clients) { | ||
this.log.debug(" - {plugin} -> {client} [initBeforePlugins:{initBeforePlugins}][initAfterPlugins:{initAfterPlugins}]", { | ||
plugin: service.pluginName, | ||
client: client.pluginName, | ||
initBeforePlugins: this.getTextReportedDeps(client.initBeforePlugins), | ||
initAfterPlugins: this.getTextReportedDeps(client.initAfterPlugins), | ||
}); | ||
list.push({ | ||
type: "client", | ||
srcPluginName: service.pluginName, | ||
pluginName: client.pluginName, | ||
initBeforePlugins: client.initBeforePlugins, | ||
initAfterPlugins: client.initAfterPlugins, | ||
runBeforePlugins: client.runBeforePlugins, | ||
runAfterPlugins: client.runAfterPlugins, | ||
reference: client, | ||
}); | ||
sortByInitDependencies(type) { | ||
return (a, b) => { | ||
if ((type == "init" ? a.initBeforePlugins : a.runBeforePlugins).includes(b.pluginName)) { | ||
return -1; | ||
} | ||
} | ||
await this.sortAndRunOrInitPlugins(list, "init"); | ||
} | ||
async sortAndRunOrInitPlugins(plugins, type) { | ||
const pluginMap = new Map(); | ||
for (let i = 0; i < plugins.length; i++) { | ||
pluginMap.set(plugins[i].pluginName, plugins[i]); | ||
} | ||
const visitPlugin = (plugin, resolved, unresolved) => { | ||
unresolved.push(plugin); | ||
const before = (type === "init" | ||
? plugin.initBeforePlugins | ||
: plugin.runBeforePlugins) ?? []; | ||
for (let i = 0; i < before.length; i++) { | ||
const beforePluginName = before[i]; | ||
const beforePlugin = pluginMap.get(beforePluginName); | ||
if (!beforePlugin) { | ||
throw new Error(`Plugin ${beforePluginName} required by ${plugin.pluginName} not found`); | ||
} | ||
if (!resolved.includes(beforePlugin)) { | ||
if (unresolved.includes(beforePlugin)) { | ||
continue; | ||
} | ||
visitPlugin(beforePlugin, resolved, unresolved); | ||
} | ||
else if ((type == "init" ? b.initBeforePlugins : b.runBeforePlugins).includes(a.pluginName)) { | ||
return 1; | ||
} | ||
resolved.push(plugin); | ||
unresolved.splice(unresolved.indexOf(plugin), 1); | ||
else if ((type == "init" ? a.initAfterPlugins : a.runAfterPlugins).includes(b.pluginName)) { | ||
return 1; | ||
} | ||
else if ((type == "init" ? b.initAfterPlugins : b.runAfterPlugins).includes(a.pluginName)) { | ||
return -1; | ||
} | ||
else { | ||
return 0; | ||
} | ||
}; | ||
const sortedPlugins = []; | ||
for (let i = 0; i < plugins.length; i++) { | ||
if (!sortedPlugins.includes(plugins[i])) { | ||
visitPlugin(plugins[i], sortedPlugins, []); | ||
} | ||
} | ||
for (let i = 0; i < sortedPlugins.length; i++) { | ||
this.log.debug("Plugin {pluginName}({ptype}) trigger {type} before {beforePlugins} and after {afterPlugins}", { | ||
ptype: sortedPlugins[i].type + | ||
(sortedPlugins[i].type === "client" | ||
? ` on ${sortedPlugins[i].srcPluginName}` | ||
: ""), | ||
pluginName: sortedPlugins[i].pluginName, | ||
} | ||
async sortAndRunOrInitPlugins(type) { | ||
const plugins = this.gatherListOfPlugins(type).sort(this.sortByInitDependencies(type)); | ||
this.log.debug("{key} plugins in order: {plugins}", { | ||
key: type, | ||
plugins: plugins.map((x) => x.pluginName).join(", "), | ||
}); | ||
for (const plugin of plugins) { | ||
this.log.debug("{type} plugin {pluginName}", { | ||
type, | ||
beforePlugins: this.getTextReportedDeps(type === "init" | ||
? sortedPlugins[i].initBeforePlugins | ||
: sortedPlugins[i].runBeforePlugins), | ||
afterPlugins: this.getTextReportedDeps(type === "init" | ||
? sortedPlugins[i].initAfterPlugins | ||
: sortedPlugins[i].runAfterPlugins), | ||
pluginName: plugin.pluginName, | ||
}); | ||
await (0, __1.SmartFunctionCallAsync)(sortedPlugins[i].reference, sortedPlugins[i].reference[type]); | ||
for (const client of plugin.clients) { | ||
this.log.debug(" -> {type} client {pluginName}", { | ||
type, | ||
pluginName: client.pluginName, | ||
}); | ||
await (0, __1.SmartFunctionCallAsync)(client.reference, client.reference[type]); | ||
} | ||
await (0, __1.SmartFunctionCallAsync)(plugin.reference, plugin.reference[type]); | ||
} | ||
@@ -278,44 +228,60 @@ } | ||
} | ||
async run() { | ||
this.log.info("Run all services"); | ||
gatherListOfPlugins(type) { | ||
const list = []; | ||
for (const service of this._activeServices) { | ||
this.log.debug("Mapping required plugins list for {plugin} [runBeforePlugins:{runBeforePlugins}][runAfterPlugins:{runAfterPlugins}]", { | ||
this.log.debug("Mapping required plugins list for {plugin} [{key}BeforePlugins: {beforePlugins}][{key}AfterPlugins:{afterPlugins}]", { | ||
key: type, | ||
plugin: service.pluginName, | ||
runBeforePlugins: this.getTextReportedDeps(service.runBeforePlugins), | ||
runAfterPlugins: this.getTextReportedDeps(service.runAfterPlugins), | ||
beforePlugins: this.getTextReportedDeps(type === "init" | ||
? service.initBeforePlugins | ||
: service.runBeforePlugins), | ||
afterPlugins: this.getTextReportedDeps(type === "init" ? service.initAfterPlugins : service.runAfterPlugins), | ||
}); | ||
list.push({ | ||
type: "service", | ||
const refPlugin = { | ||
srcPluginName: service.pluginName, | ||
pluginName: service.pluginName, | ||
initBeforePlugins: service.initBeforePlugins, | ||
initAfterPlugins: service.initAfterPlugins, | ||
runBeforePlugins: service.runBeforePlugins, | ||
runAfterPlugins: service.runAfterPlugins, | ||
initBeforePlugins: service.initBeforePlugins ?? [], | ||
initAfterPlugins: service.initAfterPlugins ?? [], | ||
runBeforePlugins: service.runBeforePlugins ?? [], | ||
runAfterPlugins: service.runAfterPlugins ?? [], | ||
reference: service, | ||
}); | ||
clients: [], | ||
}; | ||
for (const client of service._clients) { | ||
this.log.debug(" - {plugin} -> {client} [runBeforePlugins:{runBeforePlugins}][runAfterPlugins:{runAfterPlugins}]", { | ||
plugin: service.pluginName, | ||
this.log.debug(" -> {client} [{key}BeforePlugins:{beforePlugins}][{key}AfterPlugins:{afterPlugins}]", { | ||
key: type, | ||
client: client.pluginName, | ||
runBeforePlugins: this.getTextReportedDeps(client.runBeforePlugins), | ||
runAfterPlugins: this.getTextReportedDeps(client.runAfterPlugins), | ||
beforePlugins: this.getTextReportedDeps(type === "init" | ||
? client.initBeforePlugins | ||
: client.runBeforePlugins), | ||
afterPlugins: this.getTextReportedDeps(type === "init" ? client.initAfterPlugins : client.runAfterPlugins), | ||
}); | ||
list.push({ | ||
type: "client", | ||
refPlugin.clients.push({ | ||
srcPluginName: service.pluginName, | ||
pluginName: client.pluginName, | ||
initBeforePlugins: client.initBeforePlugins, | ||
initAfterPlugins: client.initAfterPlugins, | ||
runBeforePlugins: client.runBeforePlugins, | ||
runAfterPlugins: client.runAfterPlugins, | ||
initBeforePlugins: client.initBeforePlugins ?? [], | ||
initAfterPlugins: client.initAfterPlugins ?? [], | ||
runBeforePlugins: client.runBeforePlugins ?? [], | ||
runAfterPlugins: client.runAfterPlugins ?? [], | ||
reference: client, | ||
}); | ||
refPlugin.initBeforePlugins = refPlugin.initBeforePlugins.concat(client.initBeforePlugins ?? []); | ||
refPlugin.initAfterPlugins = refPlugin.initAfterPlugins.concat(client.initAfterPlugins ?? []); | ||
refPlugin.runBeforePlugins = refPlugin.runBeforePlugins.concat(client.runBeforePlugins ?? []); | ||
refPlugin.runAfterPlugins = refPlugin.runAfterPlugins.concat(client.runAfterPlugins ?? []); | ||
} | ||
list.push(refPlugin); | ||
} | ||
await this.sortAndRunOrInitPlugins(list, "run"); | ||
return list; | ||
} | ||
async init() { | ||
this.log.info("Init all services"); | ||
await this.sortAndRunOrInitPlugins("init"); | ||
} | ||
async run() { | ||
this.log.info("Run all services"); | ||
await this.sortAndRunOrInitPlugins("run"); | ||
} | ||
} | ||
exports.SBServices = SBServices; | ||
//# sourceMappingURL=services.js.map |
@@ -31,3 +31,3 @@ { | ||
"main": "lib/index.js", | ||
"version": "8.5.62", | ||
"version": "8.5.63", | ||
"bsb_project": true, | ||
@@ -34,0 +34,0 @@ "bsbInit": { |
Sorry, the diff of this file is not supported yet
398400
4963