diff --git a/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.h b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.h new file mode 100644 index 00000000000..10655aca7da --- /dev/null +++ b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.h @@ -0,0 +1,26 @@ +/* + * 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 + +#import +#import + +#import + +namespace facebook::react { +class CallInvoker; +class TurboModule; +} // namespace facebook::react + +@interface RCTAnimatedModuleProvider : NSObject + +- (std::shared_ptr)getTurboModule:(const std::string &)name + jsInvoker: + (std::shared_ptr)jsInvoker; + +@end diff --git a/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm new file mode 100644 index 00000000000..ad4fb658965 --- /dev/null +++ b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm @@ -0,0 +1,66 @@ +/* + * 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 "RCTAnimatedModuleProvider.h" + +#import + +#if TARGET_OS_OSX +#import +#else +#import +#endif +#import +#import + +@implementation RCTAnimatedModuleProvider { +#if TARGET_OS_OSX + RCTPlatformDisplayLink *_displayLink; +#else + CADisplayLink *_displayLink; +#endif + std::function _onRender; +} + +- (void)_onDisplayLinkTick +{ + if (_displayLink != nullptr && _onRender != nullptr) { + _onRender(); + } +} + +- (std::shared_ptr)getTurboModule:(const std::string &)name + jsInvoker:(std::shared_ptr)jsInvoker +{ + if (facebook::react::ReactNativeFeatureFlags::cxxNativeAnimatedEnabled()) { + if (name == facebook::react::AnimatedModule::kModuleName) { + auto provider = std::make_shared( + [self](std::function &&onRender) { + _onRender = onRender; + if (_displayLink == nil) { +#if TARGET_OS_OSX + _displayLink = [RCTPlatformDisplayLink displayLinkWithTarget:self selector:@selector(_onDisplayLinkTick)]; +#else + _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_onDisplayLinkTick)]; +#endif + [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; + } + }, + [self]() { + if (_displayLink != nil) { + [_displayLink invalidate]; + _displayLink = nil; + _onRender = nullptr; + } + }); + return std::make_shared(std::move(jsInvoker), std::move(provider)); + } + } + return nullptr; +} + +@end