Skip to content

Commit a396bcd

Browse files
Merge pull request #484 from splitio/configs-sdk-client
Refactor the client lifecycle methods (`destroy`, `init`, `flush`) for reusability
2 parents 907adcc + 216a9e3 commit a396bcd

2 files changed

Lines changed: 79 additions & 67 deletions

File tree

src/sdkClient/sdkClient.ts

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
import { objectAssign } from '../utils/lang/objectAssign';
22
import SplitIO from '../../types/splitio';
3-
import { releaseApiKey, validateAndTrackApiKey } from '../utils/inputValidation/apiKey';
43
import { clientFactory } from './client';
54
import { clientInputValidationDecorator } from './clientInputValidation';
65
import { ISdkFactoryContext } from '../sdkFactory/types';
7-
8-
const COOLDOWN_TIME_IN_MILLIS = 1000;
6+
import { sdkLifecycleFactory } from './sdkLifecycle';
97

108
/**
119
* Creates an Sdk client, i.e., a base client with status, init, flush and destroy interface
1210
*/
1311
export function sdkClientFactory(params: ISdkFactoryContext, isSharedClient?: boolean): SplitIO.IClient | SplitIO.IAsyncClient {
14-
const { sdkReadinessManager, syncManager, storage, signalListener, settings, telemetryTracker, impressionsTracker } = params;
15-
16-
let hasInit = false;
17-
let lastActionTime = 0;
18-
19-
function __cooldown(func: Function, time: number) {
20-
const now = Date.now();
21-
//get the actual time elapsed in ms
22-
const timeElapsed = now - lastActionTime;
23-
//check if the time elapsed is less than desired cooldown
24-
if (timeElapsed < time) {
25-
//if yes, return message with remaining time in seconds
26-
settings.log.warn(`Flush cooldown, remaining time ${(time - timeElapsed) / 1000} seconds`);
27-
return Promise.resolve();
28-
} else {
29-
//Do the requested action and re-assign the lastActionTime
30-
lastActionTime = now;
31-
return func();
32-
}
33-
}
12+
const { sdkReadinessManager, settings } = params;
3413

35-
function __flush() {
36-
return syncManager ? syncManager.flush() : Promise.resolve();
37-
}
3814

3915
return objectAssign(
4016
// Proto-linkage of the readiness Event Emitter
@@ -48,46 +24,6 @@ export function sdkClientFactory(params: ISdkFactoryContext, isSharedClient?: bo
4824
params.fallbackCalculator
4925
),
5026

51-
{
52-
init() {
53-
if (hasInit) return;
54-
hasInit = true;
55-
56-
if (!isSharedClient) {
57-
validateAndTrackApiKey(settings.log, settings.core.authorizationKey);
58-
sdkReadinessManager.readinessManager.init();
59-
impressionsTracker.start();
60-
syncManager && syncManager.start();
61-
signalListener && signalListener.start();
62-
}
63-
},
64-
65-
flush() {
66-
// @TODO define cooldown time
67-
return __cooldown(__flush, COOLDOWN_TIME_IN_MILLIS);
68-
},
69-
70-
destroy() {
71-
hasInit = false;
72-
// Mark the SDK as destroyed immediately
73-
sdkReadinessManager.readinessManager.destroy();
74-
75-
// For main client, cleanup the SDK Key, listeners and scheduled jobs, and record stat before flushing data
76-
if (!isSharedClient) {
77-
releaseApiKey(settings.core.authorizationKey);
78-
telemetryTracker.sessionLength();
79-
signalListener && signalListener.stop();
80-
impressionsTracker.stop();
81-
}
82-
83-
// Stop background jobs
84-
syncManager && syncManager.stop();
85-
86-
return __flush().then(() => {
87-
// Cleanup storage
88-
return storage.destroy();
89-
});
90-
}
91-
}
27+
sdkLifecycleFactory(params, isSharedClient)
9228
);
9329
}

src/sdkClient/sdkLifecycle.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { releaseApiKey, validateAndTrackApiKey } from '../utils/inputValidation/apiKey';
2+
import { ISdkFactoryContext } from '../sdkFactory/types';
3+
4+
const COOLDOWN_TIME_IN_MILLIS = 1000;
5+
6+
/**
7+
* Creates an Sdk client, i.e., a base client with status, init, flush and destroy interface
8+
*/
9+
export function sdkLifecycleFactory(params: ISdkFactoryContext, isSharedClient?: boolean): { init(): void; flush(): Promise<void>; destroy(): Promise<void> } {
10+
const { sdkReadinessManager, syncManager, storage, signalListener, settings, telemetryTracker, impressionsTracker } = params;
11+
12+
let hasInit = false;
13+
let lastActionTime = 0;
14+
15+
function __cooldown(func: Function, time: number) {
16+
const now = Date.now();
17+
//get the actual time elapsed in ms
18+
const timeElapsed = now - lastActionTime;
19+
//check if the time elapsed is less than desired cooldown
20+
if (timeElapsed < time) {
21+
//if yes, return message with remaining time in seconds
22+
settings.log.warn(`Flush cooldown, remaining time ${(time - timeElapsed) / 1000} seconds`);
23+
return Promise.resolve();
24+
} else {
25+
//Do the requested action and re-assign the lastActionTime
26+
lastActionTime = now;
27+
return func();
28+
}
29+
}
30+
31+
function __flush() {
32+
return syncManager ? syncManager.flush() : Promise.resolve();
33+
}
34+
35+
return {
36+
init() {
37+
if (hasInit) return;
38+
hasInit = true;
39+
40+
if (!isSharedClient) {
41+
validateAndTrackApiKey(settings.log, settings.core.authorizationKey);
42+
sdkReadinessManager.readinessManager.init();
43+
impressionsTracker.start();
44+
syncManager && syncManager.start();
45+
signalListener && signalListener.start();
46+
}
47+
},
48+
49+
flush() {
50+
// @TODO define cooldown time
51+
return __cooldown(__flush, COOLDOWN_TIME_IN_MILLIS);
52+
},
53+
54+
destroy() {
55+
hasInit = false;
56+
// Mark the SDK as destroyed immediately
57+
sdkReadinessManager.readinessManager.destroy();
58+
59+
// For main client, cleanup the SDK Key, listeners and scheduled jobs, and record stat before flushing data
60+
if (!isSharedClient) {
61+
releaseApiKey(settings.core.authorizationKey);
62+
telemetryTracker.sessionLength();
63+
signalListener && signalListener.stop();
64+
impressionsTracker.stop();
65+
}
66+
67+
// Stop background jobs
68+
syncManager && syncManager.stop();
69+
70+
return __flush().then(() => {
71+
// Cleanup storage
72+
return storage.destroy();
73+
});
74+
}
75+
};
76+
}

0 commit comments

Comments
 (0)