1
+ import { IConfiguration } from '../i-configuration' ;
1
2
import { Environment } from '../interfaces' ;
2
3
4
+ /**
5
+ * `ConfigurationStore` is a central piece of Eppo SDK and answers a
6
+ * simple question: what configuration is currently active?
7
+ *
8
+ * @internal `ConfigurationStore` shall only be used inside Eppo SDKs.
9
+ */
10
+ export class ConfigurationStore {
11
+ private readonly listeners : Array <
12
+ ( configuration : IConfiguration | null ) => void
13
+ > = [ ] ;
14
+
15
+ // TODO: replace IConfiguration with a concrete `Configuration` type.
16
+ public constructor ( private configuration : IConfiguration | null = null ) { }
17
+
18
+ public getConfiguration ( ) : IConfiguration | null {
19
+ return this . configuration ;
20
+ }
21
+
22
+ public setConfiguration ( configuration : IConfiguration | null ) : void {
23
+ this . configuration = configuration ;
24
+ this . notifyListeners ( ) ;
25
+ }
26
+
27
+ /**
28
+ * Subscribe to configuration changes. The callback will be called
29
+ * every time configuration is changed.
30
+ *
31
+ * Returns a function to unsubscribe from future updates.
32
+ */
33
+ public onConfigurationChange (
34
+ listener : ( configuration : IConfiguration | null ) => void
35
+ ) : ( ) => void {
36
+ this . listeners . push ( listener ) ;
37
+
38
+ return ( ) => {
39
+ const idx = this . listeners . indexOf ( listener ) ;
40
+ if ( idx !== - 1 ) {
41
+ this . listeners . splice ( idx , 1 ) ;
42
+ }
43
+ } ;
44
+ }
45
+
46
+ private notifyListeners ( ) : void {
47
+ for ( const listener of this . listeners ) {
48
+ try {
49
+ listener ( this . configuration ) ;
50
+ } catch { }
51
+ }
52
+ }
53
+ }
54
+
3
55
/**
4
56
* ConfigurationStore interface
5
57
*
@@ -21,6 +73,8 @@ import { Environment } from '../interfaces';
21
73
*
22
74
* The policy choices surrounding the use of one or more underlying storages are
23
75
* implementation specific and handled upstream.
76
+ *
77
+ * @deprecated To be replaced with ConfigurationStore and PersistentStorage.
24
78
*/
25
79
export interface IConfigurationStore < T > {
26
80
init ( ) : Promise < void > ;
@@ -41,6 +95,7 @@ export interface IConfigurationStore<T> {
41
95
salt ?: string ;
42
96
}
43
97
98
+ /** @deprecated To be replaced with ConfigurationStore and PersistentStorage. */
44
99
export interface ISyncStore < T > {
45
100
get ( key : string ) : T | null ;
46
101
entries ( ) : Record < string , T > ;
@@ -49,6 +104,7 @@ export interface ISyncStore<T> {
49
104
setEntries ( entries : Record < string , T > ) : void ;
50
105
}
51
106
107
+ /** @deprecated To be replaced with ConfigurationStore and PersistentStorage. */
52
108
export interface IAsyncStore < T > {
53
109
isInitialized ( ) : boolean ;
54
110
isExpired ( ) : Promise < boolean > ;
0 commit comments