Skip to content

Commit

Permalink
fix infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
gaojude committed Feb 6, 2025
1 parent b966d7c commit 1549051
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ export function getErrorByType(
runtime: true,
error: event.reason,
// createMemoizedPromise dedups calls to getOriginalStackFrames
frames: createMemoizedPromise(async () =>
getOriginalStackFrames(
event.frames,
getErrorSource(event.reason),
isAppDir
)
frames: createMemoizedPromise(
async () =>
getOriginalStackFrames(
event.frames,
getErrorSource(event.reason),
isAppDir
),
event
),
}
if (event.type === ACTION_UNHANDLED_ERROR) {
Expand All @@ -52,19 +54,21 @@ export function getErrorByType(
}

function createMemoizedPromise<T>(
promiseFactory: () => Promise<T>
promiseFactory: () => Promise<T>,
key: object
): () => Promise<T> {
let cachedPromise: Promise<T> | null = null
const cache = new Map<object, Promise<T>>()

return function (): Promise<T> {
// If no promise is cached, create one.
if (!cachedPromise) {
cachedPromise = promiseFactory().catch((error: Error) => {
// Clear the cache on failure so that subsequent calls can retry.
cachedPromise = null
// If no promise is cached for this key, create one
if (!cache.has(key)) {
const promise = promiseFactory().catch((error: Error) => {
// Clear the cache on failure so that subsequent calls can retry
cache.delete(key)
throw error
})
cache.set(key, promise)
}
return cachedPromise
return cache.get(key)!
}
}

0 comments on commit 1549051

Please sign in to comment.