Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/shared-fetch-utils/common/middleware/windowActiveMiddleware.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { FetchBlockedError, type FetchMiddleware, type WindowStateProvider } from '../fetchTypes';
7
8
export class WindowInactiveError extends FetchBlockedError {
9
constructor() {
10
super('Window is inactive', 60_000);
11
}
12
}
13
14
/**
15
* Prevents fetches while the window is inactive by throwing a
16
* {@link WindowInactiveError}. Callers that cache the parsed value
17
* (e.g. {@link FetchedValue}) will fall back to the last-good value
18
* automatically because they handle {@link FetchBlockedError}.
19
*/
20
export function windowActiveMiddleware(provider: WindowStateProvider): FetchMiddleware {
21
return (next) => async (request) => {
22
if (!provider.isActive) {
23
throw new WindowInactiveError();
24
}
25
return next(request);
26
};
27
}
28
29