@@ -18,28 +18,28 @@ import SnapshotAutoUpdater from './lib/utils/snapshotAutoUpdater.js';
1818import { SnapshotNotFoundError } from './lib/exceptions/index.js' ;
1919import { loadDomain , validateSnapshot , checkSwitchersLocal } from './lib/snapshot.js' ;
2020import { Switcher } from './switcher.js' ;
21- import { Auth } from './lib/remote-auth.js' ;
21+ import { Auth } from './lib/remoteAuth.js' ;
22+ import { GlobalOptions } from './lib/globals/globalOptions.js' ;
23+ import { GlobalSnapshot } from './lib/globals/globalSnapshot.js' ;
2224
2325export class Client {
2426
25- static #options;
2627 static #context;
27- static #snapshot;
2828
2929 static buildContext ( context , options ) {
3030 this . testEnabled = DEFAULT_TEST_MODE ;
3131
32- this . #snapshot = undefined ;
3332 this . #context = context ;
3433 this . #context. environment = util . get ( context . environment , DEFAULT_ENVIRONMENT ) ;
3534
3635 // Default values
37- this . #options = {
36+ GlobalSnapshot . clear ( ) ;
37+ GlobalOptions . init ( {
3838 snapshotAutoUpdateInterval : 0 ,
3939 snapshotLocation : options ?. snapshotLocation ,
4040 local : util . get ( options ?. local , DEFAULT_LOCAL ) ,
4141 logger : util . get ( options ?. logger , DEFAULT_LOGGER )
42- } ;
42+ } ) ;
4343
4444 if ( options ) {
4545 Client . #buildOptions( options ) ;
@@ -60,7 +60,7 @@ export class Client {
6060 }
6161
6262 if ( SWITCHER_OPTIONS . SNAPSHOT_AUTO_UPDATE_INTERVAL in options ) {
63- this . #options . snapshotAutoUpdateInterval = options . snapshotAutoUpdateInterval ;
63+ GlobalOptions . updateOptions ( { snapshotAutoUpdateInterval : options . snapshotAutoUpdateInterval } ) ;
6464 this . scheduleSnapshotAutoUpdate ( ) ;
6565 }
6666
@@ -70,7 +70,7 @@ export class Client {
7070 static #initSilentMode( silentMode ) {
7171 Auth . setRetryOptions ( silentMode ) ;
7272
73- Client . #options . silentMode = silentMode ;
73+ GlobalOptions . updateOptions ( { silentMode } ) ;
7474 Client . loadSnapshot ( ) ;
7575 }
7676
@@ -94,7 +94,7 @@ export class Client {
9494 }
9595
9696 static async checkSnapshot ( ) {
97- if ( ! Client . # snapshot) {
97+ if ( ! GlobalSnapshot . snapshot ) {
9898 throw new SnapshotNotFoundError ( 'Snapshot is not loaded. Use Client.loadSnapshot()' ) ;
9999 }
100100
@@ -104,52 +104,52 @@ export class Client {
104104
105105 const snapshot = await validateSnapshot (
106106 Client . #context,
107- Client . # snapshot. data . domain . version
107+ GlobalSnapshot . snapshot . data . domain . version
108108 ) ;
109109
110110 if ( snapshot ) {
111- if ( Client . #options . snapshotLocation ?. length ) {
112- writeFileSync ( `${ Client . #options . snapshotLocation } /${ Client . #context. environment } .json` , snapshot ) ;
111+ if ( GlobalOptions . snapshotLocation ?. length ) {
112+ writeFileSync ( `${ GlobalOptions . snapshotLocation } /${ Client . #context. environment } .json` , snapshot ) ;
113113 }
114114
115- Client . #snapshot = JSON . parse ( snapshot ) ;
115+ GlobalSnapshot . init ( JSON . parse ( snapshot ) ) ;
116116 return true ;
117117 }
118118
119119 return false ;
120120 }
121121
122122 static async loadSnapshot ( options = { fetchRemote : false , watchSnapshot : false } ) {
123- Client . #snapshot = loadDomain (
124- util . get ( Client . #options . snapshotLocation , '' ) ,
123+ GlobalSnapshot . init ( loadDomain (
124+ util . get ( GlobalOptions . snapshotLocation , '' ) ,
125125 util . get ( Client . #context. environment , DEFAULT_ENVIRONMENT )
126- ) ;
126+ ) ) ;
127127
128- if ( Client . # snapshot. data . domain . version == 0 &&
129- ( options . fetchRemote || ! Client . #options . local ) ) {
128+ if ( GlobalSnapshot . snapshot . data . domain . version == 0 &&
129+ ( options . fetchRemote || ! GlobalOptions . local ) ) {
130130 await Client . checkSnapshot ( ) ;
131131 }
132132
133133 if ( options . watchSnapshot ) {
134134 Client . watchSnapshot ( ) ;
135135 }
136136
137- return Client . # snapshot?. data . domain . version || 0 ;
137+ return GlobalSnapshot . snapshot ?. data . domain . version || 0 ;
138138 }
139139
140140 static watchSnapshot ( callback = { } ) {
141141 const { success = ( ) => { } , reject = ( ) => { } } = callback ;
142142
143- if ( Client . testEnabled || ! Client . #options . snapshotLocation ?. length ) {
143+ if ( Client . testEnabled || ! GlobalOptions . snapshotLocation ?. length ) {
144144 return reject ( new Error ( 'Watch Snapshot cannot be used in test mode or without a snapshot location' ) ) ;
145145 }
146146
147- const snapshotFile = `${ Client . #options . snapshotLocation } /${ Client . #context. environment } .json` ;
147+ const snapshotFile = `${ GlobalOptions . snapshotLocation } /${ Client . #context. environment } .json` ;
148148 let lastUpdate ;
149149 watchFile ( snapshotFile , ( listener ) => {
150150 try {
151151 if ( ! lastUpdate || listener . ctime > lastUpdate ) {
152- Client . #snapshot = loadDomain ( Client . #options . snapshotLocation , Client . #context. environment ) ;
152+ GlobalSnapshot . init ( loadDomain ( GlobalOptions . snapshotLocation , Client . #context. environment ) ) ;
153153 success ( ) ;
154154 }
155155 } catch ( e ) {
@@ -165,21 +165,21 @@ export class Client {
165165 return ;
166166 }
167167
168- const snapshotFile = `${ Client . #options . snapshotLocation } ${ Client . #context. environment } .json` ;
169- Client . #snapshot = undefined ;
168+ const snapshotFile = `${ GlobalOptions . snapshotLocation } ${ Client . #context. environment } .json` ;
169+ GlobalSnapshot . clear ( ) ;
170170 unwatchFile ( snapshotFile ) ;
171171 }
172172
173173 static scheduleSnapshotAutoUpdate ( interval , callback = { } ) {
174174 const { success = ( ) => { } , reject = ( ) => { } } = callback ;
175175
176176 if ( interval ) {
177- Client . #options . snapshotAutoUpdateInterval = interval ;
177+ GlobalOptions . updateOptions ( { snapshotAutoUpdateInterval : interval } ) ;
178178 }
179179
180- if ( Client . #options . snapshotAutoUpdateInterval && Client . #options . snapshotAutoUpdateInterval > 0 ) {
180+ if ( GlobalOptions . snapshotAutoUpdateInterval && GlobalOptions . snapshotAutoUpdateInterval > 0 ) {
181181 SnapshotAutoUpdater . schedule (
182- Client . #options . snapshotAutoUpdateInterval ,
182+ GlobalOptions . snapshotAutoUpdateInterval ,
183183 this . checkSnapshot ,
184184 success ,
185185 reject
@@ -192,8 +192,8 @@ export class Client {
192192 }
193193
194194 static async checkSwitchers ( switcherKeys ) {
195- if ( Client . #options . local && Client . # snapshot) {
196- checkSwitchersLocal ( Client . # snapshot, switcherKeys ) ;
195+ if ( GlobalOptions . local && GlobalSnapshot . snapshot ) {
196+ checkSwitchersLocal ( GlobalSnapshot . snapshot , switcherKeys ) ;
197197 } else {
198198 await Client . _checkSwitchersRemote ( switcherKeys ) ;
199199 }
@@ -204,8 +204,8 @@ export class Client {
204204 await Auth . auth ( ) ;
205205 await remote . checkSwitchers ( switcherKeys ) ;
206206 } catch ( e ) {
207- if ( Client . #options . silentMode ) {
208- checkSwitchersLocal ( Client . # snapshot, switcherKeys ) ;
207+ if ( GlobalOptions . silentMode ) {
208+ checkSwitchersLocal ( GlobalSnapshot . snapshot , switcherKeys ) ;
209209 } else {
210210 throw e ;
211211 }
@@ -239,15 +239,6 @@ export class Client {
239239 static testMode ( testEnabled = true ) {
240240 Client . testEnabled = testEnabled ;
241241 }
242-
243- static get options ( ) {
244- return Client . #options;
245- }
246-
247- static get snapshot ( ) {
248- return Client . #snapshot;
249- }
250-
251242}
252243
253244// Type export placeholders
0 commit comments