|
3 | 3 | #import "RCTUIManager.h"
|
4 | 4 | #import "RCTWKWebView.h"
|
5 | 5 |
|
| 6 | +@interface RCTWKWebViewManager () <RCTWKWebViewDelegate> |
| 7 | +@end |
| 8 | + |
6 | 9 | @implementation RCTWKWebViewManager
|
| 10 | +{ |
| 11 | + NSConditionLock *_shouldStartLoadLock; |
| 12 | + BOOL _shouldStartLoad; |
| 13 | +} |
7 | 14 |
|
8 | 15 | RCT_EXPORT_MODULE()
|
9 | 16 |
|
10 | 17 | - (UIView *)view
|
11 | 18 | {
|
12 |
| - return [RCTWKWebView new]; |
| 19 | + RCTWKWebView *webView = [RCTWKWebView new]; |
| 20 | + webView.delegate = self; |
| 21 | + return webView; |
13 | 22 | }
|
14 | 23 |
|
15 | 24 | RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
|
16 | 25 | RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
|
17 | 26 | RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
|
18 | 27 | RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
|
| 28 | +RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock) |
19 | 29 | RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
|
20 | 30 |
|
21 | 31 | /**
|
@@ -105,4 +115,38 @@ - (UIView *)view
|
105 | 115 | }];
|
106 | 116 | }
|
107 | 117 |
|
| 118 | +#pragma mark - Exported synchronous methods |
| 119 | + |
| 120 | +- (BOOL) webView:(RCTWKWebView *)webView |
| 121 | +shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request |
| 122 | + withCallback:(RCTDirectEventBlock)callback |
| 123 | +{ |
| 124 | + _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()]; |
| 125 | + _shouldStartLoad = YES; |
| 126 | + request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition); |
| 127 | + callback(request); |
| 128 | + |
| 129 | + // Block the main thread for a maximum of 250ms until the JS thread returns |
| 130 | + if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) { |
| 131 | + BOOL returnValue = _shouldStartLoad; |
| 132 | + [_shouldStartLoadLock unlock]; |
| 133 | + _shouldStartLoadLock = nil; |
| 134 | + return returnValue; |
| 135 | + } else { |
| 136 | + RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES"); |
| 137 | + return YES; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier) |
| 142 | +{ |
| 143 | + if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) { |
| 144 | + _shouldStartLoad = result; |
| 145 | + [_shouldStartLoadLock unlockWithCondition:0]; |
| 146 | + } else { |
| 147 | + RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: " |
| 148 | + "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition); |
| 149 | + } |
| 150 | +} |
| 151 | + |
108 | 152 | @end
|
0 commit comments