core/src/plugin/decorators/dependency.ts

60 lines
1.6 KiB
TypeScript

/**
* Fire the following method automatically when a dependency is (re)loaded.
* @param dep Name of the dependency
*/
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') {
return;
}
const nameof = plugin.manifest.name;
if (nameof !== dep) {
return;
}
originalMethod.call(self, plugin);
});
};
// Set the function to be autoexecuted when the plugin is initialized.
descriptor.value.prototype.__autoexec = 1;
return descriptor;
};
}
/**
* Fire the following method automatically when a dependency is unloaded.
* @param dep Name of the dependency
*/
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) => {
let nameof = plugin;
if (typeof plugin !== 'string') {
nameof = plugin.manifest.name;
}
if (nameof !== dep) {
return;
}
originalMethod.call(self, plugin);
});
};
descriptor.value.prototype.__autoexec = 1;
return descriptor;
};
}