3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { CancelablePromise , createCancelablePromise } from 'vs/base/common/async' ;
7
- import { CancellationToken } from 'vs/base/common/cancellation' ;
6
+ import { CancellationToken , CancellationTokenSource } from 'vs/base/common/cancellation' ;
8
7
import { Emitter , Event } from 'vs/base/common/event' ;
9
8
import { MarkdownString } from 'vs/base/common/htmlContent' ;
10
9
import { Iterable } from 'vs/base/common/iterator' ;
@@ -135,7 +134,7 @@ export class ChatService extends Disposable implements IChatService {
135
134
private readonly _providers = new Map < string , IChatProvider > ( ) ;
136
135
137
136
private readonly _sessionModels = this . _register ( new DisposableMap < string , ChatModel > ( ) ) ;
138
- private readonly _pendingRequests = new Map < string , CancelablePromise < void > > ( ) ;
137
+ private readonly _pendingRequests = this . _register ( new DisposableMap < string , CancellationTokenSource > ( ) ) ;
139
138
private readonly _persistedSessions : ISerializableChatsData ;
140
139
private readonly _hasProvider : IContextKey < boolean > ;
141
140
@@ -461,7 +460,9 @@ export class ChatService extends Disposable implements IChatService {
461
460
let gotProgress = false ;
462
461
const requestType = commandPart ? 'slashCommand' : 'string' ;
463
462
464
- const rawResponsePromise = createCancelablePromise < void > ( async token => {
463
+ const source = new CancellationTokenSource ( ) ;
464
+ const token = source . token ;
465
+ const sendRequestInternal = async ( ) => {
465
466
const progressCallback = ( progress : IChatProgress ) => {
466
467
if ( token . isCancellationRequested ) {
467
468
return ;
@@ -599,10 +600,11 @@ export class ChatService extends Disposable implements IChatService {
599
600
} finally {
600
601
listener . dispose ( ) ;
601
602
}
602
- } ) ;
603
- this . _pendingRequests . set ( model . sessionId , rawResponsePromise ) ;
603
+ } ;
604
+ const rawResponsePromise = sendRequestInternal ( ) ;
605
+ this . _pendingRequests . set ( model . sessionId , source ) ;
604
606
rawResponsePromise . finally ( ( ) => {
605
- this . _pendingRequests . delete ( model . sessionId ) ;
607
+ this . _pendingRequests . deleteAndDispose ( model . sessionId ) ;
606
608
} ) ;
607
609
return rawResponsePromise ;
608
610
}
@@ -662,6 +664,7 @@ export class ChatService extends Disposable implements IChatService {
662
664
cancelCurrentRequestForSession ( sessionId : string ) : void {
663
665
this . trace ( 'cancelCurrentRequestForSession' , `sessionId: ${ sessionId } ` ) ;
664
666
this . _pendingRequests . get ( sessionId ) ?. cancel ( ) ;
667
+ this . _pendingRequests . deleteAndDispose ( sessionId ) ;
665
668
}
666
669
667
670
clearSession ( sessionId : string ) : void {
@@ -675,6 +678,7 @@ export class ChatService extends Disposable implements IChatService {
675
678
676
679
this . _sessionModels . deleteAndDispose ( sessionId ) ;
677
680
this . _pendingRequests . get ( sessionId ) ?. cancel ( ) ;
681
+ this . _pendingRequests . deleteAndDispose ( sessionId ) ;
678
682
this . _onDidDisposeSession . fire ( { sessionId, providerId : model . providerId , reason : 'cleared' } ) ;
679
683
}
680
684
0 commit comments