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;
}
+8 -3
View File
@@ -3,6 +3,7 @@ import { System } from "../system.ts";
import { fullQueueName } from "../lib/mq_util.ts";
import { MQMessage } from "$sb/types.ts";
import { MessageQueue } from "../lib/mq.ts";
import { throttle } from "$sb/lib/async.ts";
type MQSubscription = {
queue: string;
@@ -24,14 +25,14 @@ export class MQHook implements Hook<MQHookT> {
this.system = system;
system.on({
plugLoaded: () => {
this.reloadQueues();
this.throttledReloadQueues();
},
plugUnloaded: () => {
this.reloadQueues();
this.throttledReloadQueues();
},
});
this.reloadQueues();
this.throttledReloadQueues();
}
stop() {
@@ -40,6 +41,10 @@ export class MQHook implements Hook<MQHookT> {
this.subscriptions = [];
}
throttledReloadQueues = throttle(() => {
this.reloadQueues();
}, 1000);
reloadQueues() {
this.stop();
for (const plug of this.system.loadedPlugs.values()) {