@@ -15,7 +15,7 @@ import { DefaultRefreshIntervalInMs, MinimumRefreshIntervalInMs } from "./Refres
1515import { LinkedList } from "./common/linkedList" ;
1616import { Disposable } from "./common/disposable" ;
1717
18- export class AzureAppConfigurationImpl extends Map < string , unknown > implements AzureAppConfiguration {
18+ export class AzureAppConfigurationImpl extends Map < string , any > implements AzureAppConfiguration {
1919 private adapters : IKeyValueAdapter [ ] = [ ] ;
2020 /**
2121 * Trim key prefixes sorted in descending order.
@@ -25,7 +25,7 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
2525 private readonly requestTracingEnabled : boolean ;
2626 // Refresh
2727 private refreshIntervalInMs : number | undefined ;
28- private onRefreshListeners : LinkedList < ( ) => any > | undefined ;
28+ private onRefreshListeners : LinkedList < ( ) => any > = new LinkedList ( ) ;
2929 private lastUpdateTimestamp : number ;
3030 private sentinels : ConfigurationSettingId [ ] | undefined ;
3131
@@ -41,26 +41,29 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
4141 this . sortedTrimKeyPrefixes = [ ...options . trimKeyPrefixes ] . sort ( ( a , b ) => b . localeCompare ( a ) ) ;
4242 }
4343
44- if ( options ?. refreshOptions ) {
45- this . onRefreshListeners = new LinkedList ( ) ;
46- this . refreshIntervalInMs = DefaultRefreshIntervalInMs ;
44+ if ( options ?. refreshOptions ?. enabled ) {
45+ const { watchedSettings, refreshIntervalInMs } = options . refreshOptions ;
46+ // validate watched settings
47+ if ( watchedSettings === undefined || watchedSettings . length === 0 ) {
48+ throw new Error ( "Refresh is enabled but no watched settings are specified." ) ;
49+ }
4750
48- const refreshIntervalInMs = this . options ?. refreshOptions ?. refreshIntervalInMs ;
51+ // process refresh interval
4952 if ( refreshIntervalInMs !== undefined ) {
5053 if ( refreshIntervalInMs < MinimumRefreshIntervalInMs ) {
5154 throw new Error ( `The refresh interval time cannot be less than ${ MinimumRefreshIntervalInMs } milliseconds.` ) ;
5255 } else {
5356 this . refreshIntervalInMs = refreshIntervalInMs ;
5457 }
58+ } else {
59+ this . refreshIntervalInMs = DefaultRefreshIntervalInMs ;
5560 }
5661
57- this . sentinels = options . refreshOptions . watchedSettings ?. map ( setting => {
58- const key = setting . key ;
59- const label = setting . label ;
60- if ( key . includes ( "*" ) || label ?. includes ( "*" ) ) {
61- throw new Error ( "Wildcard key or label filters are not supported for refresh." ) ;
62+ this . sentinels = watchedSettings . map ( setting => {
63+ if ( setting . key . includes ( "*" ) || setting . label ?. includes ( "*" ) ) {
64+ throw new Error ( "The character '*' is not supported in key or label." ) ;
6265 }
63- return { key , label } ;
66+ return { ... setting } ;
6467 } ) ;
6568 }
6669
@@ -70,6 +73,11 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
7073 this . adapters . push ( new JsonKeyValueAdapter ( ) ) ;
7174 }
7275
76+
77+ get _refreshEnabled ( ) : boolean {
78+ return ! ! this . options ?. refreshOptions ?. enabled ;
79+ }
80+
7381 public async load ( requestType : RequestType = RequestType . Startup ) {
7482 const keyValues : [ key : string , value : unknown ] [ ] = [ ] ;
7583
@@ -144,10 +152,10 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
144152 }
145153
146154 public onRefresh ( listener : ( ) => any , thisArg ?: any ) : Disposable {
147- if ( this . onRefreshListeners === undefined ) {
148- // TODO: Add unit tests
149- throw new Error ( "Illegal operation because refreshOptions is not provided on loading." ) ;
155+ if ( ! this . _refreshEnabled ) {
156+ throw new Error ( "Refresh is not enabled." ) ;
150157 }
158+
151159 const boundedListener = listener . bind ( thisArg ) ;
152160 const remove = this . onRefreshListeners . push ( boundedListener ) ;
153161 return new Disposable ( remove ) ;
0 commit comments