11'use strict' ;
2- import { Disposable , workspace } from 'vscode' ;
3- import { getApi , LiveShare , Role , SessionChangeEvent } from 'vsls' ;
2+ import { Disposable , extensions , workspace } from 'vscode' ;
3+ import type { LiveShare , LiveShareExtension , SessionChangeEvent } from '../@types/ vsls' ;
44import { ContextKeys , DocumentSchemes , setContext } from '../constants' ;
55import { Container } from '../container' ;
66import { Logger } from '../logger' ;
@@ -40,7 +40,7 @@ export class VslsController implements Disposable {
4040 private _onReady : ( ( ) => void ) | undefined ;
4141 private _waitForReady : Promise < void > | undefined ;
4242
43- private _api : Promise < LiveShare | null > | undefined ;
43+ private _api : Promise < LiveShare | undefined > | undefined ;
4444
4545 constructor ( ) {
4646 void this . initialize ( ) ;
@@ -60,7 +60,7 @@ export class VslsController implements Disposable {
6060 this . _waitForReady = new Promise ( resolve => ( this . _onReady = resolve ) ) ;
6161 }
6262
63- this . _api = getApi ( ) ;
63+ this . _api = this . getLiveShareApi ( ) ;
6464 const api = await this . _api ;
6565 if ( api == null ) {
6666 void setContext ( ContextKeys . Vsls , false ) ;
@@ -83,6 +83,18 @@ export class VslsController implements Disposable {
8383 }
8484 }
8585
86+ private async getLiveShareApi ( ) : Promise < LiveShare | undefined > {
87+ try {
88+ const extension = extensions . getExtension < LiveShareExtension > ( 'ms-vsliveshare.vsliveshare' ) ;
89+ if ( extension != null ) {
90+ const liveshareExtension = extension . isActive ? extension . exports : await extension . activate ( ) ;
91+ return ( await liveshareExtension . getApi ( '1.0.3015' ) ) ?? undefined ;
92+ }
93+ } catch { }
94+
95+ return undefined ;
96+ }
97+
8698 get isMaybeGuest ( ) {
8799 return this . _guest !== undefined || this . _waitForReady !== undefined ;
88100 }
@@ -112,7 +124,7 @@ export class VslsController implements Disposable {
112124 0 : ( emails : string [ ] ) => `length=${ emails . length } ` ,
113125 } ,
114126 } )
115- async getContacts ( emails : string [ ] ) {
127+ private async getContacts ( emails : string [ ] ) {
116128 const api = await this . _api ;
117129 if ( api == null ) return undefined ;
118130
@@ -144,7 +156,7 @@ export class VslsController implements Disposable {
144156
145157 @debug ( )
146158 @timeout ( 250 )
147- maybeGetPresence ( email : string | undefined ) {
159+ maybeGetPresence ( email : string | undefined ) : Promise < ContactPresence | undefined > {
148160 return Container . vsls . getContactPresence ( email ) ;
149161 }
150162
@@ -178,23 +190,18 @@ export class VslsController implements Disposable {
178190 }
179191
180192 private async onLiveShareSessionChanged ( api : LiveShare , e : SessionChangeEvent ) {
181- if ( this . _host !== undefined ) {
182- this . _host . dispose ( ) ;
183- }
184-
185- if ( this . _guest !== undefined ) {
186- this . _guest . dispose ( ) ;
187- }
193+ this . _host ?. dispose ( ) ;
194+ this . _guest ?. dispose ( ) ;
188195
189196 switch ( e . session . role ) {
190- case Role . Host :
197+ case 1 /* Role.Host*/ :
191198 this . setReadonly ( false ) ;
192199 void setContext ( ContextKeys . Vsls , 'host' ) ;
193200 if ( Container . config . liveshare . allowGuestAccess ) {
194201 this . _host = await VslsHostService . share ( api ) ;
195202 }
196203 break ;
197- case Role . Guest :
204+ case 2 /* Role.Guest*/ :
198205 this . setReadonly ( true ) ;
199206 void setContext ( ContextKeys . Vsls , 'guest' ) ;
200207 this . _guest = await VslsGuestService . connect ( api ) ;
0 commit comments