# Plugin

An abstract base class to define a plugin.

# Usage

# Define a plugin

my-plugin/src/myPlugin.ts
import { Plugin, type FireflyRuntime } from "@squide/firefly";

export class MyPlugin extends Plugin {
    #runtime: FireflyRuntime;

    constructor() {
        super(MyPlugin.name);
    }

    // An optional method that can be implemented to get an hold on the current runtime instance.
    _setRuntime(runtime: FireflyRuntime) {
        this.#runtime = runtime;
    }
}

# Register a plugin

import { FireflyRuntime } from "@squide/firefly";
import { MyPlugin } from "@sample/my-plugin";

const runtime = new FireflyRuntime({
    plugins: [new MyPlugin()]
});

# Retrieve a plugin from a runtime instance

import { MyPlugin } from "@sample/my-plugin";

const myPlugin = runtime.getPlugin(MyPlugin.name) as MyPlugin;

# Retrieve a plugin with a custom function

We recommend pairing a plugin definition with a custom function to retrieve the plugin from a runtime instance.

my-plugin/src/myPlugin.ts
import { Plugin, type FireflyRuntime } from "@squide/firefly";

export class MyPlugin extends FireflyRuntime {
    constructor() {
        super(MyPlugin.name);
    }
}

export function getMyPlugin(runtime: FireflyRuntime) {
    return runtime.getPlugin(MyPlugin.name) as MyPlugin;
}
import { getMyPlugin } from "@sample/my-plugin";

const myPlugin = getMyPlugin(runtime);

Retrieving a plugin with a custom function doesn't require the consumer to remember the plugin name, and has the upside of inferring the typings.