11import { objectAssign } from '../utils/lang/objectAssign' ;
22import SplitIO from '../../types/splitio' ;
3- import { releaseApiKey , validateAndTrackApiKey } from '../utils/inputValidation/apiKey' ;
43import { clientFactory } from './client' ;
54import { clientInputValidationDecorator } from './clientInputValidation' ;
65import { 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 */
1311export 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}
0 commit comments