-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: start tracking module resolution as soon as possible for easier …
…tracking (#2560) * fix: start tracking module resolution as soon as possible for easier tracking * chore: reduce normalization overhead
- Loading branch information
1 parent
ef77dcc
commit 9f41edd
Showing
3 changed files
with
43 additions
and
19 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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import { getWorkerState } from './global' | ||
import { setTimeout } from './timers' | ||
|
||
export async function waitForImportsToResolve(tries = 0) { | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
function waitNextTick() { | ||
return new Promise(resolve => setTimeout(resolve, 0)) | ||
} | ||
|
||
export async function waitForImportsToResolve() { | ||
await waitNextTick() | ||
const state = getWorkerState() | ||
const promises: Promise<unknown>[] = [] | ||
let resolvingCount = 0 | ||
for (const mod of state.moduleCache.values()) { | ||
if (mod.promise && !mod.evaluated) | ||
promises.push(mod.promise) | ||
if (mod.resolving) | ||
resolvingCount++ | ||
} | ||
if (!promises.length && tries >= 3) | ||
if (!promises.length && !resolvingCount) | ||
return | ||
await Promise.allSettled(promises) | ||
// wait until the end of the loop, so `.then` on modules is called, | ||
// like in import('./example').then(...) | ||
// also call dynamicImportSettled again in case new imports were added | ||
await new Promise(resolve => setTimeout(resolve, 1)) | ||
.then(() => Promise.resolve()) | ||
.then(() => waitForImportsToResolve(tries + 1)) | ||
await waitForImportsToResolve() | ||
} |