Skip to content

Commit 53cc6db

Browse files
feat: add whenReady and whenReadyFromCache methods to replace deprecated ready method
1 parent cfdd6e6 commit 53cc6db

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

src/readiness/sdkReadinessManager.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ERROR_CLIENT_LISTENER, CLIENT_READY_FROM_CACHE, CLIENT_READY, CLIENT_NO
99

1010
const NEW_LISTENER_EVENT = 'newListener';
1111
const REMOVE_LISTENER_EVENT = 'removeListener';
12+
const TIMEOUT_ERROR = new Error('Split SDK has emitted SDK_READY_TIMED_OUT event.');
1213

1314
/**
1415
* SdkReadinessManager factory, which provides the public status API of SDK clients and manager: ready promise, readiness event emitter and constants (SDK_READY, etc).
@@ -93,17 +94,44 @@ export function sdkReadinessManagerFactory(
9394
SDK_READY_TIMED_OUT,
9495
},
9596

97+
// @TODO: remove in next major
9698
ready() {
9799
if (readinessManager.hasTimedout()) {
98100
if (!readinessManager.isReady()) {
99-
return promiseWrapper(Promise.reject(new Error('Split SDK has emitted SDK_READY_TIMED_OUT event.')), defaultOnRejected);
101+
return promiseWrapper(Promise.reject(TIMEOUT_ERROR), defaultOnRejected);
100102
} else {
101103
return Promise.resolve();
102104
}
103105
}
104106
return readyPromise;
105107
},
106108

109+
whenReady() {
110+
return new Promise<void>((resolve, reject) => {
111+
if (readinessManager.isReady()) {
112+
resolve();
113+
} else if (readinessManager.hasTimedout()) {
114+
reject(TIMEOUT_ERROR);
115+
} else {
116+
readinessManager.gate.once(SDK_READY, resolve);
117+
readinessManager.gate.once(SDK_READY_TIMED_OUT, () => reject(TIMEOUT_ERROR));
118+
}
119+
});
120+
},
121+
122+
whenReadyFromCache() {
123+
return new Promise<void>((resolve, reject) => {
124+
if (readinessManager.isReadyFromCache()) {
125+
resolve();
126+
} else if (readinessManager.hasTimedout()) {
127+
reject(TIMEOUT_ERROR);
128+
} else {
129+
readinessManager.gate.once(SDK_READY_FROM_CACHE, resolve);
130+
readinessManager.gate.once(SDK_READY_TIMED_OUT, () => reject(TIMEOUT_ERROR));
131+
}
132+
});
133+
},
134+
107135
getStatus() {
108136
return {
109137
isReady: readinessManager.isReady(),

types/splitio.d.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,19 @@ declare namespace SplitIO {
525525
*/
526526
type EventConsts = {
527527
/**
528-
* The ready event.
528+
* The ready event emitted once the SDK is ready to evaluate feature flags with cache synchronized with the backend.
529529
*/
530530
SDK_READY: 'init::ready';
531531
/**
532-
* The ready event when fired with cached data.
532+
* The ready event emitted once the SDK is ready to evaluate feature flags with cache that could be stale. Use SDK_READY if you want to be sure the cache is in sync with the backend.
533533
*/
534534
SDK_READY_FROM_CACHE: 'init::cache-ready';
535535
/**
536-
* The timeout event.
536+
* The timeout event emitted after `startup.readyTimeout` seconds if the SDK_READY event was not emitted.
537537
*/
538538
SDK_READY_TIMED_OUT: 'init::timeout';
539539
/**
540-
* The update event.
540+
* The update event emitted when the SDK cache is updated with new data from the backend.
541541
*/
542542
SDK_UPDATE: 'state::update';
543543
};
@@ -704,7 +704,7 @@ declare namespace SplitIO {
704704

705705
/**
706706
* `isReadyFromCache` indicates if the client has triggered an `SDK_READY_FROM_CACHE` event and
707-
* thus is ready to evaluate with cached data, although the data in cache might be stale.
707+
* thus is ready to evaluate with cached data, although the data in cache might be stale, not synchronized with the backend.
708708
*/
709709
isReadyFromCache: boolean;
710710

@@ -728,7 +728,7 @@ declare namespace SplitIO {
728728
/**
729729
* `isOperational` indicates if the client can evaluate feature flags.
730730
* In this state, `getTreatment` calls will not return `CONTROL` due to the SDK being unready or destroyed.
731-
* It's equivalent to `(isReady || isReadyFromCache) && !isDestroyed`.
731+
* It's equivalent to `isReadyFromCache && !isDestroyed`.
732732
*/
733733
isOperational: boolean;
734734

@@ -752,7 +752,7 @@ declare namespace SplitIO {
752752
*/
753753
getStatus(): ReadinessStatus;
754754
/**
755-
* Returns a promise that resolves once the SDK has finished loading (`SDK_READY` event emitted) or rejected if the SDK has timedout (`SDK_READY_TIMED_OUT` event emitted).
755+
* Returns a promise that resolves once the SDK has finished synchronizing with the backend (`SDK_READY` event emitted) or rejected if the SDK has timedout (`SDK_READY_TIMED_OUT` event emitted).
756756
* As it's meant to provide similar flexibility to the event approach, given that the SDK might be eventually ready after a timeout event, the `ready` method will return a resolved promise once the SDK is ready.
757757
*
758758
* Caveats: the method was designed to avoid an unhandled Promise rejection if the rejection case is not handled, so that `onRejected` handler is optional when using promises.
@@ -767,8 +767,23 @@ declare namespace SplitIO {
767767
* ```
768768
*
769769
* @returns A promise that resolves once the SDK is ready or rejects if the SDK has timedout.
770+
* @deprecated Use `whenReady` instead.
770771
*/
771772
ready(): Promise<void>;
773+
/**
774+
* Returns a promise that resolves once the SDK is ready for evaluations using cached data synchronized with the backend (`SDK_READY` event emitted) or rejected if the SDK has timedout (`SDK_READY_TIMED_OUT` event emitted).
775+
* As it's meant to provide similar flexibility to the event approach, given that the SDK might be eventually ready after a timeout event, the `whenReady` method will return a resolved promise once the SDK is ready.
776+
*
777+
* @returns A promise that resolves once the SDK is ready or rejects if the SDK has timedout.
778+
*/
779+
whenReady(): Promise<void>;
780+
/**
781+
* Returns a promise that resolves once the SDK is ready for evaluations using cached data which might not yet be synchronized with the backend (`SDK_READY_FROM_CACHE` event emitted) or rejected if the SDK has timedout (`SDK_READY_TIMED_OUT` event emitted).
782+
* As it's meant to provide similar flexibility to the event approach, given that the SDK might be eventually ready from cache after a timeout event, the `whenReadyFromCache` method will return a resolved promise once the SDK is ready from cache.
783+
*
784+
* @returns A promise that resolves once the SDK is ready from cache or rejects if the SDK has timedout.
785+
*/
786+
whenReadyFromCache(): Promise<void>;
772787
}
773788
/**
774789
* Common definitions between clients for different environments interface.
@@ -1702,7 +1717,7 @@ declare namespace SplitIO {
17021717
* Wait for the SDK client to be ready before calling this method.
17031718
*
17041719
* ```js
1705-
* await factory.client().ready();
1720+
* await factory.client().whenReady();
17061721
* const rolloutPlan = factory.getRolloutPlan();
17071722
* ```
17081723
*

0 commit comments

Comments
 (0)