Unverified Commit 15dbb481 authored by Josh Story's avatar Josh Story Committed by GitHub

Avoid triggering memory leak false positive with makeHangingPromise (#71579)

Implements this in a simpler manner with just { once: true } on the
event registration
parent f71829b0
...@@ -10,29 +10,17 @@ export function makeHangingPromise<T>( ...@@ -10,29 +10,17 @@ export function makeHangingPromise<T>(
expression: string expression: string
): Promise<T> { ): Promise<T> {
const hangingPromise = new Promise<T>((_, reject) => { const hangingPromise = new Promise<T>((_, reject) => {
function abort() { signal.addEventListener(
reject( 'abort',
new Error( () => {
`During prerendering, ${expression} rejects when the prerender is complete. Typically these errors are handled by React but if you move ${expression} to a different context by using \`setTimeout\`, \`unstable_after\`, or similar functions you may observe this error and you should handle it in that context.` reject(
new Error(
`During prerendering, ${expression} rejects when the prerender is complete. Typically these errors are handled by React but if you move ${expression} to a different context by using \`setTimeout\`, \`unstable_after\`, or similar functions you may observe this error and you should handle it in that context.`
)
) )
) },
} { once: true }
)
if (signal.aborted) {
abort()
return
}
let listeners = listenersForSignal.get(signal)
if (listeners) {
listeners.push(abort)
} else {
listeners = [abort]
listenersForSignal.set(signal, listeners)
signal.addEventListener('abort', onAbort.bind(null, listeners), {
once: true,
})
}
}) })
// We are fine if no one actually awaits this promise. We shouldn't consider this an unhandled rejection so // We are fine if no one actually awaits this promise. We shouldn't consider this an unhandled rejection so
// we attach a noop catch handler here to suppress this warning. If you actually await somewhere or construct // we attach a noop catch handler here to suppress this warning. If you actually await somewhere or construct
...@@ -42,12 +30,3 @@ export function makeHangingPromise<T>( ...@@ -42,12 +30,3 @@ export function makeHangingPromise<T>(
} }
function ignoreReject() {} function ignoreReject() {}
function onAbort(listeners: Array<AbortListener>) {
for (let i = 0; i < listeners.length; i++) {
listeners[i]()
}
}
type AbortListener = () => void
const listenersForSignal = new WeakMap<AbortSignal, Array<AbortListener>>()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment