diff --git a/packages/react-native/React/CoreModules/RCTTiming.mm b/packages/react-native/React/CoreModules/RCTTiming.mm index d77719a4b5d6..a1f51a797559 100644 --- a/packages/react-native/React/CoreModules/RCTTiming.mm +++ b/packages/react-native/React/CoreModules/RCTTiming.mm @@ -11,6 +11,7 @@ #import #import #import +#import static const NSTimeInterval kMinimumSleepInterval = 1; @@ -20,6 +21,16 @@ // The minimum time left in a frame to trigger the idle callback. static const NSTimeInterval kIdleCallbackFrameDeadline = 0.001; +// The display link wakes the JS thread on every frame while it runs. When the +// next timer is further away than this, it is paused and a sleep timer wakes +// the JS thread when the timer is due instead. That is two frames with +// `sleepUntilNextTimerIOS`, and `kMinimumSleepInterval` otherwise. +static NSTimeInterval RCTMinimumSleepInterval() +{ + return facebook::react::ReactNativeFeatureFlags::sleepUntilNextTimerIOS() ? 2 * kFrameDuration + : kMinimumSleepInterval; +} + @interface _RCTTimer : NSObject @property (nonatomic, strong, readonly) NSDate *target; @@ -284,8 +295,8 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update if (timerCount == 0) { _paused = YES; } - // If the next timer is more than 1 second out, pause and schedule an NSTimer; - else if ([nextScheduledTarget timeIntervalSinceNow] > kMinimumSleepInterval) { + // If the next timer is far enough out, pause and schedule an NSTimer; + else if ([nextScheduledTarget timeIntervalSinceNow] > RCTMinimumSleepInterval()) { [self scheduleSleepTimer:nextScheduledTarget]; _paused = YES; } @@ -347,7 +358,7 @@ - (void)createTimerForNextFrame:(nonnull NSNumber *)callbackID if (_inBackground) { [self scheduleSleepTimer:timer.target]; } else if (_paused) { - if ([timer.target timeIntervalSinceNow] > kMinimumSleepInterval) { + if ([timer.target timeIntervalSinceNow] > RCTMinimumSleepInterval()) { [self scheduleSleepTimer:timer.target]; } else { [self startTimers]; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt index 243a3eae3fa8..0c39dda19fc6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -510,6 +510,12 @@ public object ReactNativeFeatureFlags { @JvmStatic public fun skipUnchangedChildrenInDifferentiator(): Boolean = accessor.skipUnchangedChildrenInDifferentiator() + /** + * When the next JS timer is more than two frames away, iOS waits for it with a one-shot timer instead of a display link that wakes the JS thread on every frame until the timer is due. + */ + @JvmStatic + public fun sleepUntilNextTimerIOS(): Boolean = accessor.sleepUntilNextTimerIOS() + /** * Override getClipBounds on Android views to return the padding box when overflow is hidden */ diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt index 0f110b756115..557b402ade48 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -100,6 +100,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces private var shouldTriggerResponderTransferOnScrollAndroidCache: Boolean? = null private var skipActivityIdentityAssertionOnHostPauseCache: Boolean? = null private var skipUnchangedChildrenInDifferentiatorCache: Boolean? = null + private var sleepUntilNextTimerIOSCache: Boolean? = null private var syncAndroidClipBoundsWithOverflowCache: Boolean? = null private var traceTurboModulePromiseRejectionsOnAndroidCache: Boolean? = null private var updateRuntimeShadowNodeReferencesOnCommitCache: Boolean? = null @@ -837,6 +838,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces return cached } + override fun sleepUntilNextTimerIOS(): Boolean { + var cached = sleepUntilNextTimerIOSCache + if (cached == null) { + cached = ReactNativeFeatureFlagsCxxInterop.sleepUntilNextTimerIOS() + sleepUntilNextTimerIOSCache = cached + } + return cached + } + override fun syncAndroidClipBoundsWithOverflow(): Boolean { var cached = syncAndroidClipBoundsWithOverflowCache if (cached == null) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt index fc4b23624bbd..97a2cf012837 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<24e0c997f84cabfbae228230f6ac7634>> */ /** @@ -188,6 +188,8 @@ public object ReactNativeFeatureFlagsCxxInterop { @DoNotStrip @JvmStatic public external fun skipUnchangedChildrenInDifferentiator(): Boolean + @DoNotStrip @JvmStatic public external fun sleepUntilNextTimerIOS(): Boolean + @DoNotStrip @JvmStatic public external fun syncAndroidClipBoundsWithOverflow(): Boolean @DoNotStrip @JvmStatic public external fun traceTurboModulePromiseRejectionsOnAndroid(): Boolean diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt index 380e9ec4c213..1a2fc631898a 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<1185e9c777d3a0cd17d268cefb7dcd5e>> */ /** @@ -183,6 +183,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi override fun skipUnchangedChildrenInDifferentiator(): Boolean = false + override fun sleepUntilNextTimerIOS(): Boolean = false + override fun syncAndroidClipBoundsWithOverflow(): Boolean = false override fun traceTurboModulePromiseRejectionsOnAndroid(): Boolean = false diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt index 073cc2b90a73..88fbe3399d44 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -104,6 +104,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc private var shouldTriggerResponderTransferOnScrollAndroidCache: Boolean? = null private var skipActivityIdentityAssertionOnHostPauseCache: Boolean? = null private var skipUnchangedChildrenInDifferentiatorCache: Boolean? = null + private var sleepUntilNextTimerIOSCache: Boolean? = null private var syncAndroidClipBoundsWithOverflowCache: Boolean? = null private var traceTurboModulePromiseRejectionsOnAndroidCache: Boolean? = null private var updateRuntimeShadowNodeReferencesOnCommitCache: Boolean? = null @@ -921,6 +922,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc return cached } + override fun sleepUntilNextTimerIOS(): Boolean { + var cached = sleepUntilNextTimerIOSCache + if (cached == null) { + cached = currentProvider.sleepUntilNextTimerIOS() + accessedFeatureFlags.add("sleepUntilNextTimerIOS") + sleepUntilNextTimerIOSCache = cached + } + return cached + } + override fun syncAndroidClipBoundsWithOverflow(): Boolean { var cached = syncAndroidClipBoundsWithOverflowCache if (cached == null) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt index a639fd19fa5e..cdafe0ebbdf1 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<8e293c96cb85a664164ba5e9b67b3053>> */ /** @@ -183,6 +183,8 @@ public interface ReactNativeFeatureFlagsProvider { @DoNotStrip public fun skipUnchangedChildrenInDifferentiator(): Boolean + @DoNotStrip public fun sleepUntilNextTimerIOS(): Boolean + @DoNotStrip public fun syncAndroidClipBoundsWithOverflow(): Boolean @DoNotStrip public fun traceTurboModulePromiseRejectionsOnAndroid(): Boolean diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp index 882566ee95ea..805c7c8d7cdb 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<1ab2eec7b9c5bdd181ec5fbfd37149ab>> + * @generated SignedSource<> */ /** @@ -519,6 +519,12 @@ class ReactNativeFeatureFlagsJavaProvider return method(javaProvider_); } + bool sleepUntilNextTimerIOS() override { + static const auto method = + getReactNativeFeatureFlagsProviderJavaClass()->getMethod("sleepUntilNextTimerIOS"); + return method(javaProvider_); + } + bool syncAndroidClipBoundsWithOverflow() override { static const auto method = getReactNativeFeatureFlagsProviderJavaClass()->getMethod("syncAndroidClipBoundsWithOverflow"); @@ -1019,6 +1025,11 @@ bool JReactNativeFeatureFlagsCxxInterop::skipUnchangedChildrenInDifferentiator( return ReactNativeFeatureFlags::skipUnchangedChildrenInDifferentiator(); } +bool JReactNativeFeatureFlagsCxxInterop::sleepUntilNextTimerIOS( + facebook::jni::alias_ref /*unused*/) { + return ReactNativeFeatureFlags::sleepUntilNextTimerIOS(); +} + bool JReactNativeFeatureFlagsCxxInterop::syncAndroidClipBoundsWithOverflow( facebook::jni::alias_ref /*unused*/) { return ReactNativeFeatureFlags::syncAndroidClipBoundsWithOverflow(); @@ -1370,6 +1381,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() { makeNativeMethod( "skipUnchangedChildrenInDifferentiator", JReactNativeFeatureFlagsCxxInterop::skipUnchangedChildrenInDifferentiator), + makeNativeMethod( + "sleepUntilNextTimerIOS", + JReactNativeFeatureFlagsCxxInterop::sleepUntilNextTimerIOS), makeNativeMethod( "syncAndroidClipBoundsWithOverflow", JReactNativeFeatureFlagsCxxInterop::syncAndroidClipBoundsWithOverflow), diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h index 8ae17b1dc3b6..01fedcdf3dfe 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<2c8f9205f1de55591ede3845ba2fa877>> + * @generated SignedSource<> */ /** @@ -270,6 +270,9 @@ class JReactNativeFeatureFlagsCxxInterop static bool skipUnchangedChildrenInDifferentiator( facebook::jni::alias_ref); + static bool sleepUntilNextTimerIOS( + facebook::jni::alias_ref); + static bool syncAndroidClipBoundsWithOverflow( facebook::jni::alias_ref); diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp index c2457ce7afc5..14f4d6c2433b 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<7542508e6a3b0dd942557748186527b3>> + * @generated SignedSource<> */ /** @@ -346,6 +346,10 @@ bool ReactNativeFeatureFlags::skipUnchangedChildrenInDifferentiator() { return getAccessor().skipUnchangedChildrenInDifferentiator(); } +bool ReactNativeFeatureFlags::sleepUntilNextTimerIOS() { + return getAccessor().sleepUntilNextTimerIOS(); +} + bool ReactNativeFeatureFlags::syncAndroidClipBoundsWithOverflow() { return getAccessor().syncAndroidClipBoundsWithOverflow(); } diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h index 9668de64aaee..94bfdf655a99 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -441,6 +441,11 @@ class ReactNativeFeatureFlags { */ RN_EXPORT static bool skipUnchangedChildrenInDifferentiator(); + /** + * When the next JS timer is more than two frames away, iOS waits for it with a one-shot timer instead of a display link that wakes the JS thread on every frame until the timer is due. + */ + RN_EXPORT static bool sleepUntilNextTimerIOS(); + /** * Override getClipBounds on Android views to return the padding box when overflow is hidden */ diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp index 418d452984b9..a4c2286543d1 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<3c7ff59afd1e9e05051e888b51a39ac0>> + * @generated SignedSource<> */ /** @@ -1469,6 +1469,24 @@ bool ReactNativeFeatureFlagsAccessor::skipUnchangedChildrenInDifferentiator() { return flagValue.value(); } +bool ReactNativeFeatureFlagsAccessor::sleepUntilNextTimerIOS() { + auto flagValue = sleepUntilNextTimerIOS_.load(); + + if (!flagValue.has_value()) { + // This block is not exclusive but it is not necessary. + // If multiple threads try to initialize the feature flag, we would only + // be accessing the provider multiple times but the end state of this + // instance and the returned flag value would be the same. + + markFlagAsAccessed(80, "sleepUntilNextTimerIOS"); + + flagValue = currentProvider_->sleepUntilNextTimerIOS(); + sleepUntilNextTimerIOS_ = flagValue; + } + + return flagValue.value(); +} + bool ReactNativeFeatureFlagsAccessor::syncAndroidClipBoundsWithOverflow() { auto flagValue = syncAndroidClipBoundsWithOverflow_.load(); @@ -1478,7 +1496,7 @@ bool ReactNativeFeatureFlagsAccessor::syncAndroidClipBoundsWithOverflow() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(80, "syncAndroidClipBoundsWithOverflow"); + markFlagAsAccessed(81, "syncAndroidClipBoundsWithOverflow"); flagValue = currentProvider_->syncAndroidClipBoundsWithOverflow(); syncAndroidClipBoundsWithOverflow_ = flagValue; @@ -1496,7 +1514,7 @@ bool ReactNativeFeatureFlagsAccessor::traceTurboModulePromiseRejectionsOnAndroid // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(81, "traceTurboModulePromiseRejectionsOnAndroid"); + markFlagAsAccessed(82, "traceTurboModulePromiseRejectionsOnAndroid"); flagValue = currentProvider_->traceTurboModulePromiseRejectionsOnAndroid(); traceTurboModulePromiseRejectionsOnAndroid_ = flagValue; @@ -1514,7 +1532,7 @@ bool ReactNativeFeatureFlagsAccessor::updateRuntimeShadowNodeReferencesOnCommit( // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(82, "updateRuntimeShadowNodeReferencesOnCommit"); + markFlagAsAccessed(83, "updateRuntimeShadowNodeReferencesOnCommit"); flagValue = currentProvider_->updateRuntimeShadowNodeReferencesOnCommit(); updateRuntimeShadowNodeReferencesOnCommit_ = flagValue; @@ -1532,7 +1550,7 @@ bool ReactNativeFeatureFlagsAccessor::updateRuntimeShadowNodeReferencesOnCommitT // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(83, "updateRuntimeShadowNodeReferencesOnCommitThread"); + markFlagAsAccessed(84, "updateRuntimeShadowNodeReferencesOnCommitThread"); flagValue = currentProvider_->updateRuntimeShadowNodeReferencesOnCommitThread(); updateRuntimeShadowNodeReferencesOnCommitThread_ = flagValue; @@ -1550,7 +1568,7 @@ bool ReactNativeFeatureFlagsAccessor::useAlwaysAvailableJSErrorHandling() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(84, "useAlwaysAvailableJSErrorHandling"); + markFlagAsAccessed(85, "useAlwaysAvailableJSErrorHandling"); flagValue = currentProvider_->useAlwaysAvailableJSErrorHandling(); useAlwaysAvailableJSErrorHandling_ = flagValue; @@ -1568,7 +1586,7 @@ bool ReactNativeFeatureFlagsAccessor::useFabricInterop() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(85, "useFabricInterop"); + markFlagAsAccessed(86, "useFabricInterop"); flagValue = currentProvider_->useFabricInterop(); useFabricInterop_ = flagValue; @@ -1586,7 +1604,7 @@ bool ReactNativeFeatureFlagsAccessor::useNativeViewConfigsInBridgelessMode() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(86, "useNativeViewConfigsInBridgelessMode"); + markFlagAsAccessed(87, "useNativeViewConfigsInBridgelessMode"); flagValue = currentProvider_->useNativeViewConfigsInBridgelessMode(); useNativeViewConfigsInBridgelessMode_ = flagValue; @@ -1604,7 +1622,7 @@ bool ReactNativeFeatureFlagsAccessor::useNestedScrollViewAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(87, "useNestedScrollViewAndroid"); + markFlagAsAccessed(88, "useNestedScrollViewAndroid"); flagValue = currentProvider_->useNestedScrollViewAndroid(); useNestedScrollViewAndroid_ = flagValue; @@ -1622,7 +1640,7 @@ bool ReactNativeFeatureFlagsAccessor::useSharedAnimatedBackend() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(88, "useSharedAnimatedBackend"); + markFlagAsAccessed(89, "useSharedAnimatedBackend"); flagValue = currentProvider_->useSharedAnimatedBackend(); useSharedAnimatedBackend_ = flagValue; @@ -1640,7 +1658,7 @@ bool ReactNativeFeatureFlagsAccessor::useTraitHiddenOnAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(89, "useTraitHiddenOnAndroid"); + markFlagAsAccessed(90, "useTraitHiddenOnAndroid"); flagValue = currentProvider_->useTraitHiddenOnAndroid(); useTraitHiddenOnAndroid_ = flagValue; @@ -1658,7 +1676,7 @@ bool ReactNativeFeatureFlagsAccessor::useTurboModuleInterop() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(90, "useTurboModuleInterop"); + markFlagAsAccessed(91, "useTurboModuleInterop"); flagValue = currentProvider_->useTurboModuleInterop(); useTurboModuleInterop_ = flagValue; @@ -1676,7 +1694,7 @@ bool ReactNativeFeatureFlagsAccessor::useWindowDensityForDisplayMetricsAndroid() // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(91, "useWindowDensityForDisplayMetricsAndroid"); + markFlagAsAccessed(92, "useWindowDensityForDisplayMetricsAndroid"); flagValue = currentProvider_->useWindowDensityForDisplayMetricsAndroid(); useWindowDensityForDisplayMetricsAndroid_ = flagValue; @@ -1694,7 +1712,7 @@ double ReactNativeFeatureFlagsAccessor::viewCullingOutsetRatio() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(92, "viewCullingOutsetRatio"); + markFlagAsAccessed(93, "viewCullingOutsetRatio"); flagValue = currentProvider_->viewCullingOutsetRatio(); viewCullingOutsetRatio_ = flagValue; @@ -1712,7 +1730,7 @@ bool ReactNativeFeatureFlagsAccessor::viewTransitionEnabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(93, "viewTransitionEnabled"); + markFlagAsAccessed(94, "viewTransitionEnabled"); flagValue = currentProvider_->viewTransitionEnabled(); viewTransitionEnabled_ = flagValue; @@ -1730,7 +1748,7 @@ bool ReactNativeFeatureFlagsAccessor::viewTransitionUseHardwareBitmapAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(94, "viewTransitionUseHardwareBitmapAndroid"); + markFlagAsAccessed(95, "viewTransitionUseHardwareBitmapAndroid"); flagValue = currentProvider_->viewTransitionUseHardwareBitmapAndroid(); viewTransitionUseHardwareBitmapAndroid_ = flagValue; @@ -1748,7 +1766,7 @@ double ReactNativeFeatureFlagsAccessor::virtualViewPrerenderRatio() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(95, "virtualViewPrerenderRatio"); + markFlagAsAccessed(96, "virtualViewPrerenderRatio"); flagValue = currentProvider_->virtualViewPrerenderRatio(); virtualViewPrerenderRatio_ = flagValue; diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h index a6ba9a46d70f..e6543d3f3d24 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<0b7f6d7766d1e038b3909bb7ea634f83>> + * @generated SignedSource<<614c38953c81a140b1f910c23964237f>> */ /** @@ -114,6 +114,7 @@ class ReactNativeFeatureFlagsAccessor { bool shouldTriggerResponderTransferOnScrollAndroid(); bool skipActivityIdentityAssertionOnHostPause(); bool skipUnchangedChildrenInDifferentiator(); + bool sleepUntilNextTimerIOS(); bool syncAndroidClipBoundsWithOverflow(); bool traceTurboModulePromiseRejectionsOnAndroid(); bool updateRuntimeShadowNodeReferencesOnCommit(); @@ -141,7 +142,7 @@ class ReactNativeFeatureFlagsAccessor { std::unique_ptr currentProvider_; bool wasOverridden_; - std::array, 96> accessedFeatureFlags_; + std::array, 97> accessedFeatureFlags_; std::atomic> commonTestFlag_; std::atomic> cdpInteractionMetricsEnabled_; @@ -223,6 +224,7 @@ class ReactNativeFeatureFlagsAccessor { std::atomic> shouldTriggerResponderTransferOnScrollAndroid_; std::atomic> skipActivityIdentityAssertionOnHostPause_; std::atomic> skipUnchangedChildrenInDifferentiator_; + std::atomic> sleepUntilNextTimerIOS_; std::atomic> syncAndroidClipBoundsWithOverflow_; std::atomic> traceTurboModulePromiseRejectionsOnAndroid_; std::atomic> updateRuntimeShadowNodeReferencesOnCommit_; diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h index 76bb504681e3..452bac6251b8 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<46e48274f8b27b9c6136b13048682252>> + * @generated SignedSource<> */ /** @@ -349,6 +349,10 @@ class ReactNativeFeatureFlagsDefaults : public ReactNativeFeatureFlagsProvider { return false; } + bool sleepUntilNextTimerIOS() override { + return false; + } + bool syncAndroidClipBoundsWithOverflow() override { return false; } diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h index bda4465a9160..b7b3b430e475 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<4c4208d9884ebee6fd451c4785f3e79d>> */ /** @@ -767,6 +767,15 @@ class ReactNativeFeatureFlagsDynamicProvider : public ReactNativeFeatureFlagsDef return ReactNativeFeatureFlagsDefaults::skipUnchangedChildrenInDifferentiator(); } + bool sleepUntilNextTimerIOS() override { + auto value = values_["sleepUntilNextTimerIOS"]; + if (!value.isNull()) { + return value.getBool(); + } + + return ReactNativeFeatureFlagsDefaults::sleepUntilNextTimerIOS(); + } + bool syncAndroidClipBoundsWithOverflow() override { auto value = values_["syncAndroidClipBoundsWithOverflow"]; if (!value.isNull()) { diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h index bc1459bef441..52dbf0bdd0e7 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<7626d25a1450564a30de026870f4d094>> + * @generated SignedSource<<99f419365c37683a00183005a0352f20>> */ /** @@ -107,6 +107,7 @@ class ReactNativeFeatureFlagsProvider { virtual bool shouldTriggerResponderTransferOnScrollAndroid() = 0; virtual bool skipActivityIdentityAssertionOnHostPause() = 0; virtual bool skipUnchangedChildrenInDifferentiator() = 0; + virtual bool sleepUntilNextTimerIOS() = 0; virtual bool syncAndroidClipBoundsWithOverflow() = 0; virtual bool traceTurboModulePromiseRejectionsOnAndroid() = 0; virtual bool updateRuntimeShadowNodeReferencesOnCommit() = 0; diff --git a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp index 9d8c414b2686..807bbc260cdd 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<22a4de3d151fb1644095e66cccdf7557>> */ /** @@ -444,6 +444,11 @@ bool NativeReactNativeFeatureFlags::skipUnchangedChildrenInDifferentiator( return ReactNativeFeatureFlags::skipUnchangedChildrenInDifferentiator(); } +bool NativeReactNativeFeatureFlags::sleepUntilNextTimerIOS( + jsi::Runtime& /*runtime*/) { + return ReactNativeFeatureFlags::sleepUntilNextTimerIOS(); +} + bool NativeReactNativeFeatureFlags::syncAndroidClipBoundsWithOverflow( jsi::Runtime& /*runtime*/) { return ReactNativeFeatureFlags::syncAndroidClipBoundsWithOverflow(); diff --git a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h index 4014c7ac6551..6bc6330d1166 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h +++ b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<4d3151d0ce9a022c5b9b74e2a910229d>> + * @generated SignedSource<<752604769167f612aeaa782dc578ec43>> */ /** @@ -198,6 +198,8 @@ class NativeReactNativeFeatureFlags bool skipUnchangedChildrenInDifferentiator(jsi::Runtime& runtime); + bool sleepUntilNextTimerIOS(jsi::Runtime& runtime); + bool syncAndroidClipBoundsWithOverflow(jsi::Runtime& runtime); bool traceTurboModulePromiseRejectionsOnAndroid(jsi::Runtime& runtime); diff --git a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js index ac6c41e0a8e3..515931886343 100644 --- a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js +++ b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js @@ -911,6 +911,17 @@ const definitions: FeatureFlagDefinitions = { }, ossReleaseStage: 'none', }, + sleepUntilNextTimerIOS: { + defaultValue: false, + metadata: { + dateAdded: '2026-10-21', + description: + 'When the next JS timer is more than two frames away, iOS waits for it with a one-shot timer instead of a display link that wakes the JS thread on every frame until the timer is due.', + expectedReleaseValue: true, + purpose: 'experimentation', + }, + ossReleaseStage: 'none', + }, syncAndroidClipBoundsWithOverflow: { defaultValue: false, metadata: { diff --git a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js index c1c6f65c21d1..705c17a6280f 100644 --- a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<5a79107fda7ef89a1d68594cad01b8e7>> + * @generated SignedSource<<1907b07b5f0474bdb706dc90db41b69a>> * @flow strict * @noformat */ @@ -129,6 +129,7 @@ export type ReactNativeFeatureFlags = Readonly<{ shouldTriggerResponderTransferOnScrollAndroid: Getter, skipActivityIdentityAssertionOnHostPause: Getter, skipUnchangedChildrenInDifferentiator: Getter, + sleepUntilNextTimerIOS: Getter, syncAndroidClipBoundsWithOverflow: Getter, traceTurboModulePromiseRejectionsOnAndroid: Getter, updateRuntimeShadowNodeReferencesOnCommit: Getter, @@ -541,6 +542,10 @@ export const skipActivityIdentityAssertionOnHostPause: Getter = createN * When a node changes, the differ leaves out its children that are unchanged in both trees instead of describing and comparing them. */ export const skipUnchangedChildrenInDifferentiator: Getter = createNativeFlagGetter('skipUnchangedChildrenInDifferentiator', false); +/** + * When the next JS timer is more than two frames away, iOS waits for it with a one-shot timer instead of a display link that wakes the JS thread on every frame until the timer is due. + */ +export const sleepUntilNextTimerIOS: Getter = createNativeFlagGetter('sleepUntilNextTimerIOS', false); /** * Override getClipBounds on Android views to return the padding box when overflow is hidden */ diff --git a/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js index 48355705857f..e2ec92e807a9 100644 --- a/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<8162cc18a70ca8fd190a3e3243bd83c9>> * @flow strict * @noformat */ @@ -105,6 +105,7 @@ export interface Spec extends TurboModule { readonly shouldTriggerResponderTransferOnScrollAndroid?: () => boolean; readonly skipActivityIdentityAssertionOnHostPause?: () => boolean; readonly skipUnchangedChildrenInDifferentiator?: () => boolean; + readonly sleepUntilNextTimerIOS?: () => boolean; readonly syncAndroidClipBoundsWithOverflow?: () => boolean; readonly traceTurboModulePromiseRejectionsOnAndroid?: () => boolean; readonly updateRuntimeShadowNodeReferencesOnCommit?: () => boolean; diff --git a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj index 725e8a288236..a1ab652d07d8 100644 --- a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj +++ b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj @@ -56,6 +56,7 @@ E7DB218C22B41FCD005AC45F /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E7DB218B22B41FCD005AC45F /* XCTest.framework */; }; F0D621C32BBB9E38005960AC /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = F0D621C22BBB9E38005960AC /* PrivacyInfo.xcprivacy */; }; F1A0B1C23D4E5F6071829302 /* RCTTurboModuleArrayBufferTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F1A0B1C23D4E5F6071829301 /* RCTTurboModuleArrayBufferTests.mm */; }; + F1A0B1C23D4E5F6071829312 /* RCTTimingTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = F1A0B1C23D4E5F6071829311 /* RCTTimingTests.mm */; }; D1A60AD11B9247A080734F02 /* RCTDevLoadingViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D1A60AD11B9247A080734F01 /* RCTDevLoadingViewTests.m */; }; /* End PBXBuildFile section */ @@ -164,6 +165,7 @@ E7DB218B22B41FCD005AC45F /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = XCTest.framework; sourceTree = DEVELOPER_DIR; }; F0D621C22BBB9E38005960AC /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = ""; }; F1A0B1C23D4E5F6071829301 /* RCTTurboModuleArrayBufferTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTTurboModuleArrayBufferTests.mm; sourceTree = ""; }; + F1A0B1C23D4E5F6071829311 /* RCTTimingTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTTimingTests.mm; sourceTree = ""; }; D1A60AD11B9247A080734F01 /* RCTDevLoadingViewTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTDevLoadingViewTests.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -349,6 +351,7 @@ A975CA6B2C05EADE0043F72A /* RCTNetworkTaskTests.m */, E7DB20BE22B2BAA4005AC45F /* RCTNativeAnimatedNodesManagerTests.m */, E7DB20AD22B2BAA3005AC45F /* RCTPerformanceLoggerTests.m */, + F1A0B1C23D4E5F6071829311 /* RCTTimingTests.mm */, E7DB20C122B2BAA4005AC45F /* RCTUnicodeDecodeTests.m */, E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */, D5D13FB1445C5C19639921CC /* RCTUtilsTests.m */, @@ -762,6 +765,7 @@ E7DB20E022B2BAA6005AC45F /* RCTMethodArgumentTests.m in Sources */, E7DB20E822B2BAA6005AC45F /* RCTModuleMethodTests.mm in Sources */, F1A0B1C23D4E5F6071829302 /* RCTTurboModuleArrayBufferTests.mm in Sources */, + F1A0B1C23D4E5F6071829312 /* RCTTimingTests.mm in Sources */, E7DB20E222B2BAA6005AC45F /* RCTGzipTests.m in Sources */, E7DB20ED22B2BAA6005AC45F /* RCTURLUtilsTests.m in Sources */, E7DB20D322B2BAA6005AC45F /* RCTBlobManagerTests.m in Sources */, diff --git a/packages/rn-tester/RNTesterUnitTests/RCTTimingTests.mm b/packages/rn-tester/RNTesterUnitTests/RCTTimingTests.mm new file mode 100644 index 000000000000..b06c253d80e5 --- /dev/null +++ b/packages/rn-tester/RNTesterUnitTests/RCTTimingTests.mm @@ -0,0 +1,242 @@ +/* + * 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. + */ + +#import + +#import +#import +#import + +#import + +using facebook::react::ReactNativeFeatureFlags; +using facebook::react::ReactNativeFeatureFlagsDefaults; + +namespace { + +class SleepUntilNextTimerFeatureFlags : public ReactNativeFeatureFlagsDefaults { + public: + bool sleepUntilNextTimerIOS() override + { + return true; + } +}; + +} // namespace + +@interface RCTTimingTestsDelegate : NSObject + +@property (nonatomic, readonly) NSMutableArray *calledTimers; + +@end + +@implementation RCTTimingTestsDelegate + +- (instancetype)init +{ + if (self = [super init]) { + _calledTimers = [NSMutableArray new]; + } + return self; +} + +- (void)callTimers:(NSArray *)timers +{ + [_calledTimers addObjectsFromArray:timers]; +} + +- (void)immediatelyCallTimer:(NSNumber *)callbackID +{ + [_calledTimers addObject:callbackID]; +} + +- (void)callIdleCallbacks:(NSNumber *)absoluteFrameStartMS +{ +} + +@end + +@interface RCTTimingTests : XCTestCase +@end + +@implementation RCTTimingTests { + RCTTimingTestsDelegate *_delegate; + RCTTiming *_timing; +} + +- (void)setUpWithSleepUntilNextTimer:(BOOL)sleepUntilNextTimer +{ + ReactNativeFeatureFlags::dangerouslyReset(); + if (sleepUntilNextTimer) { + ReactNativeFeatureFlags::override(std::make_unique()); + } + _delegate = [RCTTimingTestsDelegate new]; + _timing = [[RCTTiming alloc] initWithDelegate:_delegate]; +} + +- (void)tearDown +{ + [_timing invalidate]; + _timing = nil; + _delegate = nil; + ReactNativeFeatureFlags::dangerouslyReset(); + [super tearDown]; +} + +- (void)createTimer:(NSInteger)callbackID duration:(NSTimeInterval)duration +{ + [_timing createTimerForNextFrame:@(callbackID) duration:duration jsSchedulingTime:nil repeats:NO]; +} + +// Runs the run loop until `condition` holds or `timeout` passes. The run loop +// may have no sources, in which case `runMode:beforeDate:` returns at once, so +// keep going until the deadline. +- (BOOL)runRunLoopUntil:(BOOL (^)(void))condition timeout:(NSTimeInterval)timeout +{ + NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:timeout]; + while (!condition()) { + if ([deadline timeIntervalSinceNow] <= 0) { + return NO; + } + NSDate *step = [NSDate dateWithTimeIntervalSinceNow:0.005]; + if (![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:step]) { + [NSThread sleepForTimeInterval:0.005]; + } + } + return YES; +} + +- (void)runRunLoopFor:(NSTimeInterval)duration +{ + [self + runRunLoopUntil:^BOOL { + return NO; + } + timeout:duration]; +} + +- (void)testTimerDueWithinTwoFramesRunsDisplayLink +{ + [self setUpWithSleepUntilNextTimer:YES]; + + [self createTimer:1 duration:0.01]; + + XCTAssertFalse(_timing.paused); +} + +- (void)testTimerFurtherThanTwoFramesSleepsWithFlag +{ + [self setUpWithSleepUntilNextTimer:YES]; + + [self createTimer:1 duration:0.3]; + + XCTAssertTrue(_timing.paused); +} + +- (void)testTimerWithinOneSecondRunsDisplayLinkWithoutFlag +{ + [self setUpWithSleepUntilNextTimer:NO]; + + [self createTimer:1 duration:0.3]; + + XCTAssertFalse(_timing.paused); +} + +- (void)testSleepingTimerIsCalledWhenDueAndNotBefore +{ + [self setUpWithSleepUntilNextTimer:YES]; + + NSDate *start = [NSDate date]; + [self createTimer:1 duration:0.5]; + + [self runRunLoopFor:0.1]; + XCTAssertEqualObjects(_delegate.calledTimers, @[]); + + RCTTimingTestsDelegate *delegate = _delegate; + XCTAssertTrue([self + runRunLoopUntil:^BOOL { + return delegate.calledTimers.count > 0; + } + timeout:2]); + XCTAssertEqualObjects(_delegate.calledTimers, @[ @1 ]); + XCTAssertGreaterThanOrEqual(-[start timeIntervalSinceNow], 0.5); +} + +- (void)testNearerTimerMovesSleepTimerEarlier +{ + [self setUpWithSleepUntilNextTimer:YES]; + + [self createTimer:1 duration:1]; + [self createTimer:2 duration:0.2]; + XCTAssertTrue(_timing.paused); + + RCTTimingTestsDelegate *delegate = _delegate; + XCTAssertTrue([self + runRunLoopUntil:^BOOL { + return delegate.calledTimers.count > 0; + } + timeout:0.8]); + XCTAssertEqualObjects(_delegate.calledTimers, @[ @2 ]); +} + +- (void)testDeletedSleepingTimerIsNotCalled +{ + [self setUpWithSleepUntilNextTimer:YES]; + + [self createTimer:1 duration:0.1]; + [_timing deleteTimer:1]; + + [self runRunLoopFor:0.3]; + XCTAssertEqualObjects(_delegate.calledTimers, @[]); + XCTAssertTrue(_timing.paused); +} + +- (void)testDisplayLinkPausesWhenNextTimerIsFurtherThanTwoFrames +{ + [self setUpWithSleepUntilNextTimer:YES]; + + [self createTimer:1 duration:0.01]; + [self createTimer:2 duration:0.5]; + XCTAssertFalse(_timing.paused); + + // The first timer is due: the frame calls it and keeps the display link + // running for one more frame. + [self runRunLoopFor:0.02]; + [_timing didUpdateFrame:nil]; + XCTAssertEqualObjects(_delegate.calledTimers, @[ @1 ]); + XCTAssertFalse(_timing.paused); + + // Only the second timer is left, about 0.5 s away: the display link pauses + // and a sleep timer calls it. + [_timing didUpdateFrame:nil]; + XCTAssertTrue(_timing.paused); + + RCTTimingTestsDelegate *delegate = _delegate; + XCTAssertTrue([self + runRunLoopUntil:^BOOL { + return delegate.calledTimers.count > 1; + } + timeout:2]); + XCTAssertEqualObjects(_delegate.calledTimers, (@[ @1, @2 ])); +} + +- (void)testDisplayLinkKeepsRunningForTimersWithinOneSecondWithoutFlag +{ + [self setUpWithSleepUntilNextTimer:NO]; + + [self createTimer:1 duration:0.01]; + [self createTimer:2 duration:0.5]; + + [self runRunLoopFor:0.02]; + [_timing didUpdateFrame:nil]; + [_timing didUpdateFrame:nil]; + + XCTAssertEqualObjects(_delegate.calledTimers, @[ @1 ]); + XCTAssertFalse(_timing.paused); +} + +@end