Skip to content

Commit 291f4ec

Browse files
softgripperTkDodolachlancollins
authored
fix(svelte-query): Add try/catch to getIsRestoringContext (#6193)
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> Co-authored-by: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com>
1 parent 4a29815 commit 291f4ec

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getIsRestoringContext } from '../context'
3+
4+
describe('getIsRestoringContext', () => {
5+
it('Should not throw when called outside of a component', async () => {
6+
expect(() => getIsRestoringContext()).to.not.throw()
7+
})
8+
})

packages/svelte-query/src/context.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ const _isRestoringContextKey = '$$_isRestoring'
2626

2727
/** Retrieves a `isRestoring` from Svelte's context */
2828
export const getIsRestoringContext = (): Readable<boolean> => {
29-
const isRestoring = getContext(_isRestoringContextKey)
30-
if (!isRestoring) return readable(false)
31-
return isRestoring as Readable<boolean>
29+
try {
30+
const isRestoring = getContext<Readable<boolean> | undefined>(
31+
_isRestoringContextKey,
32+
)
33+
return isRestoring ? isRestoring : readable(false)
34+
} catch (error) {
35+
return readable(false)
36+
}
3237
}
3338

3439
/** Sets a `isRestoring` on Svelte's context */

0 commit comments

Comments
 (0)