Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): ensure UI thread is not blocked by sending events #111

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-native/ios/HotUpdater/HotUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@interface HotUpdater : RCTEventEmitter <RCTBridgeModule>
#endif // RCT_NEW_ARCH_ENABLED

@property (nonatomic, assign) NSTimeInterval lastUpdateTime;
+ (NSURL *)bundleURL;

@end
21 changes: 19 additions & 2 deletions packages/react-native/ios/HotUpdater/HotUpdater.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ @implementation HotUpdater {
bool hasListeners;
}


- (instancetype)init {
self = [super init];
if (self) {
_lastUpdateTime = 0;
}
return self;
}

RCT_EXPORT_MODULE();

#pragma mark - Bundle URL Management
Expand Down Expand Up @@ -179,8 +188,16 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
if (task.countOfBytesExpectedToReceive > 0) {
double progress = (double)task.countOfBytesReceived / (double)task.countOfBytesExpectedToReceive;

// Send progress to React Native
[self sendEventWithName:@"onProgress" body:@{@"progress": @(progress)}];
// Get current timestamp
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970] * 1000; // Convert to milliseconds

// Send event only if 200ms has passed OR progress is 100%
if ((currentTime - self.lastUpdateTime) >= 200 || progress >= 1.0) {
self.lastUpdateTime = currentTime; // Update last event timestamp

// Send progress to React Native
[self sendEventWithName:@"onProgress" body:@{@"progress": @(progress)}];
}
}
}
}
Expand Down