2024-03-16 22:29:24 +08:00
|
|
|
import type { Manifest } from "$lib/plugos/types.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { System } from "$lib/plugos/system.ts";
|
|
|
|
import type { ScriptEnvironment } from "$common/space_script.ts";
|
|
|
|
import type { EventHookI } from "$lib/plugos/eventhook.ts";
|
|
|
|
import type { EventHookT } from "$lib/manifest.ts";
|
2022-03-25 19:03:06 +08:00
|
|
|
|
2022-03-28 14:51:24 +08:00
|
|
|
// System events:
|
|
|
|
// - plug:load (plugName: string)
|
|
|
|
|
2024-03-16 22:29:24 +08:00
|
|
|
export class EventHook implements EventHookI {
|
2022-03-29 17:21:32 +08:00
|
|
|
private system?: System<EventHookT>;
|
2024-02-28 03:05:12 +08:00
|
|
|
private localListeners: Map<string, ((...args: any[]) => any)[]> = new Map();
|
|
|
|
public scriptEnvironment?: ScriptEnvironment;
|
2022-04-27 01:04:36 +08:00
|
|
|
|
2023-06-14 02:47:05 +08:00
|
|
|
addLocalListener(eventName: string, callback: (...args: any[]) => any) {
|
2022-04-27 01:04:36 +08:00
|
|
|
if (!this.localListeners.has(eventName)) {
|
|
|
|
this.localListeners.set(eventName, []);
|
|
|
|
}
|
|
|
|
this.localListeners.get(eventName)!.push(callback);
|
|
|
|
}
|
2022-03-25 19:03:06 +08:00
|
|
|
|
2022-07-04 21:07:27 +08:00
|
|
|
// Pull all events listened to
|
|
|
|
listEvents(): string[] {
|
|
|
|
if (!this.system) {
|
|
|
|
throw new Error("Event hook is not initialized");
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
const eventNames = new Set<string>();
|
2022-07-04 21:07:27 +08:00
|
|
|
for (const plug of this.system.loadedPlugs.values()) {
|
2022-10-10 20:50:21 +08:00
|
|
|
for (const functionDef of Object.values(plug.manifest!.functions)) {
|
2022-07-04 21:07:27 +08:00
|
|
|
if (functionDef.events) {
|
2022-10-10 20:50:21 +08:00
|
|
|
for (const eventName of functionDef.events) {
|
2022-07-04 21:07:27 +08:00
|
|
|
eventNames.add(eventName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
for (const eventName of this.localListeners.keys()) {
|
2022-07-04 21:07:27 +08:00
|
|
|
eventNames.add(eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...eventNames];
|
|
|
|
}
|
|
|
|
|
2023-06-14 02:47:05 +08:00
|
|
|
async dispatchEvent(eventName: string, ...args: any[]): Promise<any[]> {
|
2022-03-25 19:03:06 +08:00
|
|
|
if (!this.system) {
|
2022-03-29 17:21:32 +08:00
|
|
|
throw new Error("Event hook is not initialized");
|
2022-03-25 19:03:06 +08:00
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
const responses: any[] = [];
|
2024-01-14 00:30:15 +08:00
|
|
|
const promises: Promise<void>[] = [];
|
2022-03-25 19:03:06 +08:00
|
|
|
for (const plug of this.system.loadedPlugs.values()) {
|
2023-06-14 02:47:05 +08:00
|
|
|
const manifest = plug.manifest;
|
2022-10-10 20:50:21 +08:00
|
|
|
for (
|
|
|
|
const [name, functionDef] of Object.entries(
|
2023-05-24 02:53:53 +08:00
|
|
|
manifest!.functions,
|
2022-10-10 20:50:21 +08:00
|
|
|
)
|
|
|
|
) {
|
2023-10-03 20:16:33 +08: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)) {
|
2024-01-14 00:30:15 +08:00
|
|
|
// 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(
|
2024-01-15 23:43:12 +08:00
|
|
|
`Error dispatching event ${eventName} to ${plug.name}.${name}: ${e.message}`,
|
2024-01-14 00:30:15 +08:00
|
|
|
);
|
2024-03-05 03:22:09 +08:00
|
|
|
throw e;
|
2023-10-10 02:39:03 +08:00
|
|
|
}
|
2024-01-14 00:30:15 +08:00
|
|
|
})());
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|
2022-04-19 22:54:47 +08:00
|
|
|
}
|
2022-03-28 14:51:24 +08:00
|
|
|
}
|
2022-03-27 17:26:13 +08:00
|
|
|
}
|
2022-03-25 19:03:06 +08:00
|
|
|
}
|
|
|
|
}
|
2024-02-28 03:05:12 +08:00
|
|
|
|
|
|
|
// Local listeners
|
2022-10-10 20:50:21 +08:00
|
|
|
const localListeners = this.localListeners.get(eventName);
|
2022-04-27 01:04:36 +08:00
|
|
|
if (localListeners) {
|
2022-10-10 20:50:21 +08:00
|
|
|
for (const localListener of localListeners) {
|
2024-01-14 00:30:15 +08:00
|
|
|
// Queue the promise
|
|
|
|
promises.push((async () => {
|
|
|
|
const result = await Promise.resolve(localListener(...args));
|
|
|
|
if (result) {
|
|
|
|
responses.push(result);
|
|
|
|
}
|
|
|
|
})());
|
2022-04-27 01:04:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 03:05:12 +08:00
|
|
|
// Space script listeners
|
|
|
|
if (this.scriptEnvironment) {
|
|
|
|
for (
|
|
|
|
const [name, listeners] of Object.entries(
|
|
|
|
this.scriptEnvironment.eventHandlers,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (eventNameToRegex(name).test(eventName)) {
|
|
|
|
for (const listener of listeners) {
|
|
|
|
promises.push((async () => {
|
|
|
|
const result = await Promise.resolve(
|
|
|
|
listener({
|
|
|
|
name: eventName,
|
|
|
|
// Most events have a single argument, so let's optimize for that, otherwise pass all arguments as an array
|
|
|
|
data: args.length === 1 ? args[0] : args,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
if (result) {
|
|
|
|
responses.push(result);
|
|
|
|
}
|
|
|
|
})());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 00:30:15 +08:00
|
|
|
// Wait for all promises to resolve
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2022-04-19 22:54:47 +08:00
|
|
|
return responses;
|
2022-03-25 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 17:21:32 +08:00
|
|
|
apply(system: System<EventHookT>): void {
|
2022-03-25 19:03:06 +08:00
|
|
|
this.system = system;
|
2022-03-28 14:51:24 +08:00
|
|
|
this.system.on({
|
2023-05-24 02:53:53 +08:00
|
|
|
plugLoaded: async (plug) => {
|
|
|
|
await this.dispatchEvent("plug:load", plug.name);
|
2022-03-28 14:51:24 +08:00
|
|
|
},
|
|
|
|
});
|
2022-03-25 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 17:21:32 +08:00
|
|
|
validateManifest(manifest: Manifest<EventHookT>): string[] {
|
2022-10-10 20:50:21 +08:00
|
|
|
const errors = [];
|
|
|
|
for (
|
|
|
|
const [_, functionDef] of Object.entries(
|
|
|
|
manifest.functions || {},
|
|
|
|
)
|
|
|
|
) {
|
2022-03-27 17:26:13 +08:00
|
|
|
if (functionDef.events && !Array.isArray(functionDef.events)) {
|
|
|
|
errors.push("'events' key must be an array of strings");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
2022-03-25 19:03:06 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 20:16:33 +08:00
|
|
|
|
|
|
|
function eventNameToRegex(eventName: string): RegExp {
|
|
|
|
return new RegExp(
|
|
|
|
`^${eventName.replace(/\*/g, ".*").replace(/\//g, "\\/")}$`,
|
|
|
|
);
|
|
|
|
}
|