Skip to content

Commit

Permalink
fix(🍏): use dedicated bridge (#2760)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: William Candillon <[email protected]>
Co-authored-by: Kudo Chien <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 21eb223 commit 2cfa890
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/skia/ios/RNSkia-iOS/SkiaDomView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ @implementation SkiaDomView

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
auto skManager = [[self skiaManager] skManager];
// Pass SkManager as a raw pointer to avoid circular dependenciesr
// Pass SkManager as a raw pointer to avoid circular dependencies
auto skManager = [SkiaManager latestActiveSkManager].get();
[self
initCommon:skManager.get()
initCommon:skManager
factory:[](std::shared_ptr<RNSkia::RNSkPlatformContext> context) {
return std::make_shared<RNSkiOSView<RNSkia::RNSkDomView>>(context);
}];
Expand Down
3 changes: 2 additions & 1 deletion packages/skia/ios/RNSkia-iOS/SkiaDomViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ @implementation SkiaDomViewManager
RCT_EXPORT_MODULE(SkiaDomView)

- (SkiaManager *)skiaManager {
auto bridge = [RCTBridge currentBridge];
auto bridge = self.bridge;
RCTAssert(bridge, @"Bridge must not be nil.");
auto skiaModule = (RNSkiaModule *)[bridge moduleForName:@"RNSkiaModule"];
return [skiaModule manager];
}
Expand Down
6 changes: 6 additions & 0 deletions packages/skia/ios/RNSkia-iOS/SkiaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)
jsInvoker;

#ifdef RCT_NEW_ARCH_ENABLED
// Fabric components do not have a better way to interact with TurboModules.
// Workaround to get the SkManager instance from singleton.
+ (std::shared_ptr<RNSkia::RNSkManager>)latestActiveSkManager;
#endif // RCT_NEW_ARCH_ENABLED

@end
17 changes: 17 additions & 0 deletions packages/skia/ios/RNSkia-iOS/SkiaManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "RNSkiOSPlatformContext.h"

static __weak SkiaManager *sharedInstance = nil;

@implementation SkiaManager {
std::shared_ptr<RNSkia::RNSkManager> _skManager;
__weak RCTBridge *weakBridge;
Expand All @@ -29,6 +31,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
jsInvoker {
self = [super init];
if (self) {
sharedInstance = self;
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)bridge;
if (cxxBridge.runtime) {

Expand All @@ -44,4 +47,18 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
return self;
}

- (void)dealloc
{
sharedInstance = nil;
}

#ifdef RCT_NEW_ARCH_ENABLED
+ (std::shared_ptr<RNSkia::RNSkManager>)latestActiveSkManager
{
if (sharedInstance != nil) {
return [sharedInstance skManager];
}
return nullptr;
}
#endif // RCT_NEW_ARCH_ENABLED
@end
6 changes: 3 additions & 3 deletions packages/skia/ios/RNSkia-iOS/SkiaPictureView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ @implementation SkiaPictureView

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
auto skManager = [[self skiaManager] skManager];
// Pass SkManager as a raw pointer to avoid circular dependenciesr
[self initCommon:skManager.get()
// Pass SkManager as a raw pointer to avoid circular dependencies
auto skManager = [SkiaManager latestActiveSkManager].get();
[self initCommon:skManager
factory:[](std::shared_ptr<RNSkia::RNSkPlatformContext> context) {
return std::make_shared<RNSkiOSView<RNSkia::RNSkPictureView>>(
context);
Expand Down
3 changes: 2 additions & 1 deletion packages/skia/ios/RNSkia-iOS/SkiaPictureViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ @implementation SkiaPictureViewManager
RCT_EXPORT_MODULE(SkiaPictureView)

- (SkiaManager *)skiaManager {
auto bridge = [RCTBridge currentBridge];
auto bridge = self.bridge;
RCTAssert(bridge, @"Bridge must not be nil.");
auto skiaModule = (RNSkiaModule *)[bridge moduleForName:@"RNSkiaModule"];
return [skiaModule manager];
}
Expand Down
1 change: 0 additions & 1 deletion packages/skia/ios/RNSkia-iOS/SkiaUIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
factory:(std::function<std::shared_ptr<RNSkBaseiOSView>(
std::shared_ptr<RNSkia::RNSkPlatformContext>)>)factory;
- (std::shared_ptr<RNSkBaseiOSView>)impl;
- (SkiaManager *)skiaManager;

- (void)setDebugMode:(bool)debugMode;
- (void)setOpaque:(bool)opaque;
Expand Down
8 changes: 1 addition & 7 deletions packages/skia/ios/RNSkia-iOS/SkiaUIView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ - (void)initCommon:(RNSkia::RNSkManager *)manager
_factory = factory;
}

- (SkiaManager *)skiaManager {
auto bridge = [RCTBridge currentBridge];
auto skiaModule = (RNSkiaModule *)[bridge moduleForName:@"RNSkiaModule"];
return [skiaModule manager];
}

- (void)willInvalidateModules {
_impl = nullptr;
_manager = nullptr;
Expand Down Expand Up @@ -114,7 +108,7 @@ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask {
// this flag is only set when the view is inserted and we want to set the
// manager here since the view could be recycled or the app could be
// refreshed and we would have a stale manager then
_manager = [[self skiaManager] skManager].get();
_manager = [SkiaManager latestActiveSkManager].get();
}
}
#endif // RCT_NEW_ARCH_ENABLED
Expand Down

0 comments on commit 2cfa890

Please sign in to comment.