Skip to content

Commit

Permalink
feat: plugin helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
0t4u committed Nov 12, 2024
1 parent 8e8ed14 commit 97499ad
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { EventEmitter } from 'node:events';

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type, @typescript-eslint/no-empty-object-type
interface AnyClass<InstanceType extends {} = {}> extends Function {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new(...args: any[]): InstanceType;
prototype: InstanceType;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyFunction = (...args: any) => any;
export type Extension = Record<string, unknown>;
export type Plugin<T> = (instance: T) => Extension;

export const definePlugin = <T extends AnyClass, B extends AnyFunction = Plugin<T>>(plugin: B): B => plugin;

// https://stackoverflow.com/a/58603027
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function withPlugins<TBase extends new (...args: any[]) => any>(Base: TBase) {
export function withPlugins<TBase extends AnyClass>(Base: TBase) {
return class ClassWithPlugins extends Base {
static plugins: Plugin<ClassWithPlugins>[];
static plugin<T extends Plugin<ClassWithPlugins>>(plugin: T) {
Expand Down

0 comments on commit 97499ad

Please sign in to comment.