core/src/plugin/plugin.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-11-28 13:34:34 +00:00
import { PluginConfiguration, Service } from '../types';
2020-11-21 15:41:08 +00:00
import { ScopedEventEmitter } from '../util/events';
export interface IPlugin {
manifest: IPluginManifest;
stream: ScopedEventEmitter;
config: PluginConfiguration;
2020-11-28 13:34:34 +00:00
service: Service | null;
2020-11-21 15:41:08 +00:00
}
2021-10-02 08:07:01 +00:00
/**
* Base class for all plugins
*/
2020-11-21 15:41:08 +00:00
export class Plugin implements IPlugin {
2020-11-28 13:34:34 +00:00
public service: Service | null = null;
2020-12-05 10:01:30 +00:00
protected on = this.addEventListener;
2020-11-28 13:34:34 +00:00
2020-11-21 15:41:08 +00:00
constructor(
public manifest: IPluginManifest,
public stream: ScopedEventEmitter,
public config: PluginConfiguration) {}
2021-10-02 08:07:01 +00:00
/**
* Called when plugin first starts.
* Please use this instead of the constructor.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
2020-11-21 15:41:08 +00:00
public initialize(): void {}
2021-03-23 09:11:41 +00:00
public get name(): string {
2020-11-21 15:41:08 +00:00
return this.manifest.name;
}
2021-03-23 09:11:41 +00:00
public get version(): string {
2020-11-21 15:41:08 +00:00
return this.manifest.version;
}
2020-11-28 13:34:34 +00:00
protected addEventListener(name: string, fn: any): void {
2020-11-21 15:41:08 +00:00
this.stream.on(this.name, name, fn);
}
2021-03-23 09:11:41 +00:00
protected emit(event: string, data: any): void {
this.stream.emit.call(this.stream, event, data);
2020-11-21 15:41:08 +00:00
}
2021-03-23 09:11:41 +00:00
protected emitTo(name: string, event: string, data: any): void {
this.stream.emitTo.call(this.stream, name, event, data);
2020-11-21 15:41:08 +00:00
}
}
export interface IPluginManifest {
repository: string;
fullPath: string;
main: string;
name: string;
2020-11-29 19:23:43 +00:00
tags?: string[];
2020-11-21 15:41:08 +00:00
version: string;
description: string;
dependencies: string[];
npmDependencies: string[];
}