Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a race condition in
@babel/core
(#14843)
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
- Loading branch information
1 parent
a32cf83
commit c45da3d
Showing
5 changed files
with
127 additions
and
85 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
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,31 @@ | ||
import type { Handler } from "gensync"; | ||
|
||
import { isAsync, waitFor } from "./async"; | ||
|
||
export function once<R>(fn: () => Handler<R>): () => Handler<R> { | ||
let result: R; | ||
let resultP: Promise<R>; | ||
return function* () { | ||
if (result) return result; | ||
if (!(yield* isAsync())) return (result = yield* fn()); | ||
if (resultP) return yield* waitFor(resultP); | ||
|
||
let resolve: (result: R) => void, reject: (error: unknown) => void; | ||
resultP = new Promise((res, rej) => { | ||
resolve = res; | ||
reject = rej; | ||
}); | ||
|
||
try { | ||
result = yield* fn(); | ||
// Avoid keeping the promise around | ||
// now that we have the result. | ||
resultP = null; | ||
resolve(result); | ||
return result; | ||
} catch (error) { | ||
reject(error); | ||
throw error; | ||
} | ||
}; | ||
} |
Oops, something went wrong.