Skip to content

Commit dfccaf7

Browse files
author
James Fox
committed
cleanup usage of dataReadyPromise
1 parent 4734ead commit dfccaf7

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
184184
success: false,
185185
reason:
186186
'failed to initialize onReady before timeout, either the datafile or user info was not set before the timeout',
187+
dataReadyPromise: this.dataReadyPromise
187188
})
188189
}, timeout) as any
189190
})
190191

191192
return Promise.race([this.dataReadyPromise, timeoutPromise]).then(res => {
192193
clearTimeout(timeoutId)
193-
return Object.assign(res, { dataReadyPromise: this.dataReadyPromise });
194+
return res
194195
})
195196
}
196197

src/hooks.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ interface UseFeature {
5757
/**
5858
* A React Hook that retrieves the status of a feature flag and its variables, optionally
5959
* auto updating those values based on underlying user or datafile changes.
60+
*
61+
* Note: The react client can become ready AFTER the timeout period.
62+
* ClientReady and DidTimeout provide signals to handle this scenario.
6063
*/
6164
export const useFeature : UseFeature = (featureKey, options = {}, overrides = {}) => {
6265
const { isServerSide, optimizely, timeout } = useContext(OptimizelyContext);
@@ -89,24 +92,23 @@ export const useFeature : UseFeature = (featureKey, options = {}, overrides = {}
8992
optimizely.onReady({ timeout: finalReadyTimeout }).then((res: OnReadyResult) => {
9093
if (res.success) {
9194
// didTimeout=false
92-
setClientReady(true);
9395
useFeatureLogger.info(`feature="${featureKey}" successfully set for user="${optimizely.user.id}"`);
9496
return;
95-
} else {
96-
setDidTimeout(true);
97-
useFeatureLogger.info(
98-
`feature="${featureKey}" could not be set before timeout of ${finalReadyTimeout}ms, reason="${res.reason || ''}"`,
99-
)
100-
return res.dataReadyPromise!.then(
101-
() => {
102-
setClientReady(true);
103-
useFeatureLogger.info(
104-
`feature="${featureKey}" is now set, but after timeout.`,
105-
);
106-
});
10797
}
98+
setDidTimeout(true);
99+
useFeatureLogger.info(
100+
`feature="${featureKey}" could not be set before timeout of ${finalReadyTimeout}ms, reason="${res.reason || ''}"`,
101+
);
102+
// Since we timed out, wait for the dataReadyPromise to resolve before setting up.
103+
return res.dataReadyPromise!.then(
104+
() => {
105+
useFeatureLogger.info(
106+
`feature="${featureKey}" is now set, but after timeout.`,
107+
);
108+
});
108109
})
109110
.then(() => {
111+
setClientReady(true);
110112
setData(getCurrentValues());
111113
if (options.autoUpdate) {
112114
cleanupFns.push(

0 commit comments

Comments
 (0)