core/src/plugin/decorators/dependency.ts

51 lines
1.4 KiB
TypeScript

export function DependencyLoad(dep: string): (...args: any[]) => void {
return (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
) => {
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]): void {
const self = this as any;
self.stream.on(self.name, 'pluginLoaded', (plugin: any) => {
if (typeof plugin !== 'string') {
plugin = plugin.metadata.name;
}
if (plugin !== dep) {
return;
}
originalMethod.apply(self, plugin);
});
};
// Set the function to be autoexecuted when the plugin is initialized.
descriptor.value.prototype.__autoexec = 1;
return descriptor;
};
}
export function DependencyUnload(dep: string): (...args: any[]) => void {
return (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
) => {
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]): void {
const self = this as any;
self.stream.on(self.name, 'pluginUnloaded', (plugin: any) => {
if (typeof plugin !== 'string') {
plugin = plugin.metadata.name;
}
if (plugin !== dep) {
return;
}
originalMethod.apply(self, plugin);
});
};
// Set the function to be autoexecuted when the plugin is initialized.
descriptor.value.prototype.__autoexec = 1;
return descriptor;
};
}