Skip to content

Commit 1eeff81

Browse files
Add method to retrieve client readiness status synchronously
1 parent 3c7dedc commit 1eeff81

File tree

5 files changed

+56
-20
lines changed

5 files changed

+56
-20
lines changed

src/readiness/__tests__/sdkReadinessManager.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('SDK Readiness Manager - Event emitter', () => {
5151
});
5252

5353
expect(typeof sdkStatus.ready).toBe('function'); // The sdkStatus exposes a .ready() function.
54-
expect(typeof sdkStatus.__getStatus).toBe('function'); // The sdkStatus exposes a .__getStatus() function.
55-
expect(sdkStatus.__getStatus()).toEqual({
54+
expect(typeof sdkStatus.getStatus).toBe('function'); // The sdkStatus exposes a .getStatus() function.
55+
expect(sdkStatus.getStatus()).toEqual({
5656
isReady: false, isReadyFromCache: false, isTimedout: false, hasTimedout: false, isDestroyed: false, isOperational: false, lastUpdate: 0
5757
});
5858

src/readiness/sdkReadinessManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export function sdkReadinessManagerFactory(
104104
return readyPromise;
105105
},
106106

107-
__getStatus() {
107+
getStatus() {
108108
return {
109109
isReady: readinessManager.isReady(),
110110
isReadyFromCache: readinessManager.isReadyFromCache(),

src/readiness/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { IStatusInterface } from '../types';
21
import SplitIO from '../../types/splitio';
32

43
/** Splits data emitter */
@@ -72,7 +71,7 @@ export interface IReadinessManager {
7271

7372
export interface ISdkReadinessManager {
7473
readinessManager: IReadinessManager
75-
sdkStatus: IStatusInterface
74+
sdkStatus: SplitIO.IStatusInterface
7675

7776
/**
7877
* Increment internalReadyCbCount, an offset value of SDK_READY listeners that are added/removed internally

src/types.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ export interface ISettings extends SplitIO.ISettings {
1414
readonly initialRolloutPlan?: RolloutPlan;
1515
}
1616

17-
/**
18-
* SplitIO.IStatusInterface interface extended with private properties for internal use
19-
*/
20-
export interface IStatusInterface extends SplitIO.IStatusInterface {
21-
// Expose status for internal purposes only. Not considered part of the public API, and might be updated eventually.
22-
__getStatus(): {
23-
isReady: boolean;
24-
isReadyFromCache: boolean;
25-
isTimedout: boolean;
26-
hasTimedout: boolean;
27-
isDestroyed: boolean;
28-
isOperational: boolean;
29-
lastUpdate: number;
30-
};
31-
}
3217
/**
3318
* SplitIO.IBasicClient interface extended with private properties for internal use
3419
*/

types/splitio.d.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,52 @@ declare namespace SplitIO {
668668
[status in ConsentStatus]: ConsentStatus;
669669
};
670670
}
671+
/**
672+
* Readiness Status interface. It represents the readiness state of an SDK client.
673+
*/
674+
interface ReadinessStatus {
675+
676+
/**
677+
* `isReady` indicates if the client has triggered an `SDK_READY` event and
678+
* thus is ready to evaluate with cached data synchronized with the backend.
679+
*/
680+
isReady: boolean;
681+
682+
/**
683+
* `isReadyFromCache` indicates if the client has triggered an `SDK_READY_FROM_CACHE` event and
684+
* thus is ready to evaluate with cached data, although the data in cache might be stale.
685+
*/
686+
isReadyFromCache: boolean;
687+
688+
/**
689+
* `isTimedout` indicates if the client has triggered an `SDK_READY_TIMED_OUT` event and is not ready to evaluate.
690+
* In other words, `isTimedout` is equivalent to `hasTimedout && !isReady`.
691+
*/
692+
isTimedout: boolean;
693+
694+
/**
695+
* `hasTimedout` indicates if the client has ever triggered an `SDK_READY_TIMED_OUT` event.
696+
* It's meant to keep a reference that the SDK emitted a timeout at some point, not the current state.
697+
*/
698+
hasTimedout: boolean;
699+
700+
/**
701+
* `isDestroyed` indicates if the client has been destroyed, i.e., `destroy` method has been called.
702+
*/
703+
isDestroyed: boolean;
704+
705+
/**
706+
* `isOperational` indicates if the client can evaluate feature flags.
707+
* In this state, `getTreatment` calls will not return `CONTROL` due to the SDK being unready or destroyed.
708+
* It's equivalent to `(isReady || isReadyFromCache) && !isDestroyed`.
709+
*/
710+
isOperational: boolean;
711+
712+
/**
713+
* `lastUpdate` indicates the timestamp of the most recent status event.
714+
*/
715+
lastUpdate: number;
716+
}
671717
/**
672718
* Common API for entities that expose status handlers.
673719
*/
@@ -676,6 +722,12 @@ declare namespace SplitIO {
676722
* Constant object containing the SDK events for you to use.
677723
*/
678724
Event: EventConsts;
725+
/**
726+
* Gets the readiness status.
727+
*
728+
* @returns The current readiness status.
729+
*/
730+
getStatus(): ReadinessStatus;
679731
/**
680732
* 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).
681733
* 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.

0 commit comments

Comments
 (0)