1
0
silverbullet/packages/plugos/hooks/event.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-04-05 15:02:17 +00:00
import { Hook, Manifest } from "../types";
import { System } from "../system";
import { safeRun } from "../util";
2022-04-26 17:04:36 +00:00
import { EventEmitter } from "events";
2022-03-25 11:03:06 +00:00
2022-03-28 06:51:24 +00:00
// System events:
// - plug:load (plugName: string)
export type EventHookT = {
2022-03-27 09:26:13 +00:00
events?: string[];
2022-03-25 11:03:06 +00:00
};
export class EventHook implements Hook<EventHookT> {
private system?: System<EventHookT>;
2022-04-26 17:04:36 +00:00
public localListeners: Map<string, ((data: any) => any)[]> = new Map();
addLocalListener(eventName: string, callback: (data: any) => any) {
if (!this.localListeners.has(eventName)) {
this.localListeners.set(eventName, []);
}
this.localListeners.get(eventName)!.push(callback);
}
2022-03-25 11:03:06 +00:00
// Pull all events listened to
listEvents(): string[] {
if (!this.system) {
throw new Error("Event hook is not initialized");
}
let eventNames = new Set<string>();
for (const plug of this.system.loadedPlugs.values()) {
for (const [name, functionDef] of Object.entries(
plug.manifest!.functions
)) {
if (functionDef.events) {
for (let eventName of functionDef.events) {
eventNames.add(eventName);
}
}
}
}
for (let eventName of this.localListeners.keys()) {
eventNames.add(eventName);
}
return [...eventNames];
}
async dispatchEvent(eventName: string, data?: any): Promise<any[]> {
2022-03-25 11:03:06 +00:00
if (!this.system) {
throw new Error("Event hook is not initialized");
2022-03-25 11:03:06 +00:00
}
let responses: any[] = [];
2022-03-25 11:03:06 +00:00
for (const plug of this.system.loadedPlugs.values()) {
2022-03-27 09:26:13 +00:00
for (const [name, functionDef] of Object.entries(
plug.manifest!.functions
)) {
if (functionDef.events && functionDef.events.includes(eventName)) {
2022-03-28 06:51:24 +00:00
// Only dispatch functions that can run in this environment
if (plug.canInvoke(name)) {
let result = await plug.invoke(name, [data]);
if (result !== undefined) {
responses.push(result);
}
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-04-26 17:04:36 +00:00
let localListeners = this.localListeners.get(eventName);
if (localListeners) {
for (let localListener of localListeners) {
let result = await Promise.resolve(localListener(data));
if (result) {
responses.push(result);
}
}
}
return responses;
2022-03-25 11:03:06 +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({
2022-04-26 17:04:36 +00:00
plugLoaded: (plug) => {
2022-03-29 10:13:46 +00:00
safeRun(async () => {
2022-04-26 17:04:36 +00:00
await this.dispatchEvent("plug:load", plug.name);
2022-03-29 10:13:46 +00:00
});
2022-03-28 06:51:24 +00:00
},
});
2022-03-25 11:03:06 +00:00
}
validateManifest(manifest: Manifest<EventHookT>): string[] {
2022-03-27 09:26:13 +00:00
let errors = [];
2022-05-13 12:36:26 +00:00
for (const [name, 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
}
}