-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Reset Hermes timezone cache on system timezone change #55467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Im-awesome-Aadi
wants to merge
1
commit into
facebook:main
Choose a base branch
from
Im-awesome-Aadi:fix/my-awesome-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...t-native/ReactAndroid/src/main/java/com/facebook/react/modules/timezone/TimeZoneModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.modules.timezone | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.IntentFilter | ||
| import android.util.Log | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
| import com.facebook.react.module.annotations.ReactModule | ||
|
|
||
| /** | ||
| * Internal module responsible for listening to system timezone changes and | ||
| * resetting the Hermes timezone cache when applicable. | ||
| * | ||
| * This ensures Date behavior remains correct after a device timezone change | ||
| * during runtime of app. | ||
| */ | ||
| @ReactModule(name = TimeZoneModule.NAME) | ||
| public open class TimeZoneModule(private val reactContext: ReactApplicationContext) : | ||
| ReactContextBaseJavaModule(reactContext) { | ||
|
|
||
| public companion object { | ||
| public const val NAME: String = "TimeZoneModule" | ||
| } | ||
|
|
||
| private var timeZoneChangeReceiver: BroadcastReceiver? = null | ||
|
|
||
| public override fun getName(): String = NAME | ||
|
|
||
| private external fun resetNativeHermesTimeZoneCache(jsRuntimePtr: Long) | ||
|
|
||
| public override fun initialize() { | ||
| super.initialize() | ||
| registerTimeZoneChangeReceiver() | ||
| } | ||
|
|
||
| private fun registerTimeZoneChangeReceiver() { | ||
| if (timeZoneChangeReceiver != null) { | ||
| return | ||
| } | ||
| timeZoneChangeReceiver = | ||
| object : BroadcastReceiver() { | ||
| override fun onReceive(context: Context?, intent: Intent?) { | ||
| handleTimeZoneChange() | ||
| } | ||
| } | ||
|
|
||
| reactContext.registerReceiver( | ||
| timeZoneChangeReceiver, | ||
| IntentFilter(Intent.ACTION_TIMEZONE_CHANGED) | ||
| ) | ||
| } | ||
|
|
||
| public override fun onCatalystInstanceDestroy() { | ||
| timeZoneChangeReceiver?.let { reactContext.unregisterReceiver(it) } | ||
| timeZoneChangeReceiver = null | ||
| super.onCatalystInstanceDestroy() | ||
| } | ||
|
|
||
| protected open fun resetHermesTimeZoneCache(jsRuntimePtr: Long) { | ||
| resetNativeHermesTimeZoneCache(jsRuntimePtr) | ||
| } | ||
|
|
||
| private fun handleTimeZoneChange() { | ||
| try { | ||
| val catalystInstance = reactApplicationContext.catalystInstance ?: return | ||
| reactApplicationContext.runOnJSQueueThread { | ||
| val jsContext = catalystInstance.javaScriptContextHolder.get() | ||
| resetHermesTimeZoneCache(jsContext) | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.e(NAME, "Failed to reset Hermes timezone cache on timezone change", e) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/react-native/ReactAndroid/src/main/jni/react/jni/TimeZoneCache.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include "TimeZoneCache.h" | ||
| #include <jsi/hermes-interfaces.h> | ||
|
|
||
| namespace facebook::react { | ||
|
|
||
| void TimeZoneCache::resetNativeHermesTimeZoneCache(jni::alias_ref<jclass> /* unused */,jlong jsRuntimePtr) { | ||
| if (!jsRuntimePtr) { | ||
| return; | ||
| } | ||
| jsi::Runtime &runtime = *reinterpret_cast<jsi::Runtime*>(jsRuntimePtr); | ||
| auto* hermesAPI = jsi::castInterface<hermes::IHermes>(&runtime); | ||
| if (!hermesAPI) { | ||
| return; | ||
| } | ||
| hermesAPI->resetTimezoneCache(); | ||
| } | ||
| void TimeZoneCache::registerNatives() { | ||
| registerHybrid({ | ||
| makeNativeMethod("resetNativeHermesTimeZoneCache", TimeZoneCache::resetNativeHermesTimeZoneCache), | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } // namespace facebook::react |
27 changes: 27 additions & 0 deletions
27
packages/react-native/ReactAndroid/src/main/jni/react/jni/TimeZoneCache.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <fbjni/fbjni.h> | ||
|
|
||
| namespace facebook::react { | ||
|
|
||
| class TimeZoneCache : public jni::HybridClass<TimeZoneCache> { | ||
| public: | ||
| static constexpr auto kJavaDescriptor = "Lcom/facebook/react/modules/timezone/TimeZoneModule;"; | ||
|
|
||
| static void registerNatives(); | ||
|
|
||
| static void resetNativeHermesTimeZoneCache(jni::alias_ref<jclass> /* unused */, jlong jsRuntimePtr); | ||
|
|
||
| ~TimeZoneCache() = default; | ||
|
|
||
| friend HybridBase; | ||
| }; | ||
|
|
||
| } // namespace facebook::react |
128 changes: 128 additions & 0 deletions
128
...tive/ReactAndroid/src/test/java/com/facebook/react/modules/timezone/TimeZoneModuleTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| @file:Suppress("DEPRECATION") | ||
|
|
||
| package com.facebook.react.modules.timezone | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Intent | ||
| import android.content.IntentFilter | ||
| import com.facebook.react.bridge.CatalystInstance | ||
| import com.facebook.react.bridge.JavaScriptContextHolder | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.doAnswer | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.spy | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(manifest = Config.NONE) | ||
| class TimeZoneModuleTest { | ||
|
|
||
| private lateinit var reactContext: ReactApplicationContext | ||
| private lateinit var catalystInstance: CatalystInstance | ||
| private lateinit var module: TestableTimeZoneModule | ||
|
|
||
| // Test subclass to override the native method and avoid UnsatisfiedLinkError | ||
| class TestableTimeZoneModule(context: ReactApplicationContext) : TimeZoneModule(context) { | ||
| public override fun resetHermesTimeZoneCache(jsRuntimePtr: Long) { | ||
| // Do nothing in JVM test | ||
| } | ||
| } | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| // Mock ReactApplicationContext and CatalystInstance | ||
| reactContext = mock() | ||
| catalystInstance = mock() | ||
| whenever(reactContext.catalystInstance).thenReturn(catalystInstance) | ||
|
|
||
| // Use the test subclass that overrides the native method | ||
| module = spy(TestableTimeZoneModule(reactContext)) | ||
| } | ||
|
|
||
| @Test | ||
| fun testModuleName() { | ||
| assertEquals(TimeZoneModule.NAME, module.name) | ||
| } | ||
|
|
||
| @Test | ||
| fun testInitializeRegistersReceiver() { | ||
| module.initialize() | ||
|
|
||
| // Verify that the private receiver is not null | ||
| val receiverField = TimeZoneModule::class.java.getDeclaredField("timeZoneChangeReceiver") | ||
| receiverField.isAccessible = true | ||
| val receiver = receiverField.get(module) | ||
| assertNotNull(receiver) | ||
|
|
||
| // Verify that the receiver was registered with the correct intent filter | ||
| verify(reactContext).registerReceiver(any<BroadcastReceiver>(), any<IntentFilter>()) | ||
| } | ||
|
|
||
| @Test | ||
| fun testOnCatalystInstanceDestroyUnregistersReceiver() { | ||
| module.initialize() // registers the receiver | ||
|
|
||
| module.onCatalystInstanceDestroy() | ||
|
|
||
| // Verify that the private receiver is cleared | ||
| val receiverField = TimeZoneModule::class.java.getDeclaredField("timeZoneChangeReceiver") | ||
| receiverField.isAccessible = true | ||
| val receiver = receiverField.get(module) | ||
| assertNull(receiver) | ||
|
|
||
| // Verify that the receiver was unregistered | ||
| verify(reactContext).unregisterReceiver(any<BroadcastReceiver>()) | ||
| } | ||
|
|
||
| @Test | ||
| fun testTimeZoneChangeTriggersHermesReset() { | ||
| // 1. Mock JavaScriptContextHolder | ||
| val jsContextHolder = mock<JavaScriptContextHolder>() | ||
| whenever(catalystInstance.javaScriptContextHolder).thenReturn(jsContextHolder) | ||
| whenever(jsContextHolder.get()).thenReturn(12345L) | ||
|
|
||
| // 2. Mock runOnJSQueueThread to run synchronously | ||
| doAnswer { invocation -> | ||
| val runnable = invocation.arguments[0] as Runnable | ||
| runnable.run() | ||
| null | ||
| } | ||
| .whenever(reactContext) | ||
| .runOnJSQueueThread(any<Runnable>()) | ||
|
|
||
| // 3. Initialize the module (registers the receiver) | ||
| module.initialize() | ||
|
|
||
| // 4. Access the private receiver via reflection | ||
| val receiverField = TimeZoneModule::class.java.getDeclaredField("timeZoneChangeReceiver") | ||
| receiverField.isAccessible = true | ||
| val receiver = receiverField.get(module) as BroadcastReceiver | ||
|
|
||
| // 5. Simulate timezone change broadcast | ||
| receiver.onReceive(reactContext, Intent(Intent.ACTION_TIMEZONE_CHANGED)) | ||
|
|
||
| // 6. Verify that the JS context was accessed | ||
| verify(catalystInstance).javaScriptContextHolder | ||
| verify(jsContextHolder).get() | ||
|
|
||
| // 7. Verify that resetHermesTimeZoneCache was called with correct JS context | ||
| verify(module).resetHermesTimeZoneCache(12345L) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catalystInstance is a legacy arch concept and
javaScriptContextHolderwon't work there. Please ensure this works with the new arch.