Skip to content

Commit ebbbe17

Browse files
author
Ron Radtke
committed
Revert "ios part but no idea if working"
This reverts commit 7bb263d.
1 parent 5a34467 commit ebbbe17

9 files changed

+80
-46
lines changed

fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {ReactNativeBlobUtilConfig} from 'types';
22
import URIUtil from './utils/uri';
33
import fs from './fs';
44
import getUUID from './utils/uuid';
5-
import {DeviceEventEmitter, NativeEventEmitter} from 'react-native';
5+
import {DeviceEventEmitter, NativeEventEmitter, NativeModules} from 'react-native';
66
import {FetchBlobResponse} from './class/ReactNativeBlobUtilBlobResponse';
77
import CanceledFetchError from './class/ReactNativeBlobUtilCanceledFetchError';
88
import ReactNativeBlobUtil from './codegenSpecs/NativeBlobUtils';

ios/ReactNativeBlobUtil/ReactNativeBlobUtil.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,22 @@
1818
#import <React/RCTBridge.h>
1919
#import <React/RCTEventDispatcher.h>
2020
#import <React/RCTBridgeModule.h>
21-
#import <React/RCTEventEmitter.h>
2221
#else
2322
#import "RCTBridgeModule.h"
2423
#import "RCTLog.h"
2524
#import "RCTRootView.h"
2625
#import "RCTBridge.h"
2726
#import "RCTEventDispatcher.h"
28-
#import "RCTEventEmitter.h"
2927
#endif
3028

3129
#import <UIKit/UIKit.h>
3230

33-
3431
#if RCT_NEW_ARCH_ENABLED
3532
#import <React-Codegen/ReactNativeBlobUtilSpec/ReactNativeBlobUtilSpec.h>
3633
#endif
3734

3835

39-
@interface ReactNativeBlobUtil : RCTEventEmitter <RCTBridgeModule, UIDocumentInteractionControllerDelegate> {
36+
@interface ReactNativeBlobUtil : NSObject <RCTBridgeModule, UIDocumentInteractionControllerDelegate> {
4037

4138
NSString * filePathPrefix;
4239

@@ -46,7 +43,6 @@
4643
@property (retain) UIDocumentInteractionController * documentController;
4744

4845
+ (RCTEventDispatcher *)getRCTEventDispatcher;
49-
+(void) emitEvent;
5046

5147
@end
5248

ios/ReactNativeBlobUtil/ReactNativeBlobUtil.mm

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,10 @@
1515
#import <ReactNativeBlobUtilSpec/ReactNativeBlobUtilSpec.h>
1616
#endif
1717

18+
__strong RCTEventDispatcher * eventDispatcherRef;
1819
dispatch_queue_t commonTaskQueue;
1920
dispatch_queue_t fsQueue;
2021

21-
bool hasListeners;
22-
23-
// Will be called when this module's first listener is added.
24-
-(void)startObserving {
25-
hasListeners = YES;
26-
// Set up any upstream listeners or background tasks as necessary
27-
}
28-
29-
// Will be called when this module's last listener is removed, or on dealloc.
30-
-(void)stopObserving {
31-
hasListeners = NO;
32-
// Remove upstream listeners, stop unnecessary background tasks
33-
}
34-
35-
- (void)emitEvent:(NSString *)name body(NSString *) body
36-
{
37-
NSString *eventName = name;
38-
if (hasListeners) {// Only send events if anyone is listening
39-
[self sendEventWithName:name body:body];
40-
}
41-
}
42-
4322
////////////////////////////////////////
4423
//
4524
// Exported native methods
@@ -88,6 +67,10 @@ - (id) init {
8867
if(![[NSFileManager defaultManager] fileExistsAtPath: [ReactNativeBlobUtilFS getTempPath] isDirectory:&isDir]) {
8968
[[NSFileManager defaultManager] createDirectoryAtPath:[ReactNativeBlobUtilFS getTempPath] withIntermediateDirectories:YES attributes:nil error:NULL];
9069
}
70+
dispatch_async(dispatch_get_main_queue(), ^{
71+
eventDispatcherRef = bridge.eventDispatcher;
72+
[ReactNativeBlobUtilNetwork emitExpiredTasks: eventDispatcherRef];
73+
});
9174

9275
return self;
9376
}
@@ -152,6 +135,7 @@ - (NSDictionary *)constantsToExport
152135
{
153136
[[ReactNativeBlobUtilNetwork sharedInstance] sendRequest:options
154137
contentLength:bodyLength
138+
eventDispatcher:eventDispatcherRef
155139
taskId:taskId
156140
withRequest:req
157141
callback:callback];
@@ -188,6 +172,7 @@ - (NSDictionary *)constantsToExport
188172
{
189173
[[ReactNativeBlobUtilNetwork sharedInstance] sendRequest:options
190174
contentLength:bodyLength
175+
eventDispatcher:eventDispatcherRef
191176
taskId:taskId
192177
withRequest:req
193178
callback:callback];
@@ -381,7 +366,7 @@ - (void)writeFileArray:(NSString *)path
381366
appendData:(BOOL)append
382367
callback:(RCTResponseSenderBlock)callback)
383368
{
384-
ReactNativeBlobUtilFS * fileStream = [[ReactNativeBlobUtilFS alloc] init];
369+
ReactNativeBlobUtilFS * fileStream = [[ReactNativeBlobUtilFS alloc] initWithEventDispatcherRef:eventDispatcherRef];
385370
NSFileManager * fm = [NSFileManager defaultManager];
386371
NSString * folder = [path stringByDeletingLastPathComponent];
387372
NSError* err = nil;
@@ -711,7 +696,7 @@ - (void)hash:(NSString *)path
711696
}
712697

713698
dispatch_async(fsQueue, ^{
714-
[ReactNativeBlobUtilFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId];
699+
[ReactNativeBlobUtilFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId eventDispatcherRef:eventDispatcherRef];
715700
});
716701
}
717702

@@ -892,6 +877,13 @@ - (UIViewController *)documentInteractionControllerViewControllerForPreview: (UI
892877
return window.rootViewController;
893878
}
894879

880+
# pragma mark - check expired network events
881+
882+
RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
883+
{
884+
[ReactNativeBlobUtilNetwork emitExpiredTasks:eventDispatcherRef];
885+
}
886+
895887
# pragma mark - Android Only methods
896888
// These methods are required because in the New Arch we have a single spec for both platforms
897889
- (void)actionViewIntent:(NSString *) path

ios/ReactNativeBlobUtilFS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
// constructor
9292
- (id) init;
9393
- (id)initWithCallback:(RCTResponseSenderBlock)callback;
94+
- (id)initWithEventDispatcherRef:(RCTEventDispatcher *)eventDispatcherRef;
9495

9596
// file stream
9697
- (void) openWithDestination;

ios/ReactNativeBlobUtilFS.mm

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ - (id)initWithCallback:(RCTResponseSenderBlock)callback {
5858
return self;
5959
}
6060

61+
- (id)initWithEventDispatcherRef:(RCTEventDispatcher *)eventDispatcherRef {
62+
self = [super init];
63+
self.eventDispatcher = eventDispatcherRef;
64+
return self;
65+
}
66+
6167
// static member getter
6268
+ (NSDictionary *) getFileStreams {
6369

@@ -154,9 +160,11 @@ + (void) readStream:(NSString *)uri
154160
bufferSize:(int)bufferSize
155161
tick:(int)tick
156162
streamId:(NSString *)streamId
163+
eventDispatcherRef:(RCTEventDispatcher *)eventDispatcherRef
157164
{
158165
[[self class] getPathFromUri:uri completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
159166

167+
__block RCTEventDispatcher * event = eventDispatcherRef;
160168
__block int read = 0;
161169
__block int backoff = tick *1000;
162170
__block int chunkSize = bufferSize;
@@ -171,15 +179,15 @@ + (void) readStream:(NSString *)uri
171179
{
172180
NSString * message = [NSString stringWithFormat:@"File does not exist at path %@", path];
173181
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"ENOENT", @"detail": message };
174-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
182+
[event sendDeviceEventWithName:streamId body:payload];
175183
free(buffer);
176184
return ;
177185
}
178186
NSInputStream * stream = [[NSInputStream alloc] initWithFileAtPath:path];
179187
[stream open];
180188
while((read = [stream read:buffer maxLength:bufferSize]) > 0)
181189
{
182-
[[self class] emitDataChunks:[NSData dataWithBytes:buffer length:read] encoding:encoding streamId:streamId];
190+
[[self class] emitDataChunks:[NSData dataWithBytes:buffer length:read] encoding:encoding streamId:streamId event:event];
183191
if(tick > 0)
184192
{
185193
usleep(backoff);
@@ -204,7 +212,7 @@ + (void) readStream:(NSString *)uri
204212
else
205213
{
206214
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EINVAL", @"detail": @"Unable to resolve URI" };
207-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
215+
[event sendDeviceEventWithName:streamId body:payload];
208216
}
209217
// release buffer
210218
if(buffer != nil)
@@ -214,12 +222,12 @@ + (void) readStream:(NSString *)uri
214222
@catch (NSError * err)
215223
{
216224
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EUNSPECIFIED", @"detail": [err description] };
217-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
225+
[event sendDeviceEventWithName:streamId body:payload];
218226
}
219227
@finally
220228
{
221229
NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
222-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
230+
[event sendDeviceEventWithName:streamId body:payload];
223231
}
224232

225233
}];
@@ -228,7 +236,7 @@ + (void) readStream:(NSString *)uri
228236
}
229237

230238
// send read stream chunks via native event emitter
231-
+ (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(NSString *)streamId
239+
+ (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(NSString *)streamId event:(RCTEventDispatcher *)event
232240
{
233241
@try
234242
{
@@ -239,12 +247,12 @@ + (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(
239247
@"event": FS_EVENT_DATA,
240248
@"detail" : [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
241249
};
242-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
250+
[event sendDeviceEventWithName:streamId body:payload];
243251
}
244252
else if ([[encoding lowercaseString] isEqualToString:@"base64"])
245253
{
246254
NSDictionary * payload = @{ @"event": FS_EVENT_DATA, @"detail" : [data base64EncodedStringWithOptions:0] };
247-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
255+
[event sendDeviceEventWithName:streamId body:payload];
248256
}
249257
else if([[encoding lowercaseString] isEqualToString:@"ascii"])
250258
{
@@ -262,19 +270,21 @@ + (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(
262270
}
263271

264272
NSDictionary * payload = @{ @"event": FS_EVENT_DATA, @"detail" : asciiArray };
265-
[ReactNativeBlobUtil emitEvent:streamId body:payload];
273+
[event sendDeviceEventWithName:streamId body:payload];
266274
}
267275

268276
}
269277
@catch (NSException * ex)
270278
{
271279
NSString * message = [NSString stringWithFormat:@"Failed to convert data to '%@' encoded string, this might due to the source data is not able to convert using this encoding. source = %@", encoding, [ex description]];
272-
[ReactNativeBlobUtil emitEvent:streamId
280+
[event
281+
sendDeviceEventWithName:streamId
273282
body:@{
274283
@"event" : MSG_EVENT_ERROR,
275284
@"detail" : message
276285
}];
277-
[ReactNativeBlobUtil emitEvent:MSG_EVENT
286+
[event
287+
sendDeviceEventWithName:MSG_EVENT
278288
body:@{
279289
@"event" : MSG_EVENT_WARN,
280290
@"detail" : message

ios/ReactNativeBlobUtilNetwork.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030

3131
+ (ReactNativeBlobUtilNetwork* _Nullable)sharedInstance;
3232
+ (NSMutableDictionary * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
33+
+ (void) emitExpiredTasks:(RCTEventDispatcher *) eventDispatcher;
3334

3435
- (nullable id) init;
3536
- (void) sendRequest:(NSDictionary * _Nullable )options
3637
contentLength:(long)contentLength
38+
eventDispatcher:(RCTEventDispatcher * _Nullable)eventDispatcherRef
3739
taskId:(NSString * _Nullable)taskId
3840
withRequest:(NSURLRequest * _Nullable)req
3941
callback:(_Nullable RCTResponseSenderBlock) callback;

ios/ReactNativeBlobUtilNetwork.mm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
8080
ReactNativeBlobUtilRequest *request = [[ReactNativeBlobUtilRequest alloc] init];
8181
[request sendRequest:options
8282
contentLength:contentLength
83+
eventDispatcher:eventDispatcherRef
8384
taskId:taskId
8485
withRequest:req
8586
taskOperationQueue:self.taskQueue
@@ -157,4 +158,24 @@ + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
157158
return mheaders;
158159
}
159160

161+
// #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
162+
+ (void) emitExpiredTasks:(RCTEventDispatcher *)eventDispatcher
163+
{
164+
@synchronized ([ReactNativeBlobUtilNetwork class]){
165+
NSEnumerator * emu = [expirationTable keyEnumerator];
166+
NSString * key;
167+
168+
while ((key = [emu nextObject]))
169+
{
170+
id args = @{ @"taskId": key };
171+
[eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
172+
173+
}
174+
175+
// clear expired task entries
176+
[expirationTable removeAllObjects];
177+
expirationTable = [[NSMapTable alloc] init];
178+
}
179+
}
180+
160181
@end

ios/ReactNativeBlobUtilRequest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
- (void) sendRequest:(NSDictionary * _Nullable )options
4040
contentLength:(long)contentLength
41+
eventDispatcher:(RCTEventDispatcher * _Nullable)eventDispatcherRef
4142
taskId:(NSString * _Nullable)taskId
4243
withRequest:(NSURLRequest * _Nullable)req
4344
taskOperationQueue:(NSOperationQueue * _Nonnull)operationQueue

ios/ReactNativeBlobUtilRequest.mm

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88

99
#import "ReactNativeBlobUtilRequest.h"
1010

11-
#import "ReactNativeBlobUtil.h"
1211
#import "ReactNativeBlobUtilFS.h"
1312
#import "ReactNativeBlobUtilConst.h"
1413
#import "ReactNativeBlobUtilFileTransformer.h"
1514
#import "ReactNativeBlobUtilReqBuilder.h"
1615

1716
#import <CommonCrypto/CommonDigest.h>
1817

18+
#if __has_include(<React/RCTAssert.h>)
19+
#import <React/RCTEventDispatcherProtocol.h>
20+
#else
21+
#import "RCTEventDispatcherProtocol.h"
22+
#endif
1923

2024

2125
typedef NS_ENUM(NSUInteger, ResponseFormat) {
@@ -49,6 +53,7 @@ @implementation ReactNativeBlobUtilRequest
4953
@synthesize receivedBytes;
5054
@synthesize respData;
5155
@synthesize callback;
56+
@synthesize eventDispatcher;
5257
@synthesize options;
5358
@synthesize error;
5459

@@ -68,6 +73,7 @@ - (NSString *)md5:(NSString *)input {
6873
// send HTTP request
6974
- (void) sendRequest:(__weak NSDictionary * _Nullable )options
7075
contentLength:(long) contentLength
76+
eventDispatcher:(RCTEventDispatcher * _Nullable)eventDispatcherRef
7177
taskId:(NSString * _Nullable)taskId
7278
withRequest:(__weak NSURLRequest * _Nullable)req
7379
taskOperationQueue:(NSOperationQueue * _Nonnull)operationQueue
@@ -76,6 +82,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
7682
self.taskId = taskId;
7783
self.respData = [[NSMutableData alloc] initWithLength:0];
7884
self.callback = callback;
85+
self.eventDispatcher = eventDispatcherRef;
7986
self.expectedBytes = 0;
8087
self.receivedBytes = 0;
8188
self.options = options;
@@ -206,7 +213,8 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
206213

207214
if (self.isServerPush) {
208215
if (partBuffer) {
209-
[ReactNativeBlobUtil emitEvent:EVENT_SERVER_PUSH
216+
[self.eventDispatcher
217+
sendDeviceEventWithName:EVENT_SERVER_PUSH
210218
body:@{
211219
@"taskId": taskId,
212220
@"chunk": [partBuffer base64EncodedStringWithOptions:0],
@@ -261,7 +269,8 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
261269
[cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
262270
}
263271
}
264-
[ReactNativeBlobUtil emitEvent: EVENT_STATE_CHANGE
272+
[self.eventDispatcher
273+
sendDeviceEventWithName: EVENT_STATE_CHANGE
265274
body:@{
266275
@"taskId": taskId,
267276
@"state": @"2",
@@ -350,7 +359,8 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
350359
NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
351360

352361
if ([self.progressConfig shouldReport:now]) {
353-
[ReactNativeBlobUtil emitEvent:EVENT_PROGRESS
362+
[self.eventDispatcher
363+
sendDeviceEventWithName:EVENT_PROGRESS
354364
body:@{
355365
@"taskId": taskId,
356366
@"written": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
@@ -463,7 +473,8 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
463473
NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
464474

465475
if ([self.uploadProgressConfig shouldReport:now]) {
466-
[ReactNativeBlobUtil emitEvent:EVENT_PROGRESS_UPLOAD
476+
[self.eventDispatcher
477+
sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
467478
body:@{
468479
@"taskId": taskId,
469480
@"written": [NSString stringWithFormat:@"%ld", (long) totalBytesWritten],

0 commit comments

Comments
 (0)