Skip to content

Commit

Permalink
some style adjustments to be more compiler-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jul 3, 2024
1 parent 70f5aaf commit fed117b
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface InternalQueryResult<TData, TVariables extends OperationVariables>
[originalResult]: ApolloQueryResult<TData>;
}

const noop = () => {};
function noop() {}
export const lastWatchOptions = Symbol();

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

if (client !== internalState.client || query !== internalState.query) {
// If the client or query have changed, we need to create a new InternalState.
// This will trigger a re-render with the new state, but it will also continue
// to run the current render function to completion.
// Since we sometimes trigger some side-effects in the render function, we
// re-assign `state` to the new state to ensure that those side-effects are
// triggered with the new state.
const newInternalState = createInternalState(internalState);
updateInternalState(newInternalState);
return [newInternalState, onQueryExecuted] as const;
}

return [internalState, onQueryExecuted] as const;

/**
* Used by `useLazyQuery` when a new query is executed.
* We keep this logic here since it needs to update things in unsafe
Expand All @@ -253,6 +239,20 @@ function useInternalState<
}),
});
}

if (client !== internalState.client || query !== internalState.query) {
// If the client or query have changed, we need to create a new InternalState.
// This will trigger a re-render with the new state, but it will also continue
// to run the current render function to completion.
// Since we sometimes trigger some side-effects in the render function, we
// re-assign `state` to the new state to ensure that those side-effects are
// triggered with the new state.
const newInternalState = createInternalState(internalState);
updateInternalState(newInternalState);
return [newInternalState, onQueryExecuted] as const;
}

return [internalState, onQueryExecuted] as const;
}

export function useQueryInternals<
Expand Down Expand Up @@ -405,8 +405,11 @@ function useObservableSubscriptionResult<
};

const onError = (error: Error) => {
subscription.unsubscribe();
subscription = observable.resubscribeAfterError(onNext, onError);
subscription.current.unsubscribe();
subscription.current = observable.resubscribeAfterError(
onNext,
onError
);

if (!hasOwnProperty.call(error, "graphQLErrors")) {
// The error is not a GraphQL error
Expand Down Expand Up @@ -436,14 +439,19 @@ function useObservableSubscriptionResult<
}
};

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

// Do the "unsubscribe" with a short delay.
// This way, an existing subscription can be reused without an additional
// request if "unsubscribe" and "resubscribe" to the same ObservableQuery
// happen in very fast succession.
return () => {
setTimeout(() => subscription.unsubscribe());
setTimeout(() => subscription.current.unsubscribe());
};
},

Expand Down

0 comments on commit fed117b

Please sign in to comment.