-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
plugin.ts
173 lines (156 loc) · 5.73 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Subject } from 'rxjs';
import { first } from 'rxjs/operators';
import { isPromise } from '@kbn/std';
import { DiscoveredPlugin, PluginOpaqueId } from '../../server';
import { PluginInitializerContext } from './plugin_context';
import { read } from './plugin_reader';
import { CoreStart, CoreSetup } from '..';
/**
* The interface that should be returned by a `PluginInitializer`.
*
* @public
*/
export interface Plugin<
TSetup = void,
TStart = void,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
setup(core: CoreSetup<TPluginsStart, TStart>, plugins: TPluginsSetup): TSetup;
start(core: CoreStart, plugins: TPluginsStart): TStart;
stop?(): void;
}
/**
* A plugin with asynchronous lifecycle methods.
*
* @deprecated Asynchronous lifecycles are deprecated, and should be migrated to sync {@link Plugin | plugin}
* @public
*/
export interface AsyncPlugin<
TSetup = void,
TStart = void,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
setup(core: CoreSetup<TPluginsStart, TStart>, plugins: TPluginsSetup): TSetup | Promise<TSetup>;
start(core: CoreStart, plugins: TPluginsStart): TStart | Promise<TStart>;
stop?(): void;
}
/**
* The `plugin` export at the root of a plugin's `public` directory should conform
* to this interface.
*
* @public
*/
export type PluginInitializer<
TSetup,
TStart,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> = (
core: PluginInitializerContext
) =>
| Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>
| AsyncPlugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
/**
* Lightweight wrapper around discovered plugin that is responsible for instantiating
* plugin and dispatching proper context and dependencies into plugin's lifecycle hooks.
*
* @internal
*/
export class PluginWrapper<
TSetup = unknown,
TStart = unknown,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
public readonly name: DiscoveredPlugin['id'];
public readonly configPath: DiscoveredPlugin['configPath'];
public readonly requiredPlugins: DiscoveredPlugin['requiredPlugins'];
public readonly optionalPlugins: DiscoveredPlugin['optionalPlugins'];
private instance?:
| Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>
| AsyncPlugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
private readonly startDependencies$ = new Subject<[CoreStart, TPluginsStart, TStart]>();
public readonly startDependencies = this.startDependencies$.pipe(first()).toPromise();
constructor(
public readonly discoveredPlugin: DiscoveredPlugin,
public readonly opaqueId: PluginOpaqueId,
private readonly initializerContext: PluginInitializerContext
) {
this.name = discoveredPlugin.id;
this.configPath = discoveredPlugin.configPath;
this.requiredPlugins = discoveredPlugin.requiredPlugins;
this.optionalPlugins = discoveredPlugin.optionalPlugins;
}
/**
* Instantiates plugin and calls `setup` function exposed by the plugin initializer.
* @param setupContext Context that consists of various core services tailored specifically
* for the `setup` lifecycle event.
* @param plugins The dictionary where the key is the dependency name and the value
* is the contract returned by the dependency's `setup` function.
*/
public setup(
setupContext: CoreSetup<TPluginsStart, TStart>,
plugins: TPluginsSetup
): TSetup | Promise<TSetup> {
this.instance = this.createPluginInstance();
return this.instance.setup(setupContext, plugins);
}
/**
* Calls `setup` function exposed by the initialized plugin.
* @param startContext Context that consists of various core services tailored specifically
* for the `start` lifecycle event.
* @param plugins The dictionary where the key is the dependency name and the value
* is the contract returned by the dependency's `start` function.
*/
public start(startContext: CoreStart, plugins: TPluginsStart) {
if (this.instance === undefined) {
throw new Error(`Plugin "${this.name}" can't be started since it isn't set up.`);
}
const startContract = this.instance.start(startContext, plugins);
if (isPromise(startContract)) {
return startContract.then((resolvedContract) => {
this.startDependencies$.next([startContext, plugins, resolvedContract]);
return resolvedContract;
});
} else {
this.startDependencies$.next([startContext, plugins, startContract]);
return startContract;
}
}
/**
* Calls optional `stop` function exposed by the plugin initializer.
*/
public stop() {
if (this.instance === undefined) {
throw new Error(`Plugin "${this.name}" can't be stopped since it isn't set up.`);
}
if (typeof this.instance.stop === 'function') {
this.instance.stop();
}
this.instance = undefined;
}
private createPluginInstance() {
const initializer = read(this.name) as PluginInitializer<
TSetup,
TStart,
TPluginsSetup,
TPluginsStart
>;
const instance = initializer(this.initializerContext);
if (typeof instance.setup !== 'function') {
throw new Error(`Instance of plugin "${this.name}" does not define "setup" function.`);
} else if (typeof instance.start !== 'function') {
throw new Error(`Instance of plugin "${this.name}" does not define "start" function.`);
}
return instance;
}
}