Skip to content

Commit e57a2c6

Browse files
Merge pull request #466 from splitio/fme-12059-promises
[FME-12059] Update readiness promises with `SdkReadyMetadata` object
2 parents 2ce34d1 + 8674fdf commit e57a2c6

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

src/readiness/__tests__/sdkReadinessManager.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,20 @@ describe('SDK Readiness Manager - Promises', () => {
220220

221221
// make the SDK ready from cache
222222
sdkReadinessManager.readinessManager.splits.emit(SDK_SPLITS_CACHE_LOADED, { initialCacheLoad: false, lastUpdateTimestamp: null });
223-
expect(await sdkReadinessManager.sdkStatus.whenReadyFromCache()).toBe(false);
223+
expect(await sdkReadinessManager.sdkStatus.whenReadyFromCache()).toEqual({ initialCacheLoad: false, lastUpdateTimestamp: null });
224224

225225
// validate error log for SDK_READY_FROM_CACHE
226226
expect(loggerMock.error).not.toBeCalled();
227-
sdkReadinessManager.readinessManager.gate.on(SDK_READY_FROM_CACHE, () => {});
227+
sdkReadinessManager.readinessManager.gate.on(SDK_READY_FROM_CACHE, () => { });
228228
expect(loggerMock.error).toBeCalledWith(ERROR_CLIENT_LISTENER, ['SDK_READY_FROM_CACHE']);
229229

230230
const readyFromCache = sdkReadinessManager.sdkStatus.whenReadyFromCache();
231231
const ready = sdkReadinessManager.sdkStatus.whenReady();
232232

233233
// make the SDK ready
234234
emitReadyEvent(sdkReadinessManager.readinessManager);
235-
expect(await sdkReadinessManager.sdkStatus.whenReadyFromCache()).toBe(true);
235+
expect(await sdkReadinessManager.sdkStatus.whenReadyFromCache()).toEqual({ initialCacheLoad: false, lastUpdateTimestamp: null });
236+
expect(await sdkReadinessManager.sdkStatus.whenReady()).toEqual({ initialCacheLoad: false, lastUpdateTimestamp: null });
236237

237238
let testPassedCount = 0;
238239
function incTestPassedCount() { testPassedCount++; }
@@ -260,12 +261,12 @@ describe('SDK Readiness Manager - Promises', () => {
260261
function incTestPassedCount() { testPassedCount++; }
261262
function throwTestFailed() { throw new Error('It should rejected, not resolved.'); }
262263

263-
await readyFromCacheForTimeout.then(throwTestFailed,incTestPassedCount);
264-
await readyForTimeout.then(throwTestFailed,incTestPassedCount);
264+
await readyFromCacheForTimeout.then(throwTestFailed, incTestPassedCount);
265+
await readyForTimeout.then(throwTestFailed, incTestPassedCount);
265266

266267
// any subsequent call to .whenReady() and .whenReadyFromCache() must be a rejected promise until the SDK is ready
267-
await sdkReadinessManagerForTimedout.sdkStatus.whenReadyFromCache().then(throwTestFailed,incTestPassedCount);
268-
await sdkReadinessManagerForTimedout.sdkStatus.whenReady().then(throwTestFailed,incTestPassedCount);
268+
await sdkReadinessManagerForTimedout.sdkStatus.whenReadyFromCache().then(throwTestFailed, incTestPassedCount);
269+
await sdkReadinessManagerForTimedout.sdkStatus.whenReady().then(throwTestFailed, incTestPassedCount);
269270

270271
// make the SDK ready
271272
emitReadyEvent(sdkReadinessManagerForTimedout.readinessManager);

src/readiness/readinessManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ export function readinessManagerFactory(
172172
hasTimedout() { return hasTimedout; },
173173
isDestroyed() { return isDestroyed; },
174174
isOperational() { return (isReady || isReadyFromCache) && !isDestroyed; },
175-
lastUpdate() { return lastUpdate; }
175+
lastUpdate() { return lastUpdate; },
176+
metadataReady() { return metadataReady; }
176177
};
177178

178179
}

src/readiness/sdkReadinessManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ export function sdkReadinessManagerFactory(
120120
},
121121

122122
whenReady() {
123-
return new Promise<void>((resolve, reject) => {
123+
return new Promise<SplitIO.SdkReadyMetadata>((resolve, reject) => {
124124
if (readinessManager.isReady()) {
125-
resolve();
125+
resolve(readinessManager.metadataReady());
126126
} else if (readinessManager.hasTimedout()) {
127127
reject(TIMEOUT_ERROR);
128128
} else {
@@ -133,13 +133,13 @@ export function sdkReadinessManagerFactory(
133133
},
134134

135135
whenReadyFromCache() {
136-
return new Promise<boolean>((resolve, reject) => {
136+
return new Promise<SplitIO.SdkReadyMetadata>((resolve, reject) => {
137137
if (readinessManager.isReadyFromCache()) {
138-
resolve(readinessManager.isReady());
138+
resolve(readinessManager.metadataReady());
139139
} else if (readinessManager.hasTimedout()) {
140140
reject(TIMEOUT_ERROR);
141141
} else {
142-
readinessManager.gate.once(SDK_READY_FROM_CACHE, () => resolve(readinessManager.isReady()));
142+
readinessManager.gate.once(SDK_READY_FROM_CACHE, resolve);
143143
readinessManager.gate.once(SDK_READY_TIMED_OUT, () => reject(TIMEOUT_ERROR));
144144
}
145145
});

src/readiness/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface IReadinessManager {
6060
isDestroyed(): boolean,
6161
isOperational(): boolean,
6262
lastUpdate(): number,
63+
metadataReady(): SplitIO.SdkReadyMetadata,
6364

6465
timeout(): void,
6566
setDestroyed(): void,

types/splitio.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -831,18 +831,19 @@ declare namespace SplitIO {
831831
* As it's meant to provide similar flexibility than event listeners, given that the SDK might be ready after a timeout event, the `whenReady` method will return a resolved promise once the SDK is ready.
832832
* You must handle the promise rejection to avoid an unhandled promise rejection error, or set the `startup.readyTimeout` configuration option to 0 to avoid the timeout and thus the rejection.
833833
*
834-
* @returns A promise that resolves once the SDK_READY event is emitted or rejects if the SDK has timedout.
834+
* @returns A promise that resolves once the SDK_READY event is emitted or rejects if the SDK has timedout. The promise resolves with a metadata object that contains the `initialCacheLoad` property,
835+
* which indicates whether the SDK_READY event was emitted together with the SDK_READY_FROM_CACHE event (i.e., fresh install/first app launch) or not (warm cache/subsequent app launch).
835836
*/
836-
whenReady(): Promise<void>;
837+
whenReady(): Promise<SdkReadyMetadata>;
837838
/**
838839
* Returns a promise that resolves when 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).
839840
* As it's meant to provide similar flexibility than event listeners, given that the SDK might be ready from cache after a timeout event, the `whenReadyFromCache` method will return a resolved promise once the SDK is ready from cache.
840841
* You must handle the promise rejection to avoid an unhandled promise rejection error, or set the `startup.readyTimeout` configuration option to 0 to avoid the timeout and thus the rejection.
841842
*
842-
* @returns A promise that resolves once the SDK_READY_FROM_CACHE event is emitted or rejects if the SDK has timedout. The promise resolves with a boolean value that
843-
* indicates whether the SDK_READY_FROM_CACHE event was emitted together with the SDK_READY event (i.e., the SDK is ready and synchronized with the backend) or not.
843+
* @returns A promise that resolves once the SDK_READY_FROM_CACHE event is emitted or rejects if the SDK has timedout. The promise resolves with a metadata object that contains the `initialCacheLoad` property,
844+
* which indicates whether the SDK_READY_FROM_CACHE event was emitted together with the SDK_READY event (i.e., fresh install/first app launch) or not (warm cache/subsequent app launch).
844845
*/
845-
whenReadyFromCache(): Promise<boolean>;
846+
whenReadyFromCache(): Promise<SdkReadyMetadata>;
846847
}
847848
/**
848849
* Common definitions between clients for different environments interface.

0 commit comments

Comments
 (0)