forked from neoclide/coc-tsserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid cache cancelled response for CodeLens
- Loading branch information
Showing
3 changed files
with
56 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { TextDocument } from 'coc.nvim' | ||
import type * as Proto from '../protocol' | ||
import { ServerResponse } from '../typescriptService' | ||
|
||
type Resolve<T extends Proto.Response> = () => Promise<ServerResponse.Response<T>> | ||
|
||
/** | ||
* Caches a class of TS Server request based on document. | ||
*/ | ||
export class CachedResponse<T extends Proto.Response> { | ||
private response?: Promise<ServerResponse.Response<T>> | ||
private version: number = -1; | ||
private document: string = ''; | ||
|
||
/** | ||
* Execute a request. May return cached value or resolve the new value | ||
* | ||
* Caller must ensure that all input `resolve` functions return equivalent results (keyed only off of document). | ||
*/ | ||
public execute( | ||
document: TextDocument, | ||
resolve: Resolve<T> | ||
): Promise<ServerResponse.Response<T>> { | ||
if (this.response && this.matches(document)) { | ||
// Chain so that on cancellation we fall back to the next resolve | ||
return this.response = this.response.then(result => result.type === 'cancelled' ? resolve() : result) | ||
} | ||
return this.reset(document, resolve) | ||
} | ||
|
||
private matches(document: TextDocument): boolean { | ||
return this.version === document.version && this.document === document.uri.toString() | ||
} | ||
|
||
private async reset( | ||
document: TextDocument, | ||
resolve: Resolve<T> | ||
): Promise<ServerResponse.Response<T>> { | ||
this.version = document.version | ||
this.document = document.uri.toString() | ||
return this.response = resolve() | ||
} | ||
} |