From 0b1f53698b84768151dc698aeadb5df9cd501d5c Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Wed, 27 Feb 2019 21:09:50 +0200 Subject: [PATCH] [android] Make SvgView.drawChildren synchronized Fix race-condition https://github.com/react-native-community/react-native-svg/issues/948 Refactor toDataUrl --- .../java/com/horcrux/svg/SvgViewModule.java | 26 +++++------ elements/Svg.js | 6 +-- ios/ViewManagers/RNSVGSvgViewManager.m | 44 ++++++++----------- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/SvgViewModule.java b/android/src/main/java/com/horcrux/svg/SvgViewModule.java index a7573551..a5cc4927 100644 --- a/android/src/main/java/com/horcrux/svg/SvgViewModule.java +++ b/android/src/main/java/com/horcrux/svg/SvgViewModule.java @@ -26,27 +26,21 @@ class SvgViewModule extends ReactContextBaseJavaModule { } - @ReactMethod - public void toDataURL(int tag, Callback successCallback) { - SvgView svg = SvgViewManager.getSvgViewByTag(tag); - - if (svg != null) { - successCallback.invoke(svg.toDataURL()); - } - } - - @ReactMethod public void toDataURL(int tag, ReadableMap options, Callback successCallback) { SvgView svg = SvgViewManager.getSvgViewByTag(tag); if (svg != null) { - successCallback.invoke( - svg.toDataURL( - options.getInt("width"), - options.getInt("height") - ) - ); + if (options != null) { + successCallback.invoke( + svg.toDataURL( + options.getInt("width"), + options.getInt("height") + ) + ); + } else { + successCallback.invoke(svg.toDataURL()); + } } } } diff --git a/elements/Svg.js b/elements/Svg.js index 30f7cbbc..2ed783b8 100644 --- a/elements/Svg.js +++ b/elements/Svg.js @@ -54,11 +54,7 @@ export default class Svg extends Shape { return; } const handle = findNodeHandle(this.root); - if (options) { - RNSVGSvgViewManager.toDataURL(handle, options, callback); - } else { - RNSVGSvgViewManager.toDataURL(handle, callback); - } + RNSVGSvgViewManager.toDataURL(handle, options, callback); }; render() { diff --git a/ios/ViewManagers/RNSVGSvgViewManager.m b/ios/ViewManagers/RNSVGSvgViewManager.m index 97489a1c..bbd015be 100644 --- a/ios/ViewManagers/RNSVGSvgViewManager.m +++ b/ios/ViewManagers/RNSVGSvgViewManager.m @@ -30,39 +30,31 @@ RCT_EXPORT_VIEW_PROPERTY(align, NSString) RCT_EXPORT_VIEW_PROPERTY(meetOrSlice, RNSVGVBMOS) RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor) -RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag callback:(RCTResponseSenderBlock)callback) -{ - [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { - __kindof UIView *view = viewRegistry[reactTag]; - if ([view isKindOfClass:[RNSVGSvgView class]]) { - RNSVGSvgView *svg = view; - callback(@[[svg getDataURL]]); - } else { - RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view); - } - }]; -} - RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { __kindof UIView *view = viewRegistry[reactTag]; if ([view isKindOfClass:[RNSVGSvgView class]]) { RNSVGSvgView *svg = view; - id width = [options objectForKey:@"width"]; - id height = [options objectForKey:@"height"]; - if (![width isKindOfClass:NSNumber.class] || - ![height isKindOfClass:NSNumber.class]) { - RCTLogError(@"Invalid width or height given to toDataURL"); - return; - } - NSNumber* w = width; - NSInteger wi = (NSInteger)[w intValue]; - NSNumber* h = height; - NSInteger hi = (NSInteger)[h intValue]; + if (options == nil) { + RNSVGSvgView *svg = view; + callback(@[[svg getDataURL]]); + } else { + id width = [options objectForKey:@"width"]; + id height = [options objectForKey:@"height"]; + if (![width isKindOfClass:NSNumber.class] || + ![height isKindOfClass:NSNumber.class]) { + RCTLogError(@"Invalid width or height given to toDataURL"); + return; + } + NSNumber* w = width; + NSInteger wi = (NSInteger)[w intValue]; + NSNumber* h = height; + NSInteger hi = (NSInteger)[h intValue]; - CGSize bounds = CGSizeMake(wi, hi); - callback(@[[svg getDataURLwithBounds:bounds]]); + CGSize bounds = CGSizeMake(wi, hi); + callback(@[[svg getDataURLwithBounds:bounds]]); + } } else { RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view); }