1- import { Event , EventEmitter } from 'vscode'
1+ import { Event , EventEmitter , Disposable as VSCodeDisposable } from 'vscode'
22import fetch from 'node-fetch'
33import { STUDIO_URL } from './webview/contract'
44import { AvailableCommands , InternalCommands } from '../commands/internal'
@@ -27,6 +27,7 @@ export class Studio extends Disposable {
2727 private studioAccessToken : string | undefined = undefined
2828 private studioIsConnected = false
2929 private shareLiveToStudio : boolean | undefined = undefined
30+ private studioAccessTokenUriHandler : VSCodeDisposable | undefined = undefined
3031
3132 constructor (
3233 internalCommands : InternalCommands ,
@@ -108,6 +109,8 @@ export class Studio extends Disposable {
108109 }
109110
110111 public async requestStudioTokenAuthentication ( ) {
112+ this . resetAccessTokenUriHandler ( )
113+
111114 const response = await this . fetchFromStudio (
112115 `${ STUDIO_URL } /api/device-login` ,
113116 {
@@ -134,9 +137,17 @@ export class Studio extends Disposable {
134137 verificationUrlWithParams . searchParams . append ( 'code' , userCode )
135138
136139 await openUrl ( verificationUrlWithParams . toString ( ) )
137- void waitForUriResponse ( '/studio-complete-auth' , ( ) => {
138- void this . requestStudioToken ( deviceCode , tokenUri )
139- } )
140+ this . studioAccessTokenUriHandler = waitForUriResponse (
141+ '/studio-complete-auth' ,
142+ ( ) => {
143+ void this . requestStudioToken ( deviceCode , tokenUri )
144+ }
145+ )
146+ }
147+
148+ private resetAccessTokenUriHandler ( ) {
149+ this . studioAccessTokenUriHandler ?. dispose ( )
150+ this . studioAccessTokenUriHandler = undefined
140151 }
141152
142153 private fetchFromStudio ( reqUri : string , body : Record < string , unknown > ) {
@@ -171,6 +182,7 @@ export class Studio extends Disposable {
171182 }
172183
173184 private requestStudioToken ( deviceCode : string , tokenUri : string ) {
185+ this . resetAccessTokenUriHandler ( )
174186 return Toast . showProgress ( 'Connecting to Studio' , async progress => {
175187 progress . report ( { increment : 0 } )
176188 progress . report ( { increment : 25 , message : 'Fetching token...' } )
0 commit comments