From 2a7f9d6cd27c498d75b9079b2602e555cdfb7136 Mon Sep 17 00:00:00 2001 From: Alex Kaminskii Date: Fri, 21 Apr 2023 18:45:01 +0200 Subject: [PATCH] whatewer tracker-plpugin ideas --- tracker/tracker/src/main/plugin.ts | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tracker/tracker/src/main/plugin.ts diff --git a/tracker/tracker/src/main/plugin.ts b/tracker/tracker/src/main/plugin.ts new file mode 100644 index 000000000..cb4080ce0 --- /dev/null +++ b/tracker/tracker/src/main/plugin.ts @@ -0,0 +1,56 @@ +import type Message from './app/messages.gen.js' +import type App from './app/index.js' +//import { PluginPayload } from './app/messages.gen.js' + +// TODO: sendState(name, action, state) for state plugins; + +export interface PluginDescription { + name: string + version: string + requiredTrackerVersion: string + onStart: () => void + onStop: () => void + onNode: (node: Node, id: number) => void + onContext: (context: typeof globalThis) => void +} + +type PluginWrapper = (app: AppForPlugins) => Partial + +interface AppForPlugins { + sendMessage(m: Message): void + send(name: string, payload: Object): void + // ... +} + +export function applyPlugin(app: App, pluginWrapper: PluginWrapper) { + function send(name: string, second?: Object) { + const paload = app.safe(() => JSON.stringify(second))() || '' + // app.send(PluginPayload(name, payload)) // send PluginPayload message + return + } + function sendMessage(msg: Message) { + app.send(msg) + } + const plugin = pluginWrapper({ + send, + sendMessage, + //... + }) + + if (plugin.onStart) { + app.attachStartCallback(plugin.onStart) + } + if (plugin.onStop) { + app.attachStopCallback(plugin.onStop) + } + if (plugin.onNode) { + app.nodes.attachNodeCallback((node) => { + const id = app.nodes.getID(node) + if (id !== undefined) { + plugin.onNode(node, id) + } + }) + } + + return plugin +}