1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const lifecycle_1 = require("@stoplight/lifecycle");
|
4 | exports.isLifecycleGraphitePlugin = (plugin) => 'activate' in plugin && 'deactivate' in plugin;
|
5 | class AbstractLifecyclePlugin extends lifecycle_1.Activatable {
|
6 | constructor() {
|
7 | super(...arguments);
|
8 | this.activeDisposables = new lifecycle_1.DisposableCollection();
|
9 | }
|
10 | get graphite() {
|
11 | if (!this._graphite) {
|
12 | throw new Error('Cannot access graphite before plugin is initialized');
|
13 | }
|
14 | return this._graphite;
|
15 | }
|
16 | async init(graphite) {
|
17 | if (this._graphite) {
|
18 | throw new Error('Cannot initialize plugin twice');
|
19 | }
|
20 | this._graphite = graphite;
|
21 | await this.activate();
|
22 | }
|
23 | async dispose() {
|
24 | if (!this.activeDisposables.disposed) {
|
25 | this.activeDisposables.dispose();
|
26 | }
|
27 | if (this.state !== 'isDeactivating') {
|
28 | await this.deactivate();
|
29 | }
|
30 | }
|
31 | async doActivate() {
|
32 | if (this.tasks) {
|
33 | this.registerSchedulerTasks(this.tasks);
|
34 | }
|
35 | if (this.events) {
|
36 | this.registerGraphiteEvents(this.events);
|
37 | }
|
38 | if (this.specProvider) {
|
39 | this.activeDisposables.push(this.graphite.specRegistry.register(this.specProvider));
|
40 | }
|
41 | }
|
42 | async doDeactivate() {
|
43 | this.activeDisposables.dispose();
|
44 | }
|
45 | registerSchedulerTasks(tasks) {
|
46 | for (const task of tasks) {
|
47 | this.activeDisposables.push(this.graphite.scheduler.registerHandler(task.operation, task.handler));
|
48 | }
|
49 | }
|
50 | registerGraphiteEvents(events) {
|
51 | for (const event of events) {
|
52 | this.activeDisposables.push(this.graphite.notifier.on(event.name, event.handler.bind(this)));
|
53 | }
|
54 | }
|
55 | }
|
56 | exports.AbstractLifecyclePlugin = AbstractLifecyclePlugin;
|
57 |
|
\ | No newline at end of file |