@@ -16,50 +16,48 @@ import { LinkedList } from "./common/linkedList";
1616import { Disposable } from "./common/disposable" ;
1717
1818export class AzureAppConfigurationImpl extends Map < string , any > implements AzureAppConfiguration {
19- private adapters : IKeyValueAdapter [ ] = [ ] ;
19+ private _adapters : IKeyValueAdapter [ ] = [ ] ;
2020 /**
2121 * Trim key prefixes sorted in descending order.
2222 * Since multiple prefixes could start with the same characters, we need to trim the longest prefix first.
2323 */
24- private sortedTrimKeyPrefixes : string [ ] | undefined ;
25- private readonly requestTracingEnabled : boolean ;
24+ private _sortedTrimKeyPrefixes : string [ ] | undefined ;
25+ private readonly _requestTracingEnabled : boolean ;
2626 // Refresh
27- private refreshIntervalInMs : number | undefined ;
28- private onRefreshListeners : LinkedList < ( ) => any > = new LinkedList ( ) ;
29- private lastUpdateTimestamp : number ;
30- private sentinels : ConfigurationSettingId [ ] | undefined ;
27+ private _refreshInterval : number = DefaultRefreshIntervalInMs ;
28+ private _onRefreshListeners : LinkedList < ( ) => any > = new LinkedList ( ) ;
29+ private _lastUpdateTimestamp : number ;
30+ private _sentinels : ConfigurationSettingId [ ] ;
3131
3232 constructor (
33- private client : AppConfigurationClient ,
34- private options : AzureAppConfigurationOptions | undefined
33+ private _client : AppConfigurationClient ,
34+ private _options : AzureAppConfigurationOptions | undefined
3535 ) {
3636 super ( ) ;
3737 // Enable request tracing if not opt-out
38- this . requestTracingEnabled = requestTracingEnabled ( ) ;
38+ this . _requestTracingEnabled = requestTracingEnabled ( ) ;
3939
40- if ( options ?. trimKeyPrefixes ) {
41- this . sortedTrimKeyPrefixes = [ ...options . trimKeyPrefixes ] . sort ( ( a , b ) => b . localeCompare ( a ) ) ;
40+ if ( _options ?. trimKeyPrefixes ) {
41+ this . _sortedTrimKeyPrefixes = [ ..._options . trimKeyPrefixes ] . sort ( ( a , b ) => b . localeCompare ( a ) ) ;
4242 }
4343
44- if ( options ?. refreshOptions ?. enabled ) {
45- const { watchedSettings, refreshIntervalInMs } = options . refreshOptions ;
44+ if ( _options ?. refreshOptions ?. enabled ) {
45+ const { watchedSettings, refreshIntervalInMs } = _options . refreshOptions ;
4646 // validate watched settings
4747 if ( watchedSettings === undefined || watchedSettings . length === 0 ) {
4848 throw new Error ( "Refresh is enabled but no watched settings are specified." ) ;
4949 }
5050
51- // process refresh interval
51+ // custom refresh interval
5252 if ( refreshIntervalInMs !== undefined ) {
5353 if ( refreshIntervalInMs < MinimumRefreshIntervalInMs ) {
5454 throw new Error ( `The refresh interval time cannot be less than ${ MinimumRefreshIntervalInMs } milliseconds.` ) ;
5555 } else {
56- this . refreshIntervalInMs = refreshIntervalInMs ;
56+ this . _refreshInterval = refreshIntervalInMs ;
5757 }
58- } else {
59- this . refreshIntervalInMs = DefaultRefreshIntervalInMs ;
6058 }
6159
62- this . sentinels = watchedSettings . map ( setting => {
60+ this . _sentinels = watchedSettings . map ( setting => {
6361 if ( setting . key . includes ( "*" ) || setting . label ?. includes ( "*" ) ) {
6462 throw new Error ( "The character '*' is not supported in key or label." ) ;
6563 }
@@ -69,41 +67,41 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
6967
7068 // TODO: should add more adapters to process different type of values
7169 // feature flag, others
72- this . adapters . push ( new AzureKeyVaultKeyValueAdapter ( options ?. keyVaultOptions ) ) ;
73- this . adapters . push ( new JsonKeyValueAdapter ( ) ) ;
70+ this . _adapters . push ( new AzureKeyVaultKeyValueAdapter ( _options ?. keyVaultOptions ) ) ;
71+ this . _adapters . push ( new JsonKeyValueAdapter ( ) ) ;
7472 }
7573
7674
77- get _refreshEnabled ( ) : boolean {
78- return ! ! this . options ?. refreshOptions ?. enabled ;
75+ private get _refreshEnabled ( ) : boolean {
76+ return ! ! this . _options ?. refreshOptions ?. enabled ;
7977 }
8078
8179 public async load ( requestType : RequestType = RequestType . Startup ) {
8280 const keyValues : [ key : string , value : unknown ] [ ] = [ ] ;
8381
8482 // validate selectors
85- const selectors = getValidSelectors ( this . options ?. selectors ) ;
83+ const selectors = getValidSelectors ( this . _options ?. selectors ) ;
8684
8785 for ( const selector of selectors ) {
8886 const listOptions : ListConfigurationSettingsOptions = {
8987 keyFilter : selector . keyFilter ,
9088 labelFilter : selector . labelFilter
9189 } ;
92- if ( this . requestTracingEnabled ) {
90+ if ( this . _requestTracingEnabled ) {
9391 listOptions . requestOptions = {
9492 customHeaders : this . customHeaders ( requestType )
9593 }
9694 }
9795
98- const settings = this . client . listConfigurationSettings ( listOptions ) ;
96+ const settings = this . _client . listConfigurationSettings ( listOptions ) ;
9997
10098 for await ( const setting of settings ) {
10199 if ( setting . key ) {
102100 const keyValuePair = await this . processKeyValues ( setting ) ;
103101 keyValues . push ( keyValuePair ) ;
104102 }
105103 // update etag of sentinels
106- const matchedSentinel = this . sentinels ?. find ( s => s . key === setting . key && ( s . label ?? null ) === setting . label ) ; // Workaround: as undefined label represents the same with null.
104+ const matchedSentinel = this . _sentinels ?. find ( s => s . key === setting . key && ( s . label ?? null ) === setting . label ) ; // Workaround: as undefined label represents the same with null.
107105 if ( matchedSentinel ) {
108106 matchedSentinel . etag = setting . etag ;
109107 }
@@ -112,28 +110,27 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
112110 for ( const [ k , v ] of keyValues ) {
113111 this . set ( k , v ) ;
114112 }
115- this . lastUpdateTimestamp = Date . now ( ) ;
113+ this . _lastUpdateTimestamp = Date . now ( ) ;
116114 }
117115
118116 public async refresh ( ) : Promise < void > {
119- // if no refreshOptions set, return
120- if ( this . sentinels === undefined || this . sentinels . length === 0 || this . refreshIntervalInMs === undefined ) {
117+ if ( ! this . _refreshEnabled ) {
121118 return Promise . resolve ( ) ;
122119 }
123120 // if still within refresh interval, return
124121 const now = Date . now ( ) ;
125- if ( now < this . lastUpdateTimestamp + this . refreshIntervalInMs ) {
122+ if ( now < this . _lastUpdateTimestamp + this . _refreshInterval ) {
126123 return Promise . resolve ( ) ;
127124 }
128125
129126 // try refresh if any of watched settings is changed.
130127 let needRefresh = false ;
131- for ( const sentinel of this . sentinels ) {
132- const response = await this . client . getConfigurationSetting ( sentinel , {
128+ for ( const sentinel of this . _sentinels ) {
129+ const response = await this . _client . getConfigurationSetting ( sentinel , {
133130 onlyIfChanged : true
134131 // TODO: do we trace this request by adding custom headers?
135132 } ) ;
136- if ( response . statusCode !== 304 ) { // TODO: can be more robust, e.g. === 200?
133+ if ( response . statusCode === 200 ) {
137134 // sentinel changed.
138135 sentinel . etag = response . etag ; // update etag of the sentinel
139136 needRefresh = true ;
@@ -143,10 +140,8 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
143140 if ( needRefresh ) {
144141 await this . load ( RequestType . Watch ) ;
145142 // run callbacks in async
146- if ( this . onRefreshListeners !== undefined ) {
147- for ( const listener of this . onRefreshListeners ) {
148- listener ( ) ;
149- }
143+ for ( const listener of this . _onRefreshListeners ) {
144+ listener ( ) ;
150145 }
151146 }
152147 }
@@ -157,7 +152,7 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
157152 }
158153
159154 const boundedListener = listener . bind ( thisArg ) ;
160- const remove = this . onRefreshListeners . push ( boundedListener ) ;
155+ const remove = this . _onRefreshListeners . push ( boundedListener ) ;
161156 return new Disposable ( remove ) ;
162157 }
163158
@@ -168,7 +163,7 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
168163 }
169164
170165 private async processAdapters ( setting : ConfigurationSetting < string > ) : Promise < [ string , unknown ] > {
171- for ( const adapter of this . adapters ) {
166+ for ( const adapter of this . _adapters ) {
172167 if ( adapter . canProcess ( setting ) ) {
173168 return adapter . processKeyValue ( setting ) ;
174169 }
@@ -177,8 +172,8 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
177172 }
178173
179174 private keyWithPrefixesTrimmed ( key : string ) : string {
180- if ( this . sortedTrimKeyPrefixes ) {
181- for ( const prefix of this . sortedTrimKeyPrefixes ) {
175+ if ( this . _sortedTrimKeyPrefixes ) {
176+ for ( const prefix of this . _sortedTrimKeyPrefixes ) {
182177 if ( key . startsWith ( prefix ) ) {
183178 return key . slice ( prefix . length ) ;
184179 }
@@ -188,12 +183,12 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
188183 }
189184
190185 private customHeaders ( requestType : RequestType ) {
191- if ( ! this . requestTracingEnabled ) {
186+ if ( ! this . _requestTracingEnabled ) {
192187 return undefined ;
193188 }
194189
195190 const headers = { } ;
196- headers [ CorrelationContextHeaderName ] = createCorrelationContextHeader ( this . options , requestType ) ;
191+ headers [ CorrelationContextHeaderName ] = createCorrelationContextHeader ( this . _options , requestType ) ;
197192 return headers ;
198193 }
199194}
0 commit comments