2015-05-05 12:34:38 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-05-05 12:34:38 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-05-05 12:34:38 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-05-05 12:34:38 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-05-05 12:34:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
|
|
|
const RCTSettingsManager = require('NativeModules').SettingsManager;
|
2015-05-05 12:34:38 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-05-05 12:34:38 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const subscriptions: Array<{keys: Array<string>, callback: ?Function}> = [];
|
2015-05-05 12:34:38 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const Settings = {
|
2015-07-06 11:40:11 +00:00
|
|
|
_settings: RCTSettingsManager && RCTSettingsManager.settings,
|
2015-05-05 12:34:38 +00:00
|
|
|
|
|
|
|
get(key: string): mixed {
|
|
|
|
return this._settings[key];
|
|
|
|
},
|
|
|
|
|
|
|
|
set(settings: Object) {
|
2015-05-05 12:34:39 +00:00
|
|
|
this._settings = Object.assign(this._settings, settings);
|
|
|
|
RCTSettingsManager.setValues(settings);
|
2015-05-05 12:34:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
watchKeys(keys: string | Array<string>, callback: Function): number {
|
2015-05-19 20:43:46 +00:00
|
|
|
if (typeof keys === 'string') {
|
2015-05-05 12:34:38 +00:00
|
|
|
keys = [keys];
|
|
|
|
}
|
|
|
|
|
|
|
|
invariant(
|
|
|
|
Array.isArray(keys),
|
2018-05-11 02:06:46 +00:00
|
|
|
'keys should be a string or array of strings',
|
2015-05-05 12:34:38 +00:00
|
|
|
);
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const sid = subscriptions.length;
|
2015-05-19 20:43:46 +00:00
|
|
|
subscriptions.push({keys: keys, callback: callback});
|
2015-05-05 12:34:38 +00:00
|
|
|
return sid;
|
|
|
|
},
|
|
|
|
|
|
|
|
clearWatch(watchId: number) {
|
|
|
|
if (watchId < subscriptions.length) {
|
|
|
|
subscriptions[watchId] = {keys: [], callback: null};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_sendObservations(body: Object) {
|
2018-05-11 02:06:46 +00:00
|
|
|
Object.keys(body).forEach(key => {
|
2018-05-10 22:44:52 +00:00
|
|
|
const newValue = body[key];
|
|
|
|
const didChange = this._settings[key] !== newValue;
|
2015-05-19 20:43:46 +00:00
|
|
|
this._settings[key] = newValue;
|
2015-05-05 12:34:38 +00:00
|
|
|
|
|
|
|
if (didChange) {
|
2018-05-11 02:06:46 +00:00
|
|
|
subscriptions.forEach(sub => {
|
2015-05-19 20:43:46 +00:00
|
|
|
if (sub.keys.indexOf(key) !== -1 && sub.callback) {
|
2015-05-05 12:34:38 +00:00
|
|
|
sub.callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
RCTDeviceEventEmitter.addListener(
|
|
|
|
'settingsUpdated',
|
2018-05-11 02:06:46 +00:00
|
|
|
Settings._sendObservations.bind(Settings),
|
2015-05-05 12:34:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
module.exports = Settings;
|