-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsdkClient.ts
More file actions
86 lines (72 loc) · 2.88 KB
/
sdkClient.ts
File metadata and controls
86 lines (72 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { objectAssign } from '../utils/lang/objectAssign';
import { IStatusInterface, SplitIO } from '../types';
import { areAllClientDestroyed, releaseApiKey } from '../utils/inputValidation/apiKey';
import { clientFactory } from './client';
import { clientInputValidationDecorator } from './clientInputValidation';
import { ISdkFactoryContext } from '../sdkFactory/types';
const COOLDOWN_TIME_IN_MILLIS = 1000;
/**
* Creates an Sdk client, i.e., a base client with status and destroy interface
*/
export function sdkClientFactory(params: ISdkFactoryContext): SplitIO.IClient | SplitIO.IAsyncClient {
const { clients, sdkReadinessManager, syncManager, mySegmentsSyncManager, storage, signalListener, settings, telemetryTracker, uniqueKeysTracker } = params;
let destroyPromise: Promise<void> | undefined;
let lastActionTime = 0;
function __cooldown(func: Function, time: number) {
const now = Date.now();
//get the actual time elapsed in ms
const timeElapsed = now - lastActionTime;
//check if the time elapsed is less than desired cooldown
if (timeElapsed < time) {
//if yes, return message with remaining time in seconds
settings.log.warn(`Flush cooldown, remaining time ${(time - timeElapsed) / 1000} seconds`);
return Promise.resolve();
} else {
//Do the requested action and re-assign the lastActionTime
lastActionTime = now;
return func();
}
}
function __flush() {
return syncManager ? syncManager.flush() : Promise.resolve();
}
return objectAssign(
// Proto-linkage of the readiness Event Emitter
Object.create(sdkReadinessManager.sdkStatus) as IStatusInterface,
// Client API (getTreatment* & track methods)
clientInputValidationDecorator(
settings,
clientFactory(params),
sdkReadinessManager.readinessManager
),
// Sdk destroy
{
flush() {
// @TODO define cooldown time
return __cooldown(__flush, COOLDOWN_TIME_IN_MILLIS);
},
destroy() {
if (destroyPromise) return destroyPromise;
// Mark the client as destroyed immediately
sdkReadinessManager.readinessManager.destroy();
const isLastDestroyCall = areAllClientDestroyed(clients);
// Only for client-side standalone
mySegmentsSyncManager && mySegmentsSyncManager.stop();
// For last client, release the SDK Key and record stat before flushing data
if (isLastDestroyCall) {
releaseApiKey(settings.core.authorizationKey);
telemetryTracker.sessionLength();
syncManager && syncManager.stop();
}
return destroyPromise = __flush().then(() => {
if (isLastDestroyCall) {
signalListener && signalListener.stop();
uniqueKeysTracker && uniqueKeysTracker.stop();
}
// Cleanup storage
return storage.destroy();
});
}
}
);
}