Appearance: stop caching colorScheme in JS

Summary:
At times, the OS updates the color scheme without any active listener on RN side, e.g. if all RCTRootView's in iOS have been deallocated, no one will tell JS that the color scheme changes. So let's just always ask native side for the latest value.

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D18117334

fbshipit-source-id: e8564fb284c5720061592ba72e5b4907e5b48853
This commit is contained in:
Kevin Gozali
2019-10-24 11:09:10 -07:00
committed by Facebook Github Bot
parent 03acf57b76
commit f15309fa15
+12 -14
View File
@@ -21,18 +21,6 @@ import invariant from 'invariant';
type AppearanceListener = (preferences: AppearancePreferences) => void;
const eventEmitter = new EventEmitter();
// TODO: (hramos) T52919652 Use ?ColorSchemeName once codegen supports union
const nativeColorScheme: ?string =
NativeAppearance == null ? null : NativeAppearance.getColorScheme() || null;
invariant(
nativeColorScheme === 'dark' ||
nativeColorScheme === 'light' ||
nativeColorScheme == null,
"Unrecognized color scheme. Did you mean 'dark' or 'light'?",
);
let currentColorScheme: ?ColorSchemeName = nativeColorScheme;
if (NativeAppearance) {
const nativeEventEmitter = new NativeEventEmitter(NativeAppearance);
nativeEventEmitter.addListener(
@@ -45,7 +33,6 @@ if (NativeAppearance) {
colorScheme == null,
"Unrecognized color scheme. Did you mean 'dark' or 'light'?",
);
currentColorScheme = colorScheme;
eventEmitter.emit('change', {colorScheme});
},
);
@@ -63,7 +50,18 @@ module.exports = {
* @returns {?ColorSchemeName} Value for the color scheme preference.
*/
getColorScheme(): ?ColorSchemeName {
return currentColorScheme;
// TODO: (hramos) T52919652 Use ?ColorSchemeName once codegen supports union
const nativeColorScheme: ?string =
NativeAppearance == null
? null
: NativeAppearance.getColorScheme() || null;
invariant(
nativeColorScheme === 'dark' ||
nativeColorScheme === 'light' ||
nativeColorScheme == null,
"Unrecognized color scheme. Did you mean 'dark' or 'light'?",
);
return nativeColorScheme;
},
/**
* Add an event handler that is fired when appearance preferences change.