|
| 1 | +import { DisposableCollection, Emitter, Event } from '@theia/core'; |
| 2 | +import { FrontendApplicationContribution } from '@theia/core/lib/browser'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { HostedPluginSupport } from './theia/plugin-ext/hosted-plugin'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Frontend contribution to watch VS Code extension start/stop events from Theia. |
| 8 | + * |
| 9 | + * In Theia, there are no events when a VS Code extension is loaded, started, unloaded, and stopped. |
| 10 | + * Currently, it's possible to `@inject` the `HostedPluginSupport` service from Theia and `await` |
| 11 | + * for the `didStart` promise to resolve. But if the OS goes to sleep, the VS Code extensions will |
| 12 | + * be unloaded and loaded and started again when the OS awakes. Theia reloads the VS Code extensions |
| 13 | + * after the OS awake event, but the `didStart` promise was already resolved, so IDE2 cannot restart the LS. |
| 14 | + * This service is meant to work around the limitation of Theia and fire an event every time the VS Code extensions |
| 15 | + * loaded and started. |
| 16 | + */ |
| 17 | +@injectable() |
| 18 | +export class HostedPluginEvents implements FrontendApplicationContribution { |
| 19 | + @inject(HostedPluginSupport) |
| 20 | + private readonly hostedPluginSupport: HostedPluginSupport; |
| 21 | + |
| 22 | + private firstStart = true; |
| 23 | + private readonly onPluginsDidStartEmitter = new Emitter<void>(); |
| 24 | + private readonly onPluginsWillUnloadEmitter = new Emitter<void>(); |
| 25 | + private readonly toDispose = new DisposableCollection( |
| 26 | + this.onPluginsDidStartEmitter, |
| 27 | + this.onPluginsWillUnloadEmitter |
| 28 | + ); |
| 29 | + |
| 30 | + onStart(): void { |
| 31 | + this.hostedPluginSupport.onDidLoad(() => { |
| 32 | + // Fire the first event, when `didStart` resolves. |
| 33 | + if (!this.firstStart) { |
| 34 | + console.debug('HostedPluginEvents', "Received 'onDidLoad' event."); |
| 35 | + this.onPluginsDidStartEmitter.fire(); |
| 36 | + } else { |
| 37 | + console.debug( |
| 38 | + 'HostedPluginEvents', |
| 39 | + "Received 'onDidLoad' event before the first start. Skipping." |
| 40 | + ); |
| 41 | + } |
| 42 | + }); |
| 43 | + this.hostedPluginSupport.didStart.then(() => { |
| 44 | + console.debug('HostedPluginEvents', "Hosted plugins 'didStart'."); |
| 45 | + if (!this.firstStart) { |
| 46 | + throw new Error( |
| 47 | + 'Unexpectedly received a `didStart` event after the first start.' |
| 48 | + ); |
| 49 | + } |
| 50 | + this.firstStart = false; |
| 51 | + this.onPluginsDidStartEmitter.fire(); |
| 52 | + }); |
| 53 | + this.hostedPluginSupport.onDidCloseConnection(() => { |
| 54 | + console.debug('HostedPluginEvents', "Received 'onDidCloseConnection'."); |
| 55 | + this.onPluginsWillUnloadEmitter.fire(); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + onStop(): void { |
| 60 | + this.toDispose.dispose(); |
| 61 | + } |
| 62 | + |
| 63 | + get onPluginsDidStart(): Event<void> { |
| 64 | + return this.onPluginsDidStartEmitter.event; |
| 65 | + } |
| 66 | + |
| 67 | + get onPluginsWillUnload(): Event<void> { |
| 68 | + return this.onPluginsWillUnloadEmitter.event; |
| 69 | + } |
| 70 | + |
| 71 | + get didStart(): Promise<void> { |
| 72 | + return this.hostedPluginSupport.didStart; |
| 73 | + } |
| 74 | +} |
0 commit comments