fix: properties initialized in plugin.load are still empty in plugin.install (#2544)
This commit is contained in:
parent
c65507606a
commit
b23112fd2e
@ -399,4 +399,70 @@ describe('pm', () => {
|
|||||||
expect(record.installed).toBeTruthy();
|
expect(record.installed).toBeTruthy();
|
||||||
PluginManager.resolvePlugin = resolvePlugin;
|
PluginManager.resolvePlugin = resolvePlugin;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('life-cycle', async () => {
|
||||||
|
const resolvePlugin = PluginManager.resolvePlugin;
|
||||||
|
PluginManager.resolvePlugin = (pluginName) => {
|
||||||
|
return Plugin1;
|
||||||
|
};
|
||||||
|
const hooks = [];
|
||||||
|
const result = [];
|
||||||
|
class Plugin1 extends Plugin {
|
||||||
|
prop: any;
|
||||||
|
async afterAdd() {
|
||||||
|
hooks.push('afterAdd');
|
||||||
|
}
|
||||||
|
async beforeLoad() {
|
||||||
|
hooks.push('beforeLoad');
|
||||||
|
}
|
||||||
|
async load() {
|
||||||
|
this.prop = 'a';
|
||||||
|
hooks.push('load');
|
||||||
|
}
|
||||||
|
async beforeEnable() {
|
||||||
|
hooks.push('beforeEnable');
|
||||||
|
result.push(this.prop === 'a');
|
||||||
|
}
|
||||||
|
async install() {
|
||||||
|
hooks.push('install');
|
||||||
|
result.push(this.prop === 'a');
|
||||||
|
}
|
||||||
|
async afterEnable() {
|
||||||
|
hooks.push('afterEnable');
|
||||||
|
result.push(this.prop === 'a');
|
||||||
|
}
|
||||||
|
async beforeDisable() {
|
||||||
|
hooks.push('beforeDisable');
|
||||||
|
result.push(this.prop === 'a');
|
||||||
|
}
|
||||||
|
async afterDisable() {
|
||||||
|
hooks.push('afterDisable');
|
||||||
|
result.push(this.prop === 'a');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
app = mockServer();
|
||||||
|
await app.load();
|
||||||
|
await app.install();
|
||||||
|
await app.pm.repository.create({
|
||||||
|
values: {
|
||||||
|
name: 'Plugin1',
|
||||||
|
// enabled: true,
|
||||||
|
// installed: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await app.reload();
|
||||||
|
// console.log(hooks);
|
||||||
|
expect(app.pm.get('Plugin1')['prop']).toBeUndefined();
|
||||||
|
expect(result).toEqual([]);
|
||||||
|
await app.pm.enable('Plugin1');
|
||||||
|
expect(app.pm.get('Plugin1')['prop']).toBe('a');
|
||||||
|
// console.log(hooks.join('/'));
|
||||||
|
expect(result).toEqual([false, true, true]);
|
||||||
|
await app.pm.disable('Plugin1');
|
||||||
|
// console.log(hooks.join('/'));
|
||||||
|
expect(app.pm.get('Plugin1')['prop']).toBeUndefined();
|
||||||
|
expect(result).toEqual([false, true, true, true, false]);
|
||||||
|
// console.log(hooks.join('/'));
|
||||||
|
PluginManager.resolvePlugin = resolvePlugin;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -141,32 +141,32 @@ export class PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getPlugins() {
|
getPlugins() {
|
||||||
return this.pluginInstances;
|
return this.app.pm.pluginInstances;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAliases() {
|
getAliases() {
|
||||||
return this.pluginAliases.keys();
|
return this.app.pm.pluginAliases.keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
get(name: string | typeof Plugin) {
|
get(name: string | typeof Plugin) {
|
||||||
if (typeof name === 'string') {
|
if (typeof name === 'string') {
|
||||||
return this.pluginAliases.get(name);
|
return this.app.pm.pluginAliases.get(name);
|
||||||
}
|
}
|
||||||
return this.pluginInstances.get(name);
|
return this.app.pm.pluginInstances.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
has(name: string | typeof Plugin) {
|
has(name: string | typeof Plugin) {
|
||||||
if (typeof name === 'string') {
|
if (typeof name === 'string') {
|
||||||
return this.pluginAliases.has(name);
|
return this.app.pm.pluginAliases.has(name);
|
||||||
}
|
}
|
||||||
return this.pluginInstances.has(name);
|
return this.app.pm.pluginInstances.has(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
del(name: string | typeof Plugin) {
|
del(name: string | typeof Plugin) {
|
||||||
const instance = this.get(name);
|
const instance = this.get(name);
|
||||||
if (instance) {
|
if (instance) {
|
||||||
this.pluginAliases.delete(instance.name);
|
this.app.pm.pluginAliases.delete(instance.name);
|
||||||
this.pluginInstances.delete(instance.constructor as typeof Plugin);
|
this.app.pm.pluginInstances.delete(instance.constructor as typeof Plugin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user