core/src/plugin/decorators/dependency.ts

62 lines
1.7 KiB
TypeScript

import { Auto } from './auto';
/**
* Fire the following method automatically when a dependency is (re)loaded.
* @param dependency Name of the dependency
*/
export function DependencyLoad(dependency: string) {
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') {
return;
}
const nameof = plugin.manifest.name;
if (nameof !== dependency) {
return;
}
originalMethod.call(self, plugin);
});
};
// Set the function to be autoexecuted when the plugin is initialized.
Auto()(target, propertyKey, descriptor);
return descriptor;
};
}
/**
* Fire the following method automatically when a dependency is unloaded.
* @param dependency Name of the dependency
*/
export function DependencyUnload(dependency: string) {
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) => {
let nameof = plugin;
if (typeof plugin !== 'string') {
nameof = plugin.manifest.name;
}
if (nameof !== dependency) {
return;
}
originalMethod.call(self, plugin);
});
};
// Set the function to be autoexecuted when the plugin is initialized.
Auto()(target, propertyKey, descriptor);
return descriptor;
};
}