-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reliable support for conditional imports
I was struggling with dynamic imports for a while before I realized I was actually working on the halting problem. This is hinted at in the top-level await proposal: https://github.com/tc39/proposal-top-level-await#rejected-deadlock-prevention-mechanisms Instead, we treat resolved dynamic imports basically as static imports, and they will participate in the cyclic resolution algorithm. Since dynamic imports in practice tend to just be well-behaved conditional or lazy imports this works out well. This are probably some races here if a new dynamic import is dispatched while an update is processing but haven't actually run into it myself.
- Loading branch information
Showing
10 changed files
with
726 additions
and
530 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable @typescript-eslint/restrict-template-expressions */ | ||
import { expect, test } from "@jest/globals"; | ||
import { UpdateStatus } from "../runtime/controller.js"; | ||
import { TestModule } from "./__fixtures__/module.js"; | ||
|
||
test("dynamic import with error throws", async () => { | ||
const main = new TestModule(() => | ||
`const module = await import(${error});`); | ||
const error = new TestModule(() => | ||
"throw new Error()"); | ||
await expect(main.dispatch()).rejects.toThrow(); | ||
}); | ||
|
||
test("lazy dynamic import with error is recoverable", async () => { | ||
const main = new TestModule(() => | ||
`const module = import(${error}); | ||
await module.catch(() => {}); | ||
import.meta.hot.accept(${error});`); | ||
const error = new TestModule(() => | ||
"throw new Error()"); | ||
await main.dispatch(); | ||
await error.update(() => ""); | ||
const result = await main.releaseUpdate(); | ||
expect(result?.type).toBe(UpdateStatus.success); | ||
}); |
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
Oops, something went wrong.