-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Pull out parts of TI Adapter so we can test that more correctly instead of having to copy things #56387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Pull out parts of TI Adapter so we can test that more correctly instead of having to copy things #56387
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,250 @@ | ||
| import { | ||
| ApplyCodeActionCommandResult, | ||
| assertType, | ||
| createQueue, | ||
| Debug, | ||
| JsTyping, | ||
| MapLike, | ||
| server, | ||
| SortedReadonlyArray, | ||
| TypeAcquisition, | ||
| } from "./_namespaces/ts"; | ||
| import { | ||
| ActionInvalidate, | ||
| ActionPackageInstalled, | ||
| ActionSet, | ||
| ActionWatchTypingLocations, | ||
| BeginInstallTypes, | ||
| createInstallTypingsRequest, | ||
| DiscoverTypings, | ||
| EndInstallTypes, | ||
| Event, | ||
| EventBeginInstallTypes, | ||
| EventEndInstallTypes, | ||
| EventInitializationFailed, | ||
| EventTypesRegistry, | ||
| InitializationFailedResponse, | ||
| InstallPackageOptionsWithProject, | ||
| InstallPackageRequest, | ||
| InvalidateCachedTypings, | ||
| ITypingsInstaller, | ||
| Logger, | ||
| LogLevel, | ||
| PackageInstalledResponse, | ||
| Project, | ||
| ProjectService, | ||
| protocol, | ||
| ServerHost, | ||
| SetTypings, | ||
| stringifyIndented, | ||
| TypesRegistryResponse, | ||
| TypingInstallerRequestUnion, | ||
| } from "./_namespaces/ts.server"; | ||
|
|
||
| /** @internal */ | ||
| export interface TypingsInstallerWorkerProcess { | ||
| send<T extends TypingInstallerRequestUnion>(rq: T): void; | ||
| } | ||
|
|
||
| /** @internal */ | ||
| export abstract class TypingsInstallerAdapter implements ITypingsInstaller { | ||
| protected installer!: TypingsInstallerWorkerProcess; | ||
| private projectService!: ProjectService; | ||
| protected activeRequestCount = 0; | ||
| private requestQueue = createQueue<DiscoverTypings>(); | ||
| private requestMap = new Map<string, DiscoverTypings>(); // Maps project name to newest requestQueue entry for that project | ||
| /** We will lazily request the types registry on the first call to `isKnownTypesPackageName` and store it in `typesRegistryCache`. */ | ||
| private requestedRegistry = false; | ||
| private typesRegistryCache: Map<string, MapLike<string>> | undefined; | ||
|
|
||
| // This number is essentially arbitrary. Processing more than one typings request | ||
| // at a time makes sense, but having too many in the pipe results in a hang | ||
| // (see https://github.com/nodejs/node/issues/7657). | ||
| // It would be preferable to base our limit on the amount of space left in the | ||
| // buffer, but we have yet to find a way to retrieve that value. | ||
| private static readonly requestDelayMillis = 100; | ||
| private packageInstalledPromise: { | ||
| resolve(value: ApplyCodeActionCommandResult): void; | ||
| reject(reason: unknown): void; | ||
| } | undefined; | ||
|
|
||
| constructor( | ||
| protected readonly telemetryEnabled: boolean, | ||
| protected readonly logger: Logger, | ||
| protected readonly host: ServerHost, | ||
| readonly globalTypingsCacheLocation: string, | ||
| protected event: Event, | ||
| private readonly maxActiveRequestCount: number, | ||
| ) { | ||
| } | ||
|
|
||
| isKnownTypesPackageName(name: string): boolean { | ||
| // We want to avoid looking this up in the registry as that is expensive. So first check that it's actually an NPM package. | ||
| const validationResult = JsTyping.validatePackageName(name); | ||
| if (validationResult !== JsTyping.NameValidationResult.Ok) { | ||
| return false; | ||
| } | ||
| if (!this.requestedRegistry) { | ||
| this.requestedRegistry = true; | ||
| this.installer.send({ kind: "typesRegistry" }); | ||
| } | ||
| return !!this.typesRegistryCache?.has(name); | ||
| } | ||
|
|
||
| installPackage(options: InstallPackageOptionsWithProject): Promise<ApplyCodeActionCommandResult> { | ||
| this.installer.send<InstallPackageRequest>({ kind: "installPackage", ...options }); | ||
| Debug.assert(this.packageInstalledPromise === undefined); | ||
| return new Promise<ApplyCodeActionCommandResult>((resolve, reject) => { | ||
| this.packageInstalledPromise = { resolve, reject }; | ||
| }); | ||
| } | ||
|
|
||
| attach(projectService: ProjectService) { | ||
| this.projectService = projectService; | ||
| this.installer = this.createInstallerProcess(); | ||
| } | ||
|
|
||
| onProjectClosed(p: Project): void { | ||
| this.installer.send({ projectName: p.getProjectName(), kind: "closeProject" }); | ||
| } | ||
|
|
||
| enqueueInstallTypingsRequest(project: Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>): void { | ||
| const request = createInstallTypingsRequest(project, typeAcquisition, unresolvedImports); | ||
| if (this.logger.hasLevel(LogLevel.verbose)) { | ||
| this.logger.info(`TIAdapter:: Scheduling throttled operation:${stringifyIndented(request)}`); | ||
| } | ||
|
|
||
| if (this.activeRequestCount < this.maxActiveRequestCount) { | ||
| this.scheduleRequest(request); | ||
| } | ||
| else { | ||
| if (this.logger.hasLevel(LogLevel.verbose)) { | ||
| this.logger.info(`TIAdapter:: Deferring request for: ${request.projectName}`); | ||
| } | ||
| this.requestQueue.enqueue(request); | ||
| this.requestMap.set(request.projectName, request); | ||
| } | ||
| } | ||
|
|
||
| handleMessage(response: TypesRegistryResponse | PackageInstalledResponse | SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse | server.WatchTypingLocations) { | ||
| if (this.logger.hasLevel(LogLevel.verbose)) { | ||
| this.logger.info(`TIAdapter:: Received response:${stringifyIndented(response)}`); | ||
| } | ||
|
|
||
| switch (response.kind) { | ||
| case EventTypesRegistry: | ||
| this.typesRegistryCache = new Map(Object.entries(response.typesRegistry)); | ||
| break; | ||
| case ActionPackageInstalled: { | ||
| const { success, message } = response; | ||
| if (success) { | ||
| this.packageInstalledPromise!.resolve({ successMessage: message }); | ||
| } | ||
| else { | ||
| this.packageInstalledPromise!.reject(message); | ||
| } | ||
| this.packageInstalledPromise = undefined; | ||
|
|
||
| this.projectService.updateTypingsForProject(response); | ||
|
|
||
| // The behavior is the same as for setTypings, so send the same event. | ||
| this.event(response, "setTypings"); | ||
| break; | ||
| } | ||
| case EventInitializationFailed: { | ||
| const body: protocol.TypesInstallerInitializationFailedEventBody = { | ||
| message: response.message, | ||
| }; | ||
| const eventName: protocol.TypesInstallerInitializationFailedEventName = "typesInstallerInitializationFailed"; | ||
| this.event(body, eventName); | ||
| break; | ||
| } | ||
| case EventBeginInstallTypes: { | ||
| const body: protocol.BeginInstallTypesEventBody = { | ||
| eventId: response.eventId, | ||
| packages: response.packagesToInstall, | ||
| }; | ||
| const eventName: protocol.BeginInstallTypesEventName = "beginInstallTypes"; | ||
| this.event(body, eventName); | ||
| break; | ||
| } | ||
| case EventEndInstallTypes: { | ||
| if (this.telemetryEnabled) { | ||
| const body: protocol.TypingsInstalledTelemetryEventBody = { | ||
| telemetryEventName: "typingsInstalled", | ||
| payload: { | ||
| installedPackages: response.packagesToInstall.join(","), | ||
| installSuccess: response.installSuccess, | ||
| typingsInstallerVersion: response.typingsInstallerVersion, | ||
| }, | ||
| }; | ||
| const eventName: protocol.TelemetryEventName = "telemetry"; | ||
| this.event(body, eventName); | ||
| } | ||
|
|
||
| const body: protocol.EndInstallTypesEventBody = { | ||
| eventId: response.eventId, | ||
| packages: response.packagesToInstall, | ||
| success: response.installSuccess, | ||
| }; | ||
| const eventName: protocol.EndInstallTypesEventName = "endInstallTypes"; | ||
| this.event(body, eventName); | ||
| break; | ||
| } | ||
| case ActionInvalidate: { | ||
| this.projectService.updateTypingsForProject(response); | ||
| break; | ||
| } | ||
| case ActionSet: { | ||
| if (this.activeRequestCount > 0) { | ||
| this.activeRequestCount--; | ||
| } | ||
| else { | ||
| Debug.fail("TIAdapter:: Received too many responses"); | ||
| } | ||
|
|
||
| while (!this.requestQueue.isEmpty()) { | ||
| const queuedRequest = this.requestQueue.dequeue(); | ||
| if (this.requestMap.get(queuedRequest.projectName) === queuedRequest) { | ||
| this.requestMap.delete(queuedRequest.projectName); | ||
| this.scheduleRequest(queuedRequest); | ||
| break; | ||
| } | ||
|
|
||
| if (this.logger.hasLevel(LogLevel.verbose)) { | ||
| this.logger.info(`TIAdapter:: Skipping defunct request for: ${queuedRequest.projectName}`); | ||
| } | ||
| } | ||
|
|
||
| this.projectService.updateTypingsForProject(response); | ||
| this.event(response, "setTypings"); | ||
|
|
||
| break; | ||
| } | ||
| case ActionWatchTypingLocations: | ||
| this.projectService.watchTypingLocations(response); | ||
| break; | ||
| default: | ||
| assertType<never>(response); | ||
| } | ||
| } | ||
|
|
||
| scheduleRequest(request: DiscoverTypings) { | ||
| if (this.logger.hasLevel(LogLevel.verbose)) { | ||
| this.logger.info(`TIAdapter:: Scheduling request for: ${request.projectName}`); | ||
| } | ||
| this.activeRequestCount++; | ||
| this.host.setTimeout( | ||
| () => { | ||
| if (this.logger.hasLevel(LogLevel.verbose)) { | ||
| this.logger.info(`TIAdapter:: Sending request:${stringifyIndented(request)}`); | ||
| } | ||
| this.installer.send(request); | ||
| }, | ||
| TypingsInstallerAdapter.requestDelayMillis, | ||
| `${request.projectName}::${request.kind}`, | ||
| ); | ||
| } | ||
|
|
||
| protected abstract createInstallerProcess(): TypingsInstallerWorkerProcess; | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the fix for unnecessary queueing of closeProject request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eg tests in unittests\tsserver\autoImportProvider would fail by showing creating TI when the project is closed that does not enable TI