@@ -58,11 +58,13 @@ export class Settings {
5858 #eventSupport: ObjectWrapper < GenericEvents > ;
5959 #registry: Map < string , Setting < unknown > > ;
6060 readonly moduleSettings : Map < string , Setting < unknown > > ;
61+ #logSettingAccess?: ( name : string , value : number | string | boolean ) => Promise < void > ;
6162
6263 private constructor (
6364 readonly syncedStorage : SettingsStorage ,
6465 readonly globalStorage : SettingsStorage ,
6566 readonly localStorage : SettingsStorage ,
67+ logSettingAccess ?: ( name : string , value : number | string | boolean ) => Promise < void > ,
6668 ) {
6769 this . #sessionStorage = new SettingsStorage ( { } ) ;
6870
@@ -73,6 +75,7 @@ export class Settings {
7375 this . #eventSupport = new ObjectWrapper < GenericEvents > ( ) ;
7476 this . #registry = new Map ( ) ;
7577 this . moduleSettings = new Map ( ) ;
78+ this . #logSettingAccess = logSettingAccess ;
7679
7780 for ( const registration of this . getRegisteredSettings ( ) ) {
7881 const { settingName, defaultValue, storageType} = registration ;
@@ -107,14 +110,15 @@ export class Settings {
107110 syncedStorage : SettingsStorage | null ,
108111 globalStorage : SettingsStorage | null ,
109112 localStorage : SettingsStorage | null ,
113+ logSettingAccess ?: ( name : string , value : number | string | boolean ) => Promise < void > ,
110114 } = { forceNew : null , syncedStorage : null , globalStorage : null , localStorage : null } ) : Settings {
111- const { forceNew, syncedStorage, globalStorage, localStorage} = opts ;
115+ const { forceNew, syncedStorage, globalStorage, localStorage, logSettingAccess } = opts ;
112116 if ( ! settingsInstance || forceNew ) {
113117 if ( ! syncedStorage || ! globalStorage || ! localStorage ) {
114118 throw new Error ( `Unable to create settings: global and local storage must be provided: ${ new Error ( ) . stack } ` ) ;
115119 }
116120
117- settingsInstance = new Settings ( syncedStorage , globalStorage , localStorage ) ;
121+ settingsInstance = new Settings ( syncedStorage , globalStorage , localStorage , logSettingAccess ) ;
118122 }
119123
120124 return settingsInstance ;
@@ -192,7 +196,7 @@ export class Settings {
192196 const storage = this . storageFromType ( storageType ) ;
193197 let setting = ( this . #registry. get ( key ) as Setting < T > ) ;
194198 if ( ! setting ) {
195- setting = new Setting ( key , defaultValue , this . #eventSupport, storage ) ;
199+ setting = new Setting ( key , defaultValue , this . #eventSupport, storage , this . #logSettingAccess ) ;
196200 this . #registry. set ( key , setting ) ;
197201 }
198202 return setting ;
@@ -206,7 +210,10 @@ export class Settings {
206210 RegExpSetting {
207211 if ( ! this . #registry. get ( key ) ) {
208212 this . #registry. set (
209- key , new RegExpSetting ( key , defaultValue , this . #eventSupport, this . storageFromType ( storageType ) , regexFlags ) ) ;
213+ key ,
214+ new RegExpSetting (
215+ key , defaultValue , this . #eventSupport, this . storageFromType ( storageType ) , regexFlags ,
216+ this . #logSettingAccess) ) ;
210217 }
211218 return this . #registry. get ( key ) as RegExpSetting ;
212219 }
@@ -368,11 +375,15 @@ export class Setting<V> {
368375 #hadUserAction?: boolean ;
369376 #disabled?: boolean ;
370377 #deprecation: Deprecation | null = null ;
378+ #loggedInitialAccess = false ;
379+ #logSettingAccess?: ( name : string , value : number | string | boolean ) => Promise < void > ;
371380
372381 constructor (
373382 readonly name : string , readonly defaultValue : V , private readonly eventSupport : ObjectWrapper < GenericEvents > ,
374- readonly storage : SettingsStorage ) {
383+ readonly storage : SettingsStorage ,
384+ logSettingAccess ?: ( name : string , value : number | string | boolean ) => Promise < void > ) {
375385 storage . register ( this . name ) ;
386+ this . #logSettingAccess = logSettingAccess ;
376387 }
377388
378389 setSerializer ( serializer : Serializer < unknown , V > ) : void {
@@ -438,12 +449,30 @@ export class Setting<V> {
438449 this . eventSupport . dispatchEventToListeners ( this . name ) ;
439450 }
440451
452+ #maybeLogAccess( value : V ) : void {
453+ const valueToLog = typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ?
454+ value :
455+ this . #serializer?. stringify ( value ) ;
456+ if ( valueToLog !== undefined && this . #logSettingAccess) {
457+ void this . #logSettingAccess( this . name , valueToLog ) ;
458+ }
459+ }
460+
461+ #maybeLogInitialAccess( value : V ) : void {
462+ if ( ! this . #loggedInitialAccess) {
463+ this . #maybeLogAccess( value ) ;
464+ this . #loggedInitialAccess = true ;
465+ }
466+ }
467+
441468 get ( ) : V {
442469 if ( this . #requiresUserAction && ! this . #hadUserAction) {
470+ this . #maybeLogInitialAccess( this . defaultValue ) ;
443471 return this . defaultValue ;
444472 }
445473
446474 if ( typeof this . #value !== 'undefined' ) {
475+ this . #maybeLogInitialAccess( this . #value) ;
447476 return this . #value;
448477 }
449478
@@ -455,6 +484,7 @@ export class Setting<V> {
455484 this . storage . remove ( this . name ) ;
456485 }
457486 }
487+ this . #maybeLogInitialAccess( this . #value) ;
458488 return this . #value;
459489 }
460490
@@ -485,10 +515,12 @@ export class Setting<V> {
485515 this . eventSupport . dispatchEventToListeners ( this . name , this . #value) ;
486516 }
487517
518+ this . #maybeLogInitialAccess( this . #value) ;
488519 return this . #value;
489520 }
490521
491522 set ( value : V ) : void {
523+ this . #maybeLogAccess( value ) ;
492524 this . #hadUserAction = true ;
493525 this . #value = value ;
494526 try {
@@ -600,8 +632,8 @@ export class RegExpSetting extends Setting<any> {
600632
601633 constructor (
602634 name : string , defaultValue : string , eventSupport : ObjectWrapper < GenericEvents > , storage : SettingsStorage ,
603- regexFlags ?: string ) {
604- super ( name , defaultValue ? [ { pattern : defaultValue } ] : [ ] , eventSupport , storage ) ;
635+ regexFlags ?: string , logSettingAccess ?: ( name : string , value : number | string | boolean ) => Promise < void > ) {
636+ super ( name , defaultValue ? [ { pattern : defaultValue } ] : [ ] , eventSupport , storage , logSettingAccess ) ;
605637 this . #regexFlags = regexFlags ;
606638 }
607639
0 commit comments