Skip to content

Commit

Permalink
Allow importing a loading module multiple times
Browse files Browse the repository at this point in the history
Deadlocks may result though.

Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Aug 17, 2023
1 parent 10a444a commit 47c5f65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-sloths-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"obsidian-modules": minor
---

Allow importing a loading module multiple times. (Deadlocks may result though.)
14 changes: 7 additions & 7 deletions sources/require/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
attachFunctionSourceMap,
launderUnchecked,
patchWindows,
promisePromise,
} from "@polyipseity/obsidian-plugin-library"
import {
CompositeResolve,
Expand Down Expand Up @@ -223,8 +224,10 @@ function createRequire(
cache0(cache, key, constant(value))
return value
}
cache0(cache, key, () => { throw new Error(id) })
try {
const aloader = promisePromise<unknown>()
cache0(cache, key, async () => (await aloader).promise)
const loader = await aloader
loader.resolve(async () => {
preload(cleanup, rd, context)
const prefix =
key === "esModuleWithCommonJS"
Expand Down Expand Up @@ -380,12 +383,9 @@ function createRequire(
} satisfies Required<Omit<ProxyHandler<typeof exports
>, "apply" | "construct">>)
}
cache0(cache, key, constant(ret2))
return ret2
} catch (error) {
cache0(cache, key, () => { throw error })
throw error
}
})
return await loader.promise
} finally {
cleanup.call()
}
Expand Down

0 comments on commit 47c5f65

Please sign in to comment.