Skip to content

Commit

Permalink
fix(bindings): Fix handlers for subscriptions receiving null (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed May 3, 2024
1 parent 0204e04 commit 9272cef
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changeset/famous-deers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@urql/preact': patch
'@urql/svelte': patch
'urql': patch
'@urql/vue': patch
---

Fix subscription handlers to not receive `null` values.
2 changes: 1 addition & 1 deletion packages/preact-urql/src/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export function useSubscription<
const { current: handler } = handlerRef;
// If a handler has been passed, it's used to merge new data in
const data =
partial.data !== undefined
partial.data != null
? typeof handler === 'function'
? handler(result.data, partial.data)
: partial.data
Expand Down
8 changes: 6 additions & 2 deletions packages/react-urql/src/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,14 @@ export function useSubscription<
deferDispatch(setState, state => {
const nextResult = computeNextState(state[1], result);
if (state[1] === nextResult) return state;
if (handlerRef.current && state[1].data !== nextResult.data) {
if (
handlerRef.current &&
nextResult.data != null &&
state[1].data !== nextResult.data
) {
nextResult.data = handlerRef.current(
state[1].data,
nextResult.data!
nextResult.data
) as any;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-urql/src/subscriptionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function subscriptionStore<
),
scan((result: OperationResultState<Result, Variables>, partial) => {
const data =
partial.data !== undefined
partial.data != null
? typeof handler === 'function'
? handler(result.data, partial.data)
: partial.data
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-urql/src/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ export function callUseSubscription<
subscribe(result => {
fetching.value = true;
data.value =
result.data !== undefined
result.data != null
? typeof scanHandler.value === 'function'
? scanHandler.value(data.value as any, result.data!)
? scanHandler.value(data.value as any, result.data)
: result.data
: (result.data as any);
error.value = result.error;
Expand Down

0 comments on commit 9272cef

Please sign in to comment.