From f15309fa15fbbda2feeee2820d3f0d69e3712afd Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Thu, 24 Oct 2019 11:07:13 -0700 Subject: [PATCH] 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 --- Libraries/Utilities/Appearance.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/Libraries/Utilities/Appearance.js b/Libraries/Utilities/Appearance.js index f96698ff182..39ea987a4af 100644 --- a/Libraries/Utilities/Appearance.js +++ b/Libraries/Utilities/Appearance.js @@ -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.