1- import {
2- CachingFlagResolverClient ,
3- FetchingFlagResolverClient ,
4- FlagResolverClient ,
5- PendingResolution ,
6- } from './FlagResolverClient' ;
1+ import { FetchingFlagResolverClient , FlagResolverClient , PendingResolution } from './FlagResolverClient' ;
72import { EventSenderEngine } from './EventSenderEngine' ;
83import { Value } from './Value' ;
94import { EventData , EventSender } from './events' ;
@@ -19,6 +14,7 @@ import { FlagResolution } from './FlagResolution';
1914import { AccessiblePromise } from './AccessiblePromise' ;
2015import { Telemetry } from './Telemetry' ;
2116import { SimpleFetch } from './fetch-util' ;
17+ import { CacheOptions , CacheProvider , FlagCache } from './flag-cache' ;
2218
2319/**
2420 * Confidence options, to be used for easier initialization of Confidence
@@ -48,19 +44,21 @@ export interface ConfidenceOptions {
4844 * This is particularly useful in serverless environments where you need to ensure certain operations complete before the environment is reclaimed.
4945 */
5046 waitUntil ?: WaitUntil ;
47+ /**
48+ * Configuration options for the Confidence SDK's flag caching system.
49+ * @see {@link CacheOptions }
50+ */
51+ cache ?: CacheOptions ;
52+ context ?: Context ;
5153}
5254
5355/**
5456 * Confidence configuration
55- * @public
57+ * @internal
5658 */
57- export interface Configuration {
58- /** Environment: can be either client of backend */
59- readonly environment : 'client' | 'backend' ;
59+ export interface Configuration extends ConfidenceOptions {
6060 /** Debug logger */
6161 readonly logger : Logger ;
62- /** Resolve timeout */
63- readonly timeout : number ;
6462 /** Event Sender Engine
6563 * @internal */
6664 readonly eventSenderEngine : EventSenderEngine ;
@@ -69,6 +67,7 @@ export interface Configuration {
6967 readonly flagResolverClient : FlagResolverClient ;
7068 /* @internal */
7169 readonly clientSecret : string ;
70+ readonly cacheProvider : CacheProvider ;
7271}
7372
7473/**
@@ -79,7 +78,7 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
7978 /** Internal Confidence configurations */
8079 readonly config : Configuration ;
8180 private readonly parent ?: Confidence ;
82- private _context : Map < string , Value > = new Map ( ) ;
81+ private _context : Map < string , Value > ;
8382 private contextChanged ?: Observer < string [ ] > ;
8483
8584 /**
@@ -93,9 +92,10 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
9392 private readonly flagStateSubject : Subscribe < State > ;
9493
9594 /** @internal */
96- constructor ( config : Configuration , parent ?: Confidence ) {
95+ constructor ( { context = { } , ... config } : Configuration , parent ?: Confidence ) {
9796 this . config = config ;
9897 this . parent = parent ;
98+ this . _context = new Map ( Object . entries ( context ) ) ;
9999 this . contextChanges = subject ( observer => {
100100 let parentSubscription : Closer | void ;
101101 if ( parent ) {
@@ -336,6 +336,18 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
336336 ) ;
337337 }
338338
339+ toOptions ( signal ?: AbortSignal ) : ConfidenceOptions {
340+ const cache = this . config . cacheProvider ( this . config . clientSecret ) . toOptions ( signal ) ;
341+ return {
342+ clientSecret : this . config . clientSecret ,
343+ region : this . config . region ,
344+ timeout : this . config . timeout ,
345+ environment : this . config . environment ,
346+ cache,
347+ context : this . getContext ( ) ,
348+ } ;
349+ }
350+
339351 /**
340352 * Creates a Confidence instance
341353 * @param clientSecret - clientSecret found on the Confidence console
@@ -347,18 +359,20 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
347359 * @param resolveBaseUrl - custom backend resolve URL
348360 * @returns
349361 */
350- static create ( {
351- clientSecret,
352- region,
353- timeout,
354- environment,
355- fetchImplementation = defaultFetchImplementation ( ) ,
356- logger = defaultLogger ( ) ,
357- resolveBaseUrl,
358- disableTelemetry = false ,
359- applyDebounce = 10 ,
360- waitUntil,
361- } : ConfidenceOptions ) : Confidence {
362+ static create ( options : ConfidenceOptions ) : Confidence {
363+ const {
364+ clientSecret,
365+ region,
366+ timeout,
367+ environment,
368+ fetchImplementation = defaultFetchImplementation ( ) ,
369+ logger = defaultLogger ( ) ,
370+ resolveBaseUrl,
371+ disableTelemetry = false ,
372+ applyDebounce = 10 ,
373+ waitUntil,
374+ cache = { } ,
375+ } = options ;
362376 if ( environment !== 'client' && environment !== 'backend' ) {
363377 throw new Error ( `Invalid environment: ${ environment } . Must be 'client' or 'backend'.` ) ;
364378 }
@@ -373,7 +387,8 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
373387 if ( ! clientSecret ) {
374388 logger . error ?.( `Confidence: confidence cannot be instantiated without a client secret` ) ;
375389 }
376- let flagResolverClient : FlagResolverClient = new FetchingFlagResolverClient ( {
390+ const cacheProvider = FlagCache . provider ( clientSecret , cache ) ;
391+ const flagResolverClient : FlagResolverClient = new FetchingFlagResolverClient ( {
377392 clientSecret,
378393 fetchImplementation,
379394 sdk,
@@ -385,10 +400,8 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
385400 logger,
386401 applyDebounce,
387402 waitUntil,
403+ cacheProvider,
388404 } ) ;
389- if ( environment === 'client' ) {
390- flagResolverClient = new CachingFlagResolverClient ( flagResolverClient , Number . POSITIVE_INFINITY ) ;
391- }
392405 const estEventSizeKb = 1 ;
393406 const flushTimeoutMilliseconds = 500 ;
394407 // default grpc payload limit is 4MB, so we aim for a 1MB batch-size
@@ -408,12 +421,11 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
408421 logger,
409422 } ) ;
410423 return new Confidence ( {
411- environment : environment ,
424+ ... options ,
412425 flagResolverClient,
413426 eventSenderEngine,
414- timeout,
415427 logger,
416- clientSecret ,
428+ cacheProvider ,
417429 } ) ;
418430 }
419431}
0 commit comments