Skip to content

Commit 890820b

Browse files
committed
cleanup NodeTypingsInstaller
1 parent 8b0d3ab commit 890820b

1 file changed

Lines changed: 72 additions & 95 deletions

File tree

src/server/server.ts

Lines changed: 72 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ namespace ts.server {
246246

247247
class NodeTypingsInstaller implements ITypingsInstaller {
248248
private installer: NodeChildProcess;
249-
private installerPidReported = false;
250249
private projectService: ProjectService;
251250
private activeRequestCount = 0;
252251
private requestQueue: QueuedOperation[] = [];
@@ -272,7 +271,7 @@ namespace ts.server {
272271
readonly typingSafeListLocation: string,
273272
readonly typesMapLocation: string,
274273
private readonly npmLocation: string | undefined,
275-
private event: Event | undefined) {
274+
private event: Event) {
276275
}
277276

278277
isKnownTypesPackageName(name: string): boolean {
@@ -300,17 +299,6 @@ namespace ts.server {
300299
});
301300
}
302301

303-
private reportInstallerProcessId() {
304-
if (this.installerPidReported) {
305-
return;
306-
}
307-
if (this.event && this.installer) {
308-
this.event({ pid: this.installer.pid }, "typingsInstallerPid");
309-
this.installerPidReported = true;
310-
}
311-
}
312-
313-
314302
attach(projectService: ProjectService) {
315303
this.projectService = projectService;
316304
if (this.logger.hasLevel(LogLevel.requestTime)) {
@@ -350,7 +338,8 @@ namespace ts.server {
350338

351339
this.installer = childProcess.fork(combinePaths(__dirname, "typingsInstaller.js"), args, { execArgv });
352340
this.installer.on("message", m => this.handleMessage(m));
353-
this.reportInstallerProcessId();
341+
342+
this.event({ pid: this.installer.pid }, "typingsInstallerPid");
354343

355344
process.on("exit", () => {
356345
this.installer.kill();
@@ -415,92 +404,81 @@ namespace ts.server {
415404
break;
416405
}
417406
case EventInitializationFailed:
418-
{
419-
if (!this.event) {
407+
{
408+
const body: protocol.TypesInstallerInitializationFailedEventBody = {
409+
message: response.message
410+
};
411+
const eventName: protocol.TypesInstallerInitializationFailedEventName = "typesInstallerInitializationFailed";
412+
this.event(body, eventName);
420413
break;
421414
}
422-
const body: protocol.TypesInstallerInitializationFailedEventBody = {
423-
message: response.message
424-
};
425-
const eventName: protocol.TypesInstallerInitializationFailedEventName = "typesInstallerInitializationFailed";
426-
this.event(body, eventName);
427-
break;
428-
}
429415
case EventBeginInstallTypes:
430-
{
431-
if (!this.event) {
416+
{
417+
const body: protocol.BeginInstallTypesEventBody = {
418+
eventId: response.eventId,
419+
packages: response.packagesToInstall,
420+
};
421+
const eventName: protocol.BeginInstallTypesEventName = "beginInstallTypes";
422+
this.event(body, eventName);
432423
break;
433424
}
434-
const body: protocol.BeginInstallTypesEventBody = {
435-
eventId: response.eventId,
436-
packages: response.packagesToInstall,
437-
};
438-
const eventName: protocol.BeginInstallTypesEventName = "beginInstallTypes";
439-
this.event(body, eventName);
440-
break;
441-
}
442425
case EventEndInstallTypes:
443-
{
444-
if (!this.event) {
445-
break;
446-
}
447-
if (this.telemetryEnabled) {
448-
const body: protocol.TypingsInstalledTelemetryEventBody = {
449-
telemetryEventName: "typingsInstalled",
450-
payload: {
451-
installedPackages: response.packagesToInstall.join(","),
452-
installSuccess: response.installSuccess,
453-
typingsInstallerVersion: response.typingsInstallerVersion
454-
}
426+
{
427+
if (this.telemetryEnabled) {
428+
const body: protocol.TypingsInstalledTelemetryEventBody = {
429+
telemetryEventName: "typingsInstalled",
430+
payload: {
431+
installedPackages: response.packagesToInstall.join(","),
432+
installSuccess: response.installSuccess,
433+
typingsInstallerVersion: response.typingsInstallerVersion
434+
}
435+
};
436+
const eventName: protocol.TelemetryEventName = "telemetry";
437+
this.event(body, eventName);
438+
}
439+
440+
const body: protocol.EndInstallTypesEventBody = {
441+
eventId: response.eventId,
442+
packages: response.packagesToInstall,
443+
success: response.installSuccess,
455444
};
456-
const eventName: protocol.TelemetryEventName = "telemetry";
445+
const eventName: protocol.EndInstallTypesEventName = "endInstallTypes";
457446
this.event(body, eventName);
447+
break;
458448
}
459-
460-
const body: protocol.EndInstallTypesEventBody = {
461-
eventId: response.eventId,
462-
packages: response.packagesToInstall,
463-
success: response.installSuccess,
464-
};
465-
const eventName: protocol.EndInstallTypesEventName = "endInstallTypes";
466-
this.event(body, eventName);
467-
break;
468-
}
469449
case ActionInvalidate:
470-
{
471-
this.projectService.updateTypingsForProject(response);
472-
break;
473-
}
474-
case ActionSet:
475-
{
476-
if (this.activeRequestCount > 0) {
477-
this.activeRequestCount--;
478-
}
479-
else {
480-
Debug.fail("Received too many responses");
450+
{
451+
this.projectService.updateTypingsForProject(response);
452+
break;
481453
}
482-
483-
while (this.requestQueue.length > 0) {
484-
const queuedRequest = this.requestQueue.shift();
485-
if (this.requestMap.get(queuedRequest.operationId) === queuedRequest) {
486-
this.requestMap.delete(queuedRequest.operationId);
487-
this.scheduleRequest(queuedRequest);
488-
break;
454+
case ActionSet:
455+
{
456+
if (this.activeRequestCount > 0) {
457+
this.activeRequestCount--;
458+
}
459+
else {
460+
Debug.fail("Received too many responses");
489461
}
490462

491-
if (this.logger.hasLevel(LogLevel.verbose)) {
492-
this.logger.info(`Skipping defunct request for: ${queuedRequest.operationId}`);
463+
while (this.requestQueue.length > 0) {
464+
const queuedRequest = this.requestQueue.shift();
465+
if (this.requestMap.get(queuedRequest.operationId) === queuedRequest) {
466+
this.requestMap.delete(queuedRequest.operationId);
467+
this.scheduleRequest(queuedRequest);
468+
break;
469+
}
470+
471+
if (this.logger.hasLevel(LogLevel.verbose)) {
472+
this.logger.info(`Skipping defunct request for: ${queuedRequest.operationId}`);
473+
}
493474
}
494-
}
495475

496-
this.projectService.updateTypingsForProject(response);
476+
this.projectService.updateTypingsForProject(response);
497477

498-
if (this.event) {
499478
this.event(response, "setTypings");
500-
}
501479

502-
break;
503-
}
480+
break;
481+
}
504482
default:
505483
assertTypeIsNever(response);
506484
}
@@ -524,19 +502,18 @@ namespace ts.server {
524502
constructor(options: IoSessionOptions) {
525503
const { host, eventPort, globalTypingsCacheLocation, typingSafeListLocation, typesMapLocation, npmLocation, canUseEvents } = options;
526504

527-
const event: Event | undefined = canUseEvents ?
528-
(body: {}, eventName: string) => {
529-
if (this.constructed) {
530-
this.event(body, eventName);
531-
}
532-
else {
533-
// It is unsafe to dereference `this` before initialization completes,
534-
// so we defer until the next tick.
535-
//
536-
// Construction should finish before the next tick fires, so we do not need to do this recursively.
537-
setImmediate(() => this.event(body, eventName));
538-
}
539-
} : undefined;
505+
const event: Event | undefined = (body: {}, eventName: string) => {
506+
if (this.constructed) {
507+
this.event(body, eventName);
508+
}
509+
else {
510+
// It is unsafe to dereference `this` before initialization completes,
511+
// so we defer until the next tick.
512+
//
513+
// Construction should finish before the next tick fires, so we do not need to do this recursively.
514+
setImmediate(() => this.event(body, eventName));
515+
}
516+
};
540517

541518
const typingsInstaller = disableAutomaticTypingAcquisition
542519
? undefined

0 commit comments

Comments
 (0)