2022-10-10 12:50:21 +00:00
|
|
|
import type { Hook, Manifest } from "../types.ts";
|
|
|
|
import { System } from "../system.ts";
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-03-28 06:51:24 +00:00
|
|
|
// System events:
|
|
|
|
// - plug:load (plugName: string)
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
export type EventHookT = {
|
2022-03-27 09:26:13 +00:00
|
|
|
events?: string[];
|
2022-03-25 11:03:06 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
export class EventHook implements Hook<EventHookT> {
|
|
|
|
private system?: System<EventHookT>;
|
2023-06-13 18:47:05 +00:00
|
|
|
public localListeners: Map<string, ((...args: any[]) => any)[]> = new Map();
|
2022-04-26 17:04:36 +00:00
|
|
|
|
2023-06-13 18:47:05 +00:00
|
|
|
addLocalListener(eventName: string, callback: (...args: any[]) => any) {
|
2022-04-26 17:04:36 +00:00
|
|
|
if (!this.localListeners.has(eventName)) {
|
|
|
|
this.localListeners.set(eventName, []);
|
|
|
|
}
|
|
|
|
this.localListeners.get(eventName)!.push(callback);
|
|
|
|
}
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-07-04 13:07:27 +00:00
|
|
|
// Pull all events listened to
|
|
|
|
listEvents(): string[] {
|
|
|
|
if (!this.system) {
|
|
|
|
throw new Error("Event hook is not initialized");
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
const eventNames = new Set<string>();
|
2022-07-04 13:07:27 +00:00
|
|
|
for (const plug of this.system.loadedPlugs.values()) {
|
2022-10-10 12:50:21 +00:00
|
|
|
for (const functionDef of Object.values(plug.manifest!.functions)) {
|
2022-07-04 13:07:27 +00:00
|
|
|
if (functionDef.events) {
|
2022-10-10 12:50:21 +00:00
|
|
|
for (const eventName of functionDef.events) {
|
2022-07-04 13:07:27 +00:00
|
|
|
eventNames.add(eventName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
for (const eventName of this.localListeners.keys()) {
|
2022-07-04 13:07:27 +00:00
|
|
|
eventNames.add(eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...eventNames];
|
|
|
|
}
|
|
|
|
|
2023-06-13 18:47:05 +00:00
|
|
|
async dispatchEvent(eventName: string, ...args: any[]): Promise<any[]> {
|
2022-03-25 11:03:06 +00:00
|
|
|
if (!this.system) {
|
2022-03-29 09:21:32 +00:00
|
|
|
throw new Error("Event hook is not initialized");
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
const responses: any[] = [];
|
2022-03-25 11:03:06 +00:00
|
|
|
for (const plug of this.system.loadedPlugs.values()) {
|
2023-06-13 18:47:05 +00:00
|
|
|
const manifest = plug.manifest;
|
2022-10-10 12:50:21 +00:00
|
|
|
for (
|
|
|
|
const [name, functionDef] of Object.entries(
|
2023-05-23 18:53:53 +00:00
|
|
|
manifest!.functions,
|
2022-10-10 12:50:21 +00:00
|
|
|
)
|
|
|
|
) {
|
2023-10-03 12:16:33 +00:00
|
|
|
if (functionDef.events) {
|
|
|
|
for (const event of functionDef.events) {
|
|
|
|
if (
|
|
|
|
event === eventName || eventNameToRegex(event).test(eventName)
|
|
|
|
) {
|
|
|
|
// Only dispatch functions that can run in this environment
|
|
|
|
if (await plug.canInvoke(name)) {
|
|
|
|
const result = await plug.invoke(name, args);
|
|
|
|
if (result !== undefined) {
|
|
|
|
responses.push(result);
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
2022-03-28 06:51:24 +00:00
|
|
|
}
|
2022-03-27 09:26:13 +00:00
|
|
|
}
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
const localListeners = this.localListeners.get(eventName);
|
2022-04-26 17:04:36 +00:00
|
|
|
if (localListeners) {
|
2022-10-10 12:50:21 +00:00
|
|
|
for (const localListener of localListeners) {
|
2023-06-13 18:47:05 +00:00
|
|
|
const result = await Promise.resolve(localListener(...args));
|
2022-04-26 17:04:36 +00:00
|
|
|
if (result) {
|
|
|
|
responses.push(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
return responses;
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
apply(system: System<EventHookT>): void {
|
2022-03-25 11:03:06 +00:00
|
|
|
this.system = system;
|
2022-03-28 06:51:24 +00:00
|
|
|
this.system.on({
|
2023-05-23 18:53:53 +00:00
|
|
|
plugLoaded: async (plug) => {
|
|
|
|
await this.dispatchEvent("plug:load", plug.name);
|
2022-03-28 06:51:24 +00:00
|
|
|
},
|
|
|
|
});
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
validateManifest(manifest: Manifest<EventHookT>): string[] {
|
2022-10-10 12:50:21 +00:00
|
|
|
const errors = [];
|
|
|
|
for (
|
|
|
|
const [_, functionDef] of Object.entries(
|
|
|
|
manifest.functions || {},
|
|
|
|
)
|
|
|
|
) {
|
2022-03-27 09:26:13 +00:00
|
|
|
if (functionDef.events && !Array.isArray(functionDef.events)) {
|
|
|
|
errors.push("'events' key must be an array of strings");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
function eventNameToRegex(eventName: string): RegExp {
|
|
|
|
return new RegExp(
|
|
|
|
`^${eventName.replace(/\*/g, ".*").replace(/\//g, "\\/")}$`,
|
|
|
|
);
|
|
|
|
}
|