@@ -29,13 +29,16 @@ type UserAccountEvent = {
2929 id : string ;
3030} ;
3131
32- type AccountQuickPickItem = { label : string , authenticationProvider : IAuthenticationProvider , account ?: IUserDataSyncAccount , detail ?: string } ;
32+ type AccountQuickPickItem = { label : string , authenticationProvider : IAuthenticationProvider , account ?: UserDataSyncAccount , detail ?: string } ;
3333
34- export interface IUserDataSyncAccount {
35- readonly authenticationProviderId : string ;
36- readonly sessionId : string ;
37- readonly accountName : string ;
38- readonly accountId : string ;
34+ export class UserDataSyncAccount {
35+
36+ constructor ( readonly authenticationProviderId : string , private readonly session : AuthenticationSession ) { }
37+
38+ get sessionId ( ) : string { return this . session . id ; }
39+ get accountName ( ) : string { return this . session . account . displayName ; }
40+ get accountId ( ) : string { return this . session . account . id ; }
41+ getToken ( ) : Thenable < string > { return this . session . getAccessToken ( ) ; }
3942}
4043
4144export const enum AccountStatus {
@@ -61,10 +64,10 @@ export class UserDataSyncAccounts extends Disposable {
6164 private readonly _onDidSignOut = this . _register ( new Emitter < void > ( ) ) ;
6265 readonly onDidSignOut = this . _onDidSignOut . event ;
6366
64- private _all : Map < string , IUserDataSyncAccount [ ] > = new Map < string , IUserDataSyncAccount [ ] > ( ) ;
65- get all ( ) : IUserDataSyncAccount [ ] { return flatten ( values ( this . _all ) ) ; }
67+ private _all : Map < string , UserDataSyncAccount [ ] > = new Map < string , UserDataSyncAccount [ ] > ( ) ;
68+ get all ( ) : UserDataSyncAccount [ ] { return flatten ( values ( this . _all ) ) ; }
6669
67- get current ( ) : IUserDataSyncAccount | undefined { return this . all . filter ( account => this . isCurrentAccount ( account ) ) [ 0 ] ; }
70+ get current ( ) : UserDataSyncAccount | undefined { return this . all . filter ( account => this . isCurrentAccount ( account ) ) [ 0 ] ; }
6871
6972 constructor (
7073 @IAuthenticationService private readonly authenticationService : IAuthenticationService ,
@@ -124,45 +127,28 @@ export class UserDataSyncAccounts extends Disposable {
124127 }
125128
126129 private async update ( ) : Promise < void > {
127- const allAccounts : Map < string , IUserDataSyncAccount [ ] > = new Map < string , IUserDataSyncAccount [ ] > ( ) ;
130+ const allAccounts : Map < string , UserDataSyncAccount [ ] > = new Map < string , UserDataSyncAccount [ ] > ( ) ;
128131 for ( const { id } of this . authenticationProviders ) {
129132 const accounts = await this . getAccounts ( id ) ;
130133 allAccounts . set ( id , accounts ) ;
131134 }
132135
133136 this . _all = allAccounts ;
134- const status = this . current ? AccountStatus . Available : AccountStatus . Unavailable ;
135-
136- if ( status === AccountStatus . Unavailable ) {
137- await this . authenticationTokenService . setToken ( undefined ) ;
138- }
139-
140- if ( this . _status !== status ) {
141- const previous = this . _status ;
142- this . logService . debug ( 'Sync account status changed' , previous , status ) ;
143-
144- if ( previous === AccountStatus . Available && status === AccountStatus . Unavailable ) {
145- this . _onDidSignOut . fire ( ) ;
146- }
147-
148- this . _status = status ;
149- this . _onDidChangeStatus . fire ( status ) ;
150- }
137+ const current = this . current ;
138+ await this . updateToken ( current ) ;
139+ this . updateStatus ( current ) ;
151140 }
152141
153- private async getAccounts ( authenticationProviderId : string ) : Promise < IUserDataSyncAccount [ ] > {
154-
155- let accounts : Map < string , IUserDataSyncAccount > = new Map < string , IUserDataSyncAccount > ( ) ;
156- let currentAccount : IUserDataSyncAccount | null = null ;
157- let currentSession : AuthenticationSession | undefined = undefined ;
142+ private async getAccounts ( authenticationProviderId : string ) : Promise < UserDataSyncAccount [ ] > {
143+ let accounts : Map < string , UserDataSyncAccount > = new Map < string , UserDataSyncAccount > ( ) ;
144+ let currentAccount : UserDataSyncAccount | null = null ;
158145
159146 const sessions = await this . authenticationService . getSessions ( authenticationProviderId ) || [ ] ;
160147 for ( const session of sessions ) {
161- const account : IUserDataSyncAccount = { authenticationProviderId, sessionId : session . id , accountName : session . account . displayName , accountId : session . account . id } ;
148+ const account : UserDataSyncAccount = new UserDataSyncAccount ( authenticationProviderId , session ) ;
162149 accounts . set ( account . accountName , account ) ;
163150 if ( this . isCurrentAccount ( account ) ) {
164151 currentAccount = account ;
165- currentSession = session ;
166152 }
167153 }
168154
@@ -171,20 +157,40 @@ export class UserDataSyncAccounts extends Disposable {
171157 accounts . set ( currentAccount . accountName , currentAccount ) ;
172158 }
173159
174- // update access token
175- if ( currentSession ) {
160+ return values ( accounts ) ;
161+ }
162+
163+ private async updateToken ( current : UserDataSyncAccount | undefined ) : Promise < void > {
164+ let value : { token : string , authenticationProviderId : string } | undefined = undefined ;
165+ if ( current ) {
176166 try {
177- const token = await currentSession . getAccessToken ( ) ;
178- await this . authenticationTokenService . setToken ( { token, authenticationProviderId } ) ;
167+ const token = await current . getToken ( ) ;
168+ value = { token, authenticationProviderId : current . authenticationProviderId } ;
179169 } catch ( e ) {
180170 this . logService . error ( e ) ;
181171 }
182172 }
173+ await this . authenticationTokenService . setToken ( value ) ;
174+ }
183175
184- return values ( accounts ) ;
176+ private updateStatus ( current : UserDataSyncAccount | undefined ) : void {
177+ // set status
178+ const status : AccountStatus = current ? AccountStatus . Available : AccountStatus . Unavailable ;
179+
180+ if ( this . _status !== status ) {
181+ const previous = this . _status ;
182+ this . logService . debug ( 'Sync account status changed' , previous , status ) ;
183+
184+ if ( previous === AccountStatus . Available && status === AccountStatus . Unavailable ) {
185+ this . _onDidSignOut . fire ( ) ;
186+ }
187+
188+ this . _status = status ;
189+ this . _onDidChangeStatus . fire ( status ) ;
190+ }
185191 }
186192
187- private isCurrentAccount ( account : IUserDataSyncAccount ) : boolean {
193+ private isCurrentAccount ( account : UserDataSyncAccount ) : boolean {
188194 return account . sessionId === this . currentSessionId ;
189195 }
190196
@@ -208,7 +214,7 @@ export class UserDataSyncAccounts extends Disposable {
208214 return true ;
209215 }
210216
211- private async doPick ( ) : Promise < IUserDataSyncAccount | IAuthenticationProvider | undefined > {
217+ private async doPick ( ) : Promise < UserDataSyncAccount | IAuthenticationProvider | undefined > {
212218 if ( this . authenticationProviders . length === 0 ) {
213219 return undefined ;
214220 }
@@ -220,8 +226,8 @@ export class UserDataSyncAccounts extends Disposable {
220226 return this . authenticationProviders [ 0 ] ;
221227 }
222228
223- return new Promise < IUserDataSyncAccount | IAuthenticationProvider | undefined > ( async ( c , e ) => {
224- let result : IUserDataSyncAccount | IAuthenticationProvider | undefined ;
229+ return new Promise < UserDataSyncAccount | IAuthenticationProvider | undefined > ( async ( c , e ) => {
230+ let result : UserDataSyncAccount | IAuthenticationProvider | undefined ;
225231 const disposables : DisposableStore = new DisposableStore ( ) ;
226232 const quickPick = this . quickInputService . createQuickPick < AccountQuickPickItem > ( ) ;
227233 disposables . add ( quickPick ) ;
0 commit comments