Skip to content

Commit

Permalink
feat(plugins): Make plugin.initialize() async
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen authored and mergify[bot] committed Apr 21, 2020
1 parent 39fbad2 commit 828595e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
17 changes: 17 additions & 0 deletions app/scripts/modules/core/src/plugins/deck.plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import { HelpContentsRegistry } from '../help';
import { Registry } from '../registry';

describe('deck plugin registerPluginExtensions', () => {
it('returns a promise', async () => {
const promiseLike = registerPluginExtensions({});
expect(promiseLike).toBeDefined();
expect(promiseLike.then).toBeDefined();
expect(typeof promiseLike.then).toBe('function');
});

it('returns a promise that resolves to the return value of initialize', async () => {
const result = await registerPluginExtensions({ initialize: () => 'anything' });
expect(result).toBe('anything');
});

it('returns a promise that unwraps a promise returned by initialize', async () => {
const result = await registerPluginExtensions({ initialize: () => Promise.resolve('anything') });
expect(result).toBe('anything');
});

it('should register stages', async () => {
const registerSpy = spyOn(Registry.pipeline, 'registerStage');
const stage = { key: 'test' };
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/modules/core/src/plugins/deck.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export interface IDeckPlugin {
}

/** Given a plugin, registers the plugin's extensions */
export function registerPluginExtensions(plugin: IDeckPlugin) {
export function registerPluginExtensions(plugin: IDeckPlugin): PromiseLike<any> {
// Register the plugin's extensions with deck.
plugin.stages?.forEach(stage => Registry.pipeline.registerStage(stage));
plugin.preconfiguredJobStages?.forEach(stage => Registry.pipeline.registerPreconfiguredJobStage(stage));
toPairs(plugin.help ?? {}).forEach(([key, value]) => HelpContentsRegistry.register(key, value));
plugin.search?.forEach(search => searchResultTypeRegistry.register(search));

// Run code that currently does not have an extension point
plugin.initialize?.();
// Run arbitrary initialization code
return Promise.resolve(plugin.initialize?.());
}
4 changes: 1 addition & 3 deletions app/scripts/modules/core/src/plugins/plugin.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ export class PluginRegistry {
);
}

registerPluginExtensions(module.plugin as IDeckPlugin);

return module;
return registerPluginExtensions(module.plugin as IDeckPlugin).then(() => module);
} catch (error) {
console.error(`Failed to load plugin code from ${pluginUrl}`);
throw error;
Expand Down

0 comments on commit 828595e

Please sign in to comment.