Skip to content

Commit a5d813d

Browse files
committed
Introduce thenRegisterOrDispose helper
1 parent 67b78f4 commit a5d813d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/vs/base/common/lifecycle.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,18 @@ export function thenIfNotDisposed<T>(promise: Promise<T>, then: (result: T) => v
818818
disposed = true;
819819
});
820820
}
821+
822+
/**
823+
* Call `then` on a promise that resolves to a {@link IDisposable}, then either register the
824+
* disposable or register it to the {@link DisposableStore}, depending on whether the store is
825+
* disposed or not.
826+
*/
827+
export function thenRegisterOrDispose<T extends IDisposable>(promise: Promise<T>, store: DisposableStore): void {
828+
promise.then(ref => {
829+
if (store.isDisposed) {
830+
ref.dispose();
831+
} else {
832+
store.add(ref);
833+
}
834+
});
835+
}

src/vs/workbench/contrib/chat/browser/chatContentParts/toolInvocationParts/chatTerminalToolConfirmationSubPart.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { asArray } from '../../../../../../base/common/arrays.js';
99
import { Codicon } from '../../../../../../base/common/codicons.js';
1010
import { ErrorNoTelemetry } from '../../../../../../base/common/errors.js';
1111
import { MarkdownString, type IMarkdownString } from '../../../../../../base/common/htmlContent.js';
12-
import { thenIfNotDisposed } from '../../../../../../base/common/lifecycle.js';
12+
import { thenIfNotDisposed, thenRegisterOrDispose } from '../../../../../../base/common/lifecycle.js';
1313
import { Schemas } from '../../../../../../base/common/network.js';
1414
import { isObject } from '../../../../../../base/common/types.js';
1515
import { URI } from '../../../../../../base/common/uri.js';
@@ -126,13 +126,7 @@ export class ChatTerminalToolConfirmationSubPart extends BaseChatToolInvocationS
126126
this._getUniqueCodeBlockUri(),
127127
true
128128
));
129-
textModelService.createModelReference(model.uri).then(ref => {
130-
if (this._store.isDisposed) {
131-
ref.dispose();
132-
} else {
133-
this._register(ref);
134-
}
135-
});
129+
thenRegisterOrDispose(textModelService.createModelReference(model.uri), this._store);
136130
const editor = this._register(this.editorPool.get());
137131
const renderPromise = editor.object.render({
138132
codeBlockIndex: this.codeBlockStartIndex,

0 commit comments

Comments
 (0)