Backporting a bunch of optimizations from db-only branch

This commit is contained in:
Zef Hemel
2024-01-13 17:30:15 +01:00
parent 509ece91f0
commit bf1eb03129
24 changed files with 264 additions and 104 deletions
+23 -13
View File
@@ -46,6 +46,7 @@ export class EventHook implements Hook<EventHookT> {
throw new Error("Event hook is not initialized");
}
const responses: any[] = [];
const promises: Promise<void>[] = [];
for (const plug of this.system.loadedPlugs.values()) {
const manifest = plug.manifest;
for (
@@ -60,16 +61,19 @@ export class EventHook implements Hook<EventHookT> {
) {
// Only dispatch functions that can run in this environment
if (await plug.canInvoke(name)) {
try {
const result = await plug.invoke(name, args);
if (result !== undefined) {
responses.push(result);
// Queue the promise
promises.push((async () => {
try {
const result = await plug.invoke(name, args);
if (result !== undefined) {
responses.push(result);
}
} catch (e: any) {
console.error(
`Error dispatching event ${eventName} to plug ${plug.name}: ${e.message}`,
);
}
} catch (e: any) {
console.error(
`Error dispatching event ${eventName} to plug ${plug.name}: ${e.message}`,
);
}
})());
}
}
}
@@ -79,13 +83,19 @@ export class EventHook implements Hook<EventHookT> {
const localListeners = this.localListeners.get(eventName);
if (localListeners) {
for (const localListener of localListeners) {
const result = await Promise.resolve(localListener(...args));
if (result) {
responses.push(result);
}
// Queue the promise
promises.push((async () => {
const result = await Promise.resolve(localListener(...args));
if (result) {
responses.push(result);
}
})());
}
}
// Wait for all promises to resolve
await Promise.all(promises);
return responses;
}