Skip to content

Commit

Permalink
fix: only clear the React stream/shape cache when the previous stream…
Browse files Browse the repository at this point in the history
…/shape was aborted (#2092)

The current behavior also clears them when there's an error which leads
to an infinite loop with a bad stream being recreated over and over.
  • Loading branch information
KyleAMathews authored Dec 3, 2024
1 parent ab1ea51 commit 67a9347
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-falcons-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@electric-sql/react": patch
---

fix: only clear the React stream/shape cache when the previous stream/shape was aborted
10 changes: 4 additions & 6 deletions packages/react-hooks/src/react-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,16 @@ export function getShapeStream<T extends Row<unknown>>(
// If the stream is already cached, return it if valid
if (streamCache.has(shapeHash)) {
const stream = streamCache.get(shapeHash)! as ShapeStream<T>
if (!stream.error && !stream.options.signal?.aborted) {
if (!stream.options.signal?.aborted) {
return stream
}

// if stream is cached but errored/aborted, remove it and related shapes
// if stream is aborted, remove it and related shapes
streamCache.delete(shapeHash)
shapeCache.delete(stream)
}

const newShapeStream = new ShapeStream<T>(options)

streamCache.set(shapeHash, newShapeStream)

// Return the created shape
Expand All @@ -76,17 +75,16 @@ export function getShape<T extends Row<unknown>>(
): Shape<T> {
// If the stream is already cached, return it if valid
if (shapeCache.has(shapeStream)) {
if (!shapeStream.error && !shapeStream.options.signal?.aborted) {
if (!shapeStream.options.signal?.aborted) {
return shapeCache.get(shapeStream)! as Shape<T>
}

// if stream is cached but errored/aborted, remove it and related shapes
// if stream is aborted, remove it and related shapes
streamCache.delete(sortedOptionsHash(shapeStream.options))
shapeCache.delete(shapeStream)
}

const newShape = new Shape<T>(shapeStream)

shapeCache.set(shapeStream, newShape)

// Return the created shape
Expand Down

0 comments on commit 67a9347

Please sign in to comment.