Skip to content

Commit fed117b

Browse files
committed
some style adjustments to be more compiler-friendly
1 parent 70f5aaf commit fed117b

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/react/hooks/useQuery.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ interface InternalQueryResult<TData, TVariables extends OperationVariables>
7070
[originalResult]: ApolloQueryResult<TData>;
7171
}
7272

73-
const noop = () => {};
73+
function noop() {}
7474
export const lastWatchOptions = Symbol();
7575

7676
export interface ObsQueryWithMeta<TData, TVariables extends OperationVariables>
@@ -213,20 +213,6 @@ function useInternalState<
213213
let [internalState, updateInternalState] =
214214
React.useState(createInternalState);
215215

216-
if (client !== internalState.client || query !== internalState.query) {
217-
// If the client or query have changed, we need to create a new InternalState.
218-
// This will trigger a re-render with the new state, but it will also continue
219-
// to run the current render function to completion.
220-
// Since we sometimes trigger some side-effects in the render function, we
221-
// re-assign `state` to the new state to ensure that those side-effects are
222-
// triggered with the new state.
223-
const newInternalState = createInternalState(internalState);
224-
updateInternalState(newInternalState);
225-
return [newInternalState, onQueryExecuted] as const;
226-
}
227-
228-
return [internalState, onQueryExecuted] as const;
229-
230216
/**
231217
* Used by `useLazyQuery` when a new query is executed.
232218
* We keep this logic here since it needs to update things in unsafe
@@ -253,6 +239,20 @@ function useInternalState<
253239
}),
254240
});
255241
}
242+
243+
if (client !== internalState.client || query !== internalState.query) {
244+
// If the client or query have changed, we need to create a new InternalState.
245+
// This will trigger a re-render with the new state, but it will also continue
246+
// to run the current render function to completion.
247+
// Since we sometimes trigger some side-effects in the render function, we
248+
// re-assign `state` to the new state to ensure that those side-effects are
249+
// triggered with the new state.
250+
const newInternalState = createInternalState(internalState);
251+
updateInternalState(newInternalState);
252+
return [newInternalState, onQueryExecuted] as const;
253+
}
254+
255+
return [internalState, onQueryExecuted] as const;
256256
}
257257

258258
export function useQueryInternals<
@@ -405,8 +405,11 @@ function useObservableSubscriptionResult<
405405
};
406406

407407
const onError = (error: Error) => {
408-
subscription.unsubscribe();
409-
subscription = observable.resubscribeAfterError(onNext, onError);
408+
subscription.current.unsubscribe();
409+
subscription.current = observable.resubscribeAfterError(
410+
onNext,
411+
onError
412+
);
410413

411414
if (!hasOwnProperty.call(error, "graphQLErrors")) {
412415
// The error is not a GraphQL error
@@ -436,14 +439,19 @@ function useObservableSubscriptionResult<
436439
}
437440
};
438441

439-
let subscription = observable.subscribe(onNext, onError);
442+
// TODO evaluate if we keep this in
443+
// React Compiler cannot handle scoped `let` access, but a mutable object
444+
// like this is fine.
445+
// was:
446+
// let subscription = observable.subscribe(onNext, onError);
447+
const subscription = { current: observable.subscribe(onNext, onError) };
440448

441449
// Do the "unsubscribe" with a short delay.
442450
// This way, an existing subscription can be reused without an additional
443451
// request if "unsubscribe" and "resubscribe" to the same ObservableQuery
444452
// happen in very fast succession.
445453
return () => {
446-
setTimeout(() => subscription.unsubscribe());
454+
setTimeout(() => subscription.current.unsubscribe());
447455
};
448456
},
449457

0 commit comments

Comments
 (0)