fix: properties initialized in plugin.load are still empty in plugin.install (#2544)

This commit is contained in:
chenos 2023-08-28 11:35:50 +08:00 committed by GitHub
parent c65507606a
commit b23112fd2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 8 deletions

View File

@ -399,4 +399,70 @@ describe('pm', () => {
expect(record.installed).toBeTruthy();
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;
});
});

View File

@ -141,32 +141,32 @@ export class PluginManager {
}
getPlugins() {
return this.pluginInstances;
return this.app.pm.pluginInstances;
}
getAliases() {
return this.pluginAliases.keys();
return this.app.pm.pluginAliases.keys();
}
get(name: string | typeof Plugin) {
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) {
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) {
const instance = this.get(name);
if (instance) {
this.pluginAliases.delete(instance.name);
this.pluginInstances.delete(instance.constructor as typeof Plugin);
this.app.pm.pluginAliases.delete(instance.name);
this.app.pm.pluginInstances.delete(instance.constructor as typeof Plugin);
}
}