- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathhosted-plugin-events.ts
74 lines (67 loc) · 2.76 KB
/
hosted-plugin-events.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
import{DisposableCollection,Emitter,Event}from'@theia/core';
import{FrontendApplicationContribution}from'@theia/core/lib/browser';
import{inject,injectable}from'@theia/core/shared/inversify';
import{HostedPluginSupport}from'./hosted-plugin-support';
/**
* Frontend contribution to watch VS Code extension start/stop events from Theia.
*
* In Theia, there are no events when a VS Code extension is loaded, started, unloaded, and stopped.
* Currently, it's possible to `@inject` the `HostedPluginSupport` service from Theia and `await`
* for the `didStart` promise to resolve. But if the OS goes to sleep, the VS Code extensions will
* be unloaded and loaded and started again when the OS awakes. Theia reloads the VS Code extensions
* after the OS awake event, but the `didStart` promise was already resolved, so IDE2 cannot restart the LS.
* This service is meant to work around the limitation of Theia and fire an event every time the VS Code extensions
* loaded and started.
*/
@injectable()
exportclassHostedPluginEventsimplementsFrontendApplicationContribution{
@inject(HostedPluginSupport)
privatereadonlyhostedPluginSupport: HostedPluginSupport;
privatefirstStart=true;
privatereadonlyonPluginsDidStartEmitter=newEmitter<void>();
privatereadonlyonPluginsWillUnloadEmitter=newEmitter<void>();
privatereadonlytoDispose=newDisposableCollection(
this.onPluginsDidStartEmitter,
this.onPluginsWillUnloadEmitter
);
onStart(): void{
this.hostedPluginSupport.onDidLoad(()=>{
// Fire the first event, when `didStart` resolves.
if(!this.firstStart){
console.debug('HostedPluginEvents',"Received 'onDidLoad' event.");
this.onPluginsDidStartEmitter.fire();
}else{
console.debug(
'HostedPluginEvents',
"Received 'onDidLoad' event before the first start. Skipping."
);
}
});
this.hostedPluginSupport.didStart.then(()=>{
console.debug('HostedPluginEvents',"Hosted plugins 'didStart'.");
if(!this.firstStart){
thrownewError(
'Unexpectedly received a `didStart` event after the first start.'
);
}
this.firstStart=false;
this.onPluginsDidStartEmitter.fire();
});
this.hostedPluginSupport.onDidCloseConnection(()=>{
console.debug('HostedPluginEvents',"Received 'onDidCloseConnection'.");
this.onPluginsWillUnloadEmitter.fire();
});
}
onStop(): void{
this.toDispose.dispose();
}
getonPluginsDidStart(): Event<void>{
returnthis.onPluginsDidStartEmitter.event;
}
getonPluginsWillUnload(): Event<void>{
returnthis.onPluginsWillUnloadEmitter.event;
}
getdidStart(): Promise<void>{
returnthis.hostedPluginSupport.didStart;
}
}