Skip to content

Commit

Permalink
Force to execute evaluateJavascript: func on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
vince-hz committed Dec 7, 2023
1 parent 057083e commit 927b521
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
5 changes: 4 additions & 1 deletion Whiteboard/Classes/Displayer/WhiteAudioEffectMixerBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ - (void)receiveRouteChangedNotification:(NSNotification *)notification {
}

- (void)resumeAllAudioInterruptByAudioSessionChanged {
[self.bridge evaluateJavaScript:@"window.postMessage({name: 'resumeAllAudioInterruptByAudioSessionChanged'})" completionHandler:^(id _Nullable r, NSError * _Nullable error) {}];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.bridge evaluateJavaScript:@"window.postMessage({name: 'resumeAllAudioInterruptByAudioSessionChanged'})" completionHandler:^(id _Nullable r, NSError * _Nullable error) {}];
});
}

// Returns volume.
Expand Down
36 changes: 19 additions & 17 deletions Whiteboard/Classes/Displayer/WhiteDisplayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,26 @@ - (void)getSceneSnapshotImage:(NSString *)scenePath completion:(void (^)(UIImage
- (void)getLocalSnapShotWithCompletion:(void (^)(UIImage * _Nullable, NSError * _Nullable))completionHandler
{
__weak typeof(self) weakSelf = self;
[self.bridge evaluateJavaScript:@"window.postMessage({type: '@slide/_request_frozen_'}, '*')" completionHandler:^(id _Nullable value, NSError * _Nullable error) {
if (error) {
completionHandler(nil, error);
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
WhiteBoardView *bridge = weakSelf.bridge;
CGSize whiteboardSize = bridge.bounds.size;
UIGraphicsBeginImageContextWithOptions(whiteboardSize, FALSE, UIScreen.mainScreen.scale);
[bridge drawViewHierarchyInRect:CGRectMake(0, 0, whiteboardSize.width, whiteboardSize.height) afterScreenUpdates:YES];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
completionHandler(snapshot, nil);
[bridge evaluateJavaScript:@"window.postMessage({type: '@slide/_request_release_'}, '*')" completionHandler:^(id _Nullable value, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.bridge evaluateJavaScript:@"window.postMessage({type: '@slide/_request_frozen_'}, '*')" completionHandler:^(id _Nullable value, NSError * _Nullable error) {
if (error) {
completionHandler(nil, error);
return;
}];
});
}];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
WhiteBoardView *bridge = weakSelf.bridge;
CGSize whiteboardSize = bridge.bounds.size;
UIGraphicsBeginImageContextWithOptions(whiteboardSize, FALSE, UIScreen.mainScreen.scale);
[bridge drawViewHierarchyInRect:CGRectMake(0, 0, whiteboardSize.width, whiteboardSize.height) afterScreenUpdates:YES];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
completionHandler(snapshot, nil);
[bridge evaluateJavaScript:@"window.postMessage({type: '@slide/_request_release_'}, '*')" completionHandler:^(id _Nullable value, NSError * _Nullable error) {
return;
}];
});
}];
});
}

- (void)getWindowManagerAttributesWithResult:(void (^)(NSDictionary * _Nonnull))result
Expand Down
36 changes: 22 additions & 14 deletions Whiteboard/Classes/SDK/WhiteSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,23 @@ - (void)getSlideVolumeWithCompletionHandler:(void (^)(CGFloat, NSError * _Nonnul
{
self.requestSlideVolumeHandler = completionHandler;
__weak typeof(self) weakSelf = self;
[self.bridge evaluateJavaScript:@"window.postMessage({type: \"@slide/_get_volume_\"}, '*');" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (error) {
completionHandler(0, error);
weakSelf.requestSlideVolumeHandler = nil;
return;
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.bridge evaluateJavaScript:@"window.postMessage({type: \"@slide/_get_volume_\"}, '*');" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (error) {
completionHandler(0, error);
weakSelf.requestSlideVolumeHandler = nil;
return;
}
}];
});
}

- (void)updateSlideVolume:(CGFloat)volume
{
[self.bridge evaluateJavaScript:[NSString stringWithFormat:@"window.postMessage ({'type': \"@slide/_update_volume_\", 'volume': %f}, '*')", volume] completionHandler:nil];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.bridge evaluateJavaScript:[NSString stringWithFormat:@"window.postMessage ({'type': \"@slide/_update_volume_\", 'volume': %f}, '*')", volume] completionHandler:nil];
});
}

#pragma mark - CommonCallback
Expand Down Expand Up @@ -181,12 +186,15 @@ - (void)requestSlideLogToFilePath:(NSString *)path result:(void(^)(BOOL success,
self.slideLogPath = path;
self.slideLogFileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
NSString *logJs = [NSString stringWithFormat:@"window.postMessage({type: '@slide/_request_log_', sessionId: '%@'}, '*')", sessionId];
[self.bridge evaluateJavaScript:logJs completionHandler:^(id _Nullable value, NSError * _Nullable error) {
if (error) {
result(NO, error);
[self cleanSlideLogResource];
}
}];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.bridge evaluateJavaScript:logJs completionHandler:^(id _Nullable value, NSError * _Nullable error) {
if (error) {
result(NO, error);
[weakSelf cleanSlideLogResource];
}
}];
});
}

- (void)cleanSlideLogResource
Expand Down

0 comments on commit 927b521

Please sign in to comment.