From 4e93186e1771cc9fc22dcf7ff8d4011da2e34643 Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 4 Sep 2017 08:28:03 +0200 Subject: [PATCH 1/3] fix: Use setTimeout to delay diagnostics --- src/typescript-service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/typescript-service.ts b/src/typescript-service.ts index 349987338..fa7c0d330 100644 --- a/src/typescript-service.ts +++ b/src/typescript-service.ts @@ -1267,7 +1267,7 @@ export class TypeScriptService { // Ensure files needed for most operations are fetched await this.projectManager.ensureReferencedFiles(uri).toPromise(); this.projectManager.didOpen(uri, params.textDocument.text); - this._publishDiagnostics(uri); + setTimeout(() => this._publishDiagnostics(uri), 200); } /** @@ -1288,7 +1288,7 @@ export class TypeScriptService { return; } this.projectManager.didChange(uri, text); - this._publishDiagnostics(uri); + setTimeout(() => this._publishDiagnostics(uri), 200); } /** From 9d8e71d79a7832d9bc5f8c11d55817ef1ea4c5f1 Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 4 Sep 2017 21:05:30 +0200 Subject: [PATCH 2/3] fix: add sleep to tests --- src/test/typescript-service-helpers.ts | 105 +++++++++++++++---------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/src/test/typescript-service-helpers.ts b/src/test/typescript-service-helpers.ts index e896e0bec..7dc8f2385 100644 --- a/src/test/typescript-service-helpers.ts +++ b/src/test/typescript-service-helpers.ts @@ -1503,7 +1503,7 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor }); }); - describe('Diagnostics', function (this: TestContext & ISuiteCallbackContext) { + describe.only('Diagnostics', function (this: TestContext & ISuiteCallbackContext) { beforeEach(initializeTypeScriptService(createService, rootUri, new Map([ [rootUri + 'src/errors.ts', 'const text: string = 33;'] @@ -1511,16 +1511,23 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor afterEach(shutdownService); + function sleep(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + it('should publish diagnostics on didOpen', async function (this: TestContext & ITestCallbackContext) { - await this.service.textDocumentDidOpen({ - textDocument: { - uri: rootUri + 'src/errors.ts', - languageId: 'typescript', - text: 'const text: string = 33;', - version: 1 - } - }); + await Promise.all([ + this.service.textDocumentDidOpen({ + textDocument: { + uri: rootUri + 'src/errors.ts', + languageId: 'typescript', + text: 'const text: string = 33;', + version: 1 + } + }), + sleep(300) + ]); sinon.assert.calledOnce(this.client.textDocumentPublishDiagnostics); sinon.assert.calledWithExactly(this.client.textDocumentPublishDiagnostics, { @@ -1537,26 +1544,32 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor it('should publish diagnostics on didChange', async function (this: TestContext & ITestCallbackContext) { - await this.service.textDocumentDidOpen({ - textDocument: { - uri: rootUri + 'src/errors.ts', - languageId: 'typescript', - text: 'const text: string = 33;', - version: 1 - } - }); + await Promise.all([ + await this.service.textDocumentDidOpen({ + textDocument: { + uri: rootUri + 'src/errors.ts', + languageId: 'typescript', + text: 'const text: string = 33;', + version: 1 + } + }), + sleep(300) + ]); this.client.textDocumentPublishDiagnostics.resetHistory(); - await this.service.textDocumentDidChange({ - textDocument: { - uri: rootUri + 'src/errors.ts', - version: 2 - }, - contentChanges: [ - { text: 'const text: boolean = 33;' } - ] - }); + await Promise.all([ + this.service.textDocumentDidChange({ + textDocument: { + uri: rootUri + 'src/errors.ts', + version: 2 + }, + contentChanges: [ + { text: 'const text: boolean = 33;' } + ] + }), + sleep(300) + ]); sinon.assert.calledOnce(this.client.textDocumentPublishDiagnostics); sinon.assert.calledWithExactly(this.client.textDocumentPublishDiagnostics, { @@ -1573,26 +1586,32 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor it('should publish empty diagnostics on didChange if error was fixed', async function (this: TestContext & ITestCallbackContext) { - await this.service.textDocumentDidOpen({ - textDocument: { - uri: rootUri + 'src/errors.ts', - languageId: 'typescript', - text: 'const text: string = 33;', - version: 1 - } - }); + await Promise.all([ + this.service.textDocumentDidOpen({ + textDocument: { + uri: rootUri + 'src/errors.ts', + languageId: 'typescript', + text: 'const text: string = 33;', + version: 1 + } + }), + sleep(300) + ]); this.client.textDocumentPublishDiagnostics.resetHistory(); - await this.service.textDocumentDidChange({ - textDocument: { - uri: rootUri + 'src/errors.ts', - version: 2 - }, - contentChanges: [ - { text: 'const text: number = 33;' } - ] - }); + await Promise.all([ + this.service.textDocumentDidChange({ + textDocument: { + uri: rootUri + 'src/errors.ts', + version: 2 + }, + contentChanges: [ + { text: 'const text: number = 33;' } + ] + }), + sleep(300) + ]); sinon.assert.calledOnce(this.client.textDocumentPublishDiagnostics); sinon.assert.calledWithExactly(this.client.textDocumentPublishDiagnostics, { From 6318209d19825bfd934f989752e9477904ac45ea Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 4 Sep 2017 21:16:01 +0200 Subject: [PATCH 3/3] fix: run publishDiagnostics in handler after timeout --- src/test/typescript-service-helpers.ts | 105 ++++++++++--------------- src/typescript-service.ts | 6 +- 2 files changed, 47 insertions(+), 64 deletions(-) diff --git a/src/test/typescript-service-helpers.ts b/src/test/typescript-service-helpers.ts index 7dc8f2385..e896e0bec 100644 --- a/src/test/typescript-service-helpers.ts +++ b/src/test/typescript-service-helpers.ts @@ -1503,7 +1503,7 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor }); }); - describe.only('Diagnostics', function (this: TestContext & ISuiteCallbackContext) { + describe('Diagnostics', function (this: TestContext & ISuiteCallbackContext) { beforeEach(initializeTypeScriptService(createService, rootUri, new Map([ [rootUri + 'src/errors.ts', 'const text: string = 33;'] @@ -1511,23 +1511,16 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor afterEach(shutdownService); - function sleep(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - it('should publish diagnostics on didOpen', async function (this: TestContext & ITestCallbackContext) { - await Promise.all([ - this.service.textDocumentDidOpen({ - textDocument: { - uri: rootUri + 'src/errors.ts', - languageId: 'typescript', - text: 'const text: string = 33;', - version: 1 - } - }), - sleep(300) - ]); + await this.service.textDocumentDidOpen({ + textDocument: { + uri: rootUri + 'src/errors.ts', + languageId: 'typescript', + text: 'const text: string = 33;', + version: 1 + } + }); sinon.assert.calledOnce(this.client.textDocumentPublishDiagnostics); sinon.assert.calledWithExactly(this.client.textDocumentPublishDiagnostics, { @@ -1544,32 +1537,26 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor it('should publish diagnostics on didChange', async function (this: TestContext & ITestCallbackContext) { - await Promise.all([ - await this.service.textDocumentDidOpen({ - textDocument: { - uri: rootUri + 'src/errors.ts', - languageId: 'typescript', - text: 'const text: string = 33;', - version: 1 - } - }), - sleep(300) - ]); + await this.service.textDocumentDidOpen({ + textDocument: { + uri: rootUri + 'src/errors.ts', + languageId: 'typescript', + text: 'const text: string = 33;', + version: 1 + } + }); this.client.textDocumentPublishDiagnostics.resetHistory(); - await Promise.all([ - this.service.textDocumentDidChange({ - textDocument: { - uri: rootUri + 'src/errors.ts', - version: 2 - }, - contentChanges: [ - { text: 'const text: boolean = 33;' } - ] - }), - sleep(300) - ]); + await this.service.textDocumentDidChange({ + textDocument: { + uri: rootUri + 'src/errors.ts', + version: 2 + }, + contentChanges: [ + { text: 'const text: boolean = 33;' } + ] + }); sinon.assert.calledOnce(this.client.textDocumentPublishDiagnostics); sinon.assert.calledWithExactly(this.client.textDocumentPublishDiagnostics, { @@ -1586,32 +1573,26 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor it('should publish empty diagnostics on didChange if error was fixed', async function (this: TestContext & ITestCallbackContext) { - await Promise.all([ - this.service.textDocumentDidOpen({ - textDocument: { - uri: rootUri + 'src/errors.ts', - languageId: 'typescript', - text: 'const text: string = 33;', - version: 1 - } - }), - sleep(300) - ]); + await this.service.textDocumentDidOpen({ + textDocument: { + uri: rootUri + 'src/errors.ts', + languageId: 'typescript', + text: 'const text: string = 33;', + version: 1 + } + }); this.client.textDocumentPublishDiagnostics.resetHistory(); - await Promise.all([ - this.service.textDocumentDidChange({ - textDocument: { - uri: rootUri + 'src/errors.ts', - version: 2 - }, - contentChanges: [ - { text: 'const text: number = 33;' } - ] - }), - sleep(300) - ]); + await this.service.textDocumentDidChange({ + textDocument: { + uri: rootUri + 'src/errors.ts', + version: 2 + }, + contentChanges: [ + { text: 'const text: number = 33;' } + ] + }); sinon.assert.calledOnce(this.client.textDocumentPublishDiagnostics); sinon.assert.calledWithExactly(this.client.textDocumentPublishDiagnostics, { diff --git a/src/typescript-service.ts b/src/typescript-service.ts index fa7c0d330..ab03bc638 100644 --- a/src/typescript-service.ts +++ b/src/typescript-service.ts @@ -1267,7 +1267,8 @@ export class TypeScriptService { // Ensure files needed for most operations are fetched await this.projectManager.ensureReferencedFiles(uri).toPromise(); this.projectManager.didOpen(uri, params.textDocument.text); - setTimeout(() => this._publishDiagnostics(uri), 200); + await new Promise(resolve => setTimeout(resolve, 200)); + this._publishDiagnostics(uri); } /** @@ -1288,7 +1289,8 @@ export class TypeScriptService { return; } this.projectManager.didChange(uri, text); - setTimeout(() => this._publishDiagnostics(uri), 200); + await new Promise(resolve => setTimeout(resolve, 200)); + this._publishDiagnostics(uri); } /**