From 641f5873d7811cbe636572b25495954d0291ad80 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 10 Aug 2019 20:26:16 +0300 Subject: [PATCH 1/4] refactor: convert method to function, because it is not a method --- packages/jest-haste-map/src/index.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index 70965909d79d..103ed5eb250c 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -391,7 +391,7 @@ class HasteMap extends EventEmitter { try { hasteMap = serializer.readFileSync(this._cachePath); } catch (err) { - hasteMap = this._createEmptyMap(); + hasteMap = createEmptyMap(); } return hasteMap; @@ -417,10 +417,10 @@ class HasteMap extends EventEmitter { }> { let hasteMap: InternalHasteMap; try { - const read = this._options.resetCache ? this._createEmptyMap : this.read; + const read = this._options.resetCache ? createEmptyMap : this.read; hasteMap = await read.call(this); } catch { - hasteMap = this._createEmptyMap(); + hasteMap = createEmptyMap(); } return this._crawl(hasteMap); } @@ -1102,16 +1102,6 @@ class HasteMap extends EventEmitter { return true; } - private _createEmptyMap(): InternalHasteMap { - return { - clocks: new Map(), - duplicates: new Map(), - files: new Map(), - map: new Map(), - mocks: new Map(), - }; - } - static H: HType; static DuplicateError: typeof DuplicateError; static ModuleMap: typeof HasteModuleMap; @@ -1129,6 +1119,16 @@ class DuplicateError extends Error { } } +function createEmptyMap(): InternalHasteMap { + return { + clocks: new Map(), + duplicates: new Map(), + files: new Map(), + map: new Map(), + mocks: new Map(), + }; +} + function copy>(object: T): T { return Object.assign(Object.create(null), object); } From c824576a46b5ef06f80e23d686605568c72d7a54 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 10 Aug 2019 20:36:10 +0300 Subject: [PATCH 2/4] simplify: read does not throw, rewrite _buildFileMap to be simpler, remove async/await --- packages/jest-haste-map/src/index.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index 103ed5eb250c..ac815b45ba7b 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -410,18 +410,12 @@ class HasteMap extends EventEmitter { /** * 2. crawl the file system. */ - private async _buildFileMap(): Promise<{ + private _buildFileMap(): Promise<{ removedFiles: FileData; changedFiles?: FileData; hasteMap: InternalHasteMap; }> { - let hasteMap: InternalHasteMap; - try { - const read = this._options.resetCache ? createEmptyMap : this.read; - hasteMap = await read.call(this); - } catch { - hasteMap = createEmptyMap(); - } + const hasteMap = this._options.resetCache ? createEmptyMap() : this.read(); return this._crawl(hasteMap); } From bbdf1adb88e93635bfa35697dc732b92d38f8956 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 10 Aug 2019 20:51:47 +0300 Subject: [PATCH 3/4] simplify: dont use async/await in _buildHasteMap method, I think the code flow is cleaner now --- packages/jest-haste-map/src/index.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index ac815b45ba7b..d0c755b2a7ed 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -636,7 +636,7 @@ class HasteMap extends EventEmitter { .then(workerReply, workerError); } - private async _buildHasteMap(data: { + private _buildHasteMap(data: { removedFiles: FileData; changedFiles?: FileData; hasteMap: InternalHasteMap; @@ -681,16 +681,17 @@ class HasteMap extends EventEmitter { } } - try { - await Promise.all(promises); - this._cleanup(); - hasteMap.map = map; - hasteMap.mocks = mocks; - return hasteMap; - } catch (error) { - this._cleanup(); - throw error; - } + return Promise.all(promises) + .then(() => { + this._cleanup(); + hasteMap.map = map; + hasteMap.mocks = mocks; + return hasteMap; + }) + .catch(error => { + this._cleanup(); + throw error; + }); } private _cleanup() { From b3e672e1b97b9adaf7148a9571808ff006d90b93 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeev Date: Sat, 10 Aug 2019 22:29:04 +0300 Subject: [PATCH 4/4] refactor: build method split for readability + remove async/await --- packages/jest-haste-map/src/index.ts | 81 +++++++++++++++------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index d0c755b2a7ed..8340576df5bc 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -341,43 +341,7 @@ class HasteMap extends EventEmitter { build(): Promise { if (!this._buildPromise) { - this._buildPromise = (async () => { - const data = await this._buildFileMap(); - - // Persist when we don't know if files changed (changedFiles undefined) - // or when we know a file was changed or deleted. - let hasteMap: InternalHasteMap; - if ( - data.changedFiles === undefined || - data.changedFiles.size > 0 || - data.removedFiles.size > 0 - ) { - hasteMap = await this._buildHasteMap(data); - this._persist(hasteMap); - } else { - hasteMap = data.hasteMap; - } - - const rootDir = this._options.rootDir; - const hasteFS = new HasteFS({ - files: hasteMap.files, - rootDir, - }); - const moduleMap = new HasteModuleMap({ - duplicates: hasteMap.duplicates, - map: hasteMap.map, - mocks: hasteMap.mocks, - rootDir, - }); - const __hasteMapForTest = - (process.env.NODE_ENV === 'test' && hasteMap) || null; - await this._watch(hasteMap); - return { - __hasteMapForTest, - hasteFS, - moduleMap, - }; - })(); + this._buildPromise = this._runBuild(); } return this._buildPromise; } @@ -636,6 +600,48 @@ class HasteMap extends EventEmitter { .then(workerReply, workerError); } + private _runBuild(): Promise { + let hasteMap: InternalHasteMap; + return this._buildFileMap() + .then(data => { + // Persist when we don't know if files changed (changedFiles undefined) + // or when we know a file was changed or deleted. + if ( + data.changedFiles === undefined || + data.changedFiles.size > 0 || + data.removedFiles.size > 0 + ) { + return this._buildHasteMap(data); + } else { + return data.hasteMap; + } + }) + .then(map => { + hasteMap = map; + return this._watch(hasteMap); + }) + .then(() => { + const rootDir = this._options.rootDir; + const hasteFS = new HasteFS({ + files: hasteMap.files, + rootDir, + }); + const moduleMap = new HasteModuleMap({ + duplicates: hasteMap.duplicates, + map: hasteMap.map, + mocks: hasteMap.mocks, + rootDir, + }); + const __hasteMapForTest = + (process.env.NODE_ENV === 'test' && hasteMap) || null; + return { + __hasteMapForTest, + hasteFS, + moduleMap, + }; + }); + } + private _buildHasteMap(data: { removedFiles: FileData; changedFiles?: FileData; @@ -686,6 +692,7 @@ class HasteMap extends EventEmitter { this._cleanup(); hasteMap.map = map; hasteMap.mocks = mocks; + this._persist(hasteMap); return hasteMap; }) .catch(error => {