|
| 1 | +/** |
| 2 | + * Copyright 2026, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { useEffect, useState } from 'react'; |
| 18 | +import type { OptimizelyUserContext } from '@optimizely/optimizely-sdk'; |
| 19 | + |
| 20 | +import type { Client } from '@optimizely/optimizely-sdk'; |
| 21 | +import type { ProviderState } from '../provider/index'; |
| 22 | + |
| 23 | +interface AsyncState<TResult> { |
| 24 | + result: TResult; |
| 25 | + error: Error | null; |
| 26 | + isLoading: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Shared async decision state machine used by useDecideAsync, |
| 31 | + * useDecideForKeysAsync, and useDecideAllAsync. |
| 32 | + * |
| 33 | + * Handles: loading state, error propagation, cancellation of stale promises, |
| 34 | + * and redundant re-render avoidance on first mount. |
| 35 | + * |
| 36 | + * @param state - Provider state from useProviderState |
| 37 | + * @param client - Optimizely client instance |
| 38 | + * @param fdVersion - Forced decision version counter (triggers re-evaluation) |
| 39 | + * @param emptyResult - Default/empty result value (null for single, {} for multi) |
| 40 | + * @param execute - Callback that performs the async SDK call |
| 41 | + */ |
| 42 | +export function useAsyncDecision<TResult>( |
| 43 | + state: ProviderState, |
| 44 | + client: Client, |
| 45 | + fdVersion: number, |
| 46 | + emptyResult: TResult, |
| 47 | + execute: (userContext: OptimizelyUserContext) => Promise<TResult> |
| 48 | +): AsyncState<TResult> { |
| 49 | + const [asyncState, setAsyncState] = useState<AsyncState<TResult>>({ |
| 50 | + result: emptyResult, |
| 51 | + error: null, |
| 52 | + isLoading: true, |
| 53 | + }); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + const { userContext, error } = state; |
| 57 | + const hasConfig = client.getOptimizelyConfig() !== null; |
| 58 | + |
| 59 | + // Store-level error — no async call needed |
| 60 | + if (error) { |
| 61 | + setAsyncState({ result: emptyResult, error, isLoading: false }); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Ensure loading state (skip if already loading to avoid re-render) |
| 66 | + setAsyncState((prev) => { |
| 67 | + if (prev.isLoading) return prev; |
| 68 | + return { result: emptyResult, error: null, isLoading: true }; |
| 69 | + }); |
| 70 | + |
| 71 | + // Store not ready — wait for config/user context |
| 72 | + if (!hasConfig || userContext === null) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Store is ready — fire async decision |
| 77 | + let cancelled = false; |
| 78 | + |
| 79 | + execute(userContext).then( |
| 80 | + (result) => { |
| 81 | + if (!cancelled) { |
| 82 | + setAsyncState({ result, error: null, isLoading: false }); |
| 83 | + } |
| 84 | + }, |
| 85 | + (err) => { |
| 86 | + if (!cancelled) { |
| 87 | + setAsyncState({ |
| 88 | + result: emptyResult, |
| 89 | + error: err instanceof Error ? err : new Error(String(err)), |
| 90 | + isLoading: false, |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + return () => { |
| 97 | + cancelled = true; |
| 98 | + }; |
| 99 | + }, [state, fdVersion, client, execute, emptyResult]); |
| 100 | + |
| 101 | + return asyncState; |
| 102 | +} |
0 commit comments