44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
66 *
7- * @flow
7+ * @flow strict-local
88 * @format
99 */
1010
1111import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter' ;
1212import NativeSettingsManager from './NativeSettingsManager' ;
1313import invariant from 'invariant' ;
1414
15+ type SettingsValues = { [ string ] : unknown , ...} ;
16+ type SettingsCallback = ( ) => void ;
17+
18+ declare function castSetting < T > ( value : unknown ) : T ;
19+ function castSetting ( value : unknown ) {
20+ return value ;
21+ }
22+
1523const subscriptions : Array < {
1624 keys : Array < string > ,
17- callback : ?Function ,
25+ callback : ?SettingsCallback ,
1826 ...
1927} > = [ ] ;
2028
@@ -25,26 +33,31 @@ const subscriptions: Array<{
2533 * @see https://reactnative.dev/docs/settings
2634 * @platform ios
2735 */
28- const Settings = {
29- _settings : ( NativeSettingsManager &&
30- NativeSettingsManager . getConstants ( ) . settings ) as any ,
36+ const Settings : {
37+ _settings : SettingsValues ,
38+ get< T = unknown > ( key : string ) : ?T ,
39+ set ( settings : SettingsValues ) : void ,
40+ watchKeys ( keys : string | Array < string > , callback : SettingsCallback ) : number ,
41+ clearWatch ( watchId : number ) : void ,
42+ _sendObservations ( body : SettingsValues ) : void ,
43+ } = {
44+ _settings : NativeSettingsManager . getConstants ( ) . settings ,
3145
3246 /**
3347 * Get the current value for the given key.
3448 */
35- get ( key : string ) : unknown {
36- // $FlowFixMe[object-this-reference]
37- return this . _settings [ key ] ;
49+ get< T = unknown > ( key : string ) : ?T {
50+ return castSetting ( Settings . _settings [ key ] ) ;
3851 } ,
3952
4053 /**
4154 * Set one or more values by merging the provided object into the current
4255 * settings.
4356 */
44- set ( settings : Object ) {
45- // $FlowFixMe[object-this-reference]
46- // $FlowFixMe[unsafe-object-assign]
47- this . _settings = Object . assign ( this . _settings , settings ) ;
57+ set ( settings : SettingsValues ) {
58+ Object . keys ( settings ) . forEach ( key => {
59+ Settings . _settings [ key ] = settings [ key ] ;
60+ } ) ;
4861 NativeSettingsManager . setValues ( settings ) ;
4962 } ,
5063
@@ -53,18 +66,16 @@ const Settings = {
5366 * whenever a watched key's value changes. Returns a `watchId` that can be
5467 * passed to `clearWatch` to unsubscribe.
5568 */
56- watchKeys ( keys : string | Array < string > , callback : Function ) : number {
57- if ( typeof keys === 'string' ) {
58- keys = [ keys ] ;
59- }
69+ watchKeys ( keys : string | Array < string > , callback : SettingsCallback ) : number {
70+ const watchedKeys = typeof keys === 'string' ? [ keys ] : keys ;
6071
6172 invariant (
62- Array . isArray ( keys ) ,
73+ Array . isArray ( watchedKeys ) ,
6374 'keys should be a string or array of strings' ,
6475 ) ;
6576
6677 const sid = subscriptions . length ;
67- subscriptions . push ( { keys : keys , callback : callback } ) ;
78+ subscriptions . push ( { keys : watchedKeys , callback} ) ;
6879 return sid ;
6980 } ,
7081
@@ -77,13 +88,11 @@ const Settings = {
7788 }
7889 } ,
7990
80- _sendObservations ( body : Object ) {
91+ _sendObservations ( body : SettingsValues ) {
8192 Object . keys ( body ) . forEach ( key => {
8293 const newValue = body [ key ] ;
83- // $FlowFixMe[object-this-reference]
84- const didChange = this . _settings [ key ] !== newValue ;
85- // $FlowFixMe[object-this-reference]
86- this . _settings [ key ] = newValue ;
94+ const didChange = Settings . _settings [ key ] !== newValue ;
95+ Settings . _settings [ key ] = newValue ;
8796
8897 if ( didChange ) {
8998 subscriptions . forEach ( sub => {
0 commit comments