-
Notifications
You must be signed in to change notification settings - Fork 37
[FSSDK-12296] Async Hook implementation #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c967727
bfe2941
b54e1e2
d2b8c8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,104 @@ | ||||||||||||
| /** | ||||||||||||
| * Copyright 2026, Optimizely | ||||||||||||
| * | ||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||
| * You may obtain a copy of the License at | ||||||||||||
| * | ||||||||||||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| * | ||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||
| * limitations under the License. | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| import { useEffect, useState } from 'react'; | ||||||||||||
| import type { OptimizelyUserContext } from '@optimizely/optimizely-sdk'; | ||||||||||||
|
|
||||||||||||
| import type { Client } from '@optimizely/optimizely-sdk'; | ||||||||||||
| import type { ProviderState } from '../provider/index'; | ||||||||||||
|
|
||||||||||||
| interface AsyncState<TResult> { | ||||||||||||
| result: TResult; | ||||||||||||
| error: Error | null; | ||||||||||||
| isLoading: boolean; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Shared async decision state machine used by useDecideAsync, | ||||||||||||
| * useDecideForKeysAsync, and useDecideAllAsync. | ||||||||||||
| * | ||||||||||||
| * Handles: loading state, error propagation, cancellation of stale promises, | ||||||||||||
| * and redundant re-render avoidance on first mount. | ||||||||||||
| * | ||||||||||||
| * @param state - Provider state from useProviderState | ||||||||||||
| * @param client - Optimizely client instance | ||||||||||||
| * @param fdVersion - Forced decision version counter (triggers re-evaluation) | ||||||||||||
| * @param emptyResult - Default/empty result value (null for single, {} for multi) | ||||||||||||
| * @param execute - Callback that performs the async SDK call | ||||||||||||
| */ | ||||||||||||
| export function useAsyncDecision<TResult>( | ||||||||||||
| state: ProviderState, | ||||||||||||
| client: Client, | ||||||||||||
| fdVersion: number, | ||||||||||||
| emptyResult: TResult, | ||||||||||||
| execute: (userContext: OptimizelyUserContext) => Promise<TResult> | ||||||||||||
| ): AsyncState<TResult> { | ||||||||||||
| const [asyncState, setAsyncState] = useState<AsyncState<TResult>>({ | ||||||||||||
| result: emptyResult, | ||||||||||||
| error: null, | ||||||||||||
| isLoading: true, | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| useEffect(() => { | ||||||||||||
| const { userContext, error } = state; | ||||||||||||
| const hasConfig = client.getOptimizelyConfig() !== null; | ||||||||||||
|
|
||||||||||||
| // Store-level error — no async call needed | ||||||||||||
| if (error) { | ||||||||||||
| setAsyncState({ result: emptyResult, error, isLoading: false }); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Store not ready — stay in loading | ||||||||||||
| if (!hasConfig || userContext === null) { | ||||||||||||
| setAsyncState({ result: emptyResult, error: null, isLoading: true }); | ||||||||||||
|
||||||||||||
| setAsyncState({ result: emptyResult, error: null, isLoading: true }); | |
| setAsyncState((prev) => { | |
| if (prev.isLoading && prev.error === null && prev.result === emptyResult) return prev; | |
| return { result: emptyResult, error: null, isLoading: true }; | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The async hooks rely on
as UseDecide*AsyncResulttype assertions when returning{ result, error, isLoading }, which can mask violations of the intended discriminated union (e.g., accidentally returningisLoading: truewith a non-nullerrorafter future refactors). Consider makinguseAsyncDecisionreturn a discriminated union (or a helper that maps its internal state into the union) so TypeScript enforces the invariants and the hooks can return without assertions.