Skip to content

Commit 7bb263d

Browse files
author
Ron Radtke
committed
ios part but no idea if working
1 parent 6a9eb7b commit 7bb263d

10 files changed

+50
-80
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ DerivedData
2828
*.xcuserstate
2929
project.xcworkspace
3030

31+
# VSCode
32+
#
33+
.vscode
34+
3135
# Android/IJ
3236
#
3337
.idea

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, NativeModules} from 'react-native';
5+
import {DeviceEventEmitter, NativeEventEmitter} 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@
1818
#import <React/RCTBridge.h>
1919
#import <React/RCTEventDispatcher.h>
2020
#import <React/RCTBridgeModule.h>
21+
#import <React/RCTEventEmitter.h>
2122
#else
2223
#import "RCTBridgeModule.h"
2324
#import "RCTLog.h"
2425
#import "RCTRootView.h"
2526
#import "RCTBridge.h"
2627
#import "RCTEventDispatcher.h"
28+
#import "RCTEventEmitter.h"
2729
#endif
2830

2931
#import <UIKit/UIKit.h>
3032

33+
3134
#if RCT_NEW_ARCH_ENABLED
3235
#import <React-Codegen/ReactNativeBlobUtilSpec/ReactNativeBlobUtilSpec.h>
3336
#endif
3437

3538

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

3841
NSString * filePathPrefix;
3942

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

4548
+ (RCTEventDispatcher *)getRCTEventDispatcher;
49+
+(void) emitEvent;
4650

4751
@end
4852

ios/ReactNativeBlobUtil/ReactNativeBlobUtil.mm

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

18-
__strong RCTEventDispatcher * eventDispatcherRef;
1918
dispatch_queue_t commonTaskQueue;
2019
dispatch_queue_t fsQueue;
2120

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+
2243
////////////////////////////////////////
2344
//
2445
// Exported native methods
@@ -67,10 +88,6 @@ - (id) init {
6788
if(![[NSFileManager defaultManager] fileExistsAtPath: [ReactNativeBlobUtilFS getTempPath] isDirectory:&isDir]) {
6889
[[NSFileManager defaultManager] createDirectoryAtPath:[ReactNativeBlobUtilFS getTempPath] withIntermediateDirectories:YES attributes:nil error:NULL];
6990
}
70-
dispatch_async(dispatch_get_main_queue(), ^{
71-
eventDispatcherRef = bridge.eventDispatcher;
72-
[ReactNativeBlobUtilNetwork emitExpiredTasks: eventDispatcherRef];
73-
});
7491

7592
return self;
7693
}
@@ -135,7 +152,6 @@ - (NSDictionary *)constantsToExport
135152
{
136153
[[ReactNativeBlobUtilNetwork sharedInstance] sendRequest:options
137154
contentLength:bodyLength
138-
eventDispatcher:eventDispatcherRef
139155
taskId:taskId
140156
withRequest:req
141157
callback:callback];
@@ -172,7 +188,6 @@ - (NSDictionary *)constantsToExport
172188
{
173189
[[ReactNativeBlobUtilNetwork sharedInstance] sendRequest:options
174190
contentLength:bodyLength
175-
eventDispatcher:eventDispatcherRef
176191
taskId:taskId
177192
withRequest:req
178193
callback:callback];
@@ -366,7 +381,7 @@ - (void)writeFileArray:(NSString *)path
366381
appendData:(BOOL)append
367382
callback:(RCTResponseSenderBlock)callback)
368383
{
369-
ReactNativeBlobUtilFS * fileStream = [[ReactNativeBlobUtilFS alloc] initWithEventDispatcherRef:eventDispatcherRef];
384+
ReactNativeBlobUtilFS * fileStream = [[ReactNativeBlobUtilFS alloc] init];
370385
NSFileManager * fm = [NSFileManager defaultManager];
371386
NSString * folder = [path stringByDeletingLastPathComponent];
372387
NSError* err = nil;
@@ -696,7 +711,7 @@ - (void)hash:(NSString *)path
696711
}
697712

698713
dispatch_async(fsQueue, ^{
699-
[ReactNativeBlobUtilFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId eventDispatcherRef:eventDispatcherRef];
714+
[ReactNativeBlobUtilFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId];
700715
});
701716
}
702717

@@ -877,13 +892,6 @@ - (UIViewController *)documentInteractionControllerViewControllerForPreview: (UI
877892
return window.rootViewController;
878893
}
879894

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

ios/ReactNativeBlobUtilFS.h

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

9695
// file stream
9796
- (void) openWithDestination;

ios/ReactNativeBlobUtilFS.mm

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ - (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-
6761
// static member getter
6862
+ (NSDictionary *) getFileStreams {
6963

@@ -160,11 +154,9 @@ + (void) readStream:(NSString *)uri
160154
bufferSize:(int)bufferSize
161155
tick:(int)tick
162156
streamId:(NSString *)streamId
163-
eventDispatcherRef:(RCTEventDispatcher *)eventDispatcherRef
164157
{
165158
[[self class] getPathFromUri:uri completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
166159

167-
__block RCTEventDispatcher * event = eventDispatcherRef;
168160
__block int read = 0;
169161
__block int backoff = tick *1000;
170162
__block int chunkSize = bufferSize;
@@ -179,15 +171,15 @@ + (void) readStream:(NSString *)uri
179171
{
180172
NSString * message = [NSString stringWithFormat:@"File does not exist at path %@", path];
181173
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"ENOENT", @"detail": message };
182-
[event sendDeviceEventWithName:streamId body:payload];
174+
[ReactNativeBlobUtil emitEvent:streamId body:payload];
183175
free(buffer);
184176
return ;
185177
}
186178
NSInputStream * stream = [[NSInputStream alloc] initWithFileAtPath:path];
187179
[stream open];
188180
while((read = [stream read:buffer maxLength:bufferSize]) > 0)
189181
{
190-
[[self class] emitDataChunks:[NSData dataWithBytes:buffer length:read] encoding:encoding streamId:streamId event:event];
182+
[[self class] emitDataChunks:[NSData dataWithBytes:buffer length:read] encoding:encoding streamId:streamId];
191183
if(tick > 0)
192184
{
193185
usleep(backoff);
@@ -212,7 +204,7 @@ + (void) readStream:(NSString *)uri
212204
else
213205
{
214206
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EINVAL", @"detail": @"Unable to resolve URI" };
215-
[event sendDeviceEventWithName:streamId body:payload];
207+
[ReactNativeBlobUtil emitEvent:streamId body:payload];
216208
}
217209
// release buffer
218210
if(buffer != nil)
@@ -222,12 +214,12 @@ + (void) readStream:(NSString *)uri
222214
@catch (NSError * err)
223215
{
224216
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EUNSPECIFIED", @"detail": [err description] };
225-
[event sendDeviceEventWithName:streamId body:payload];
217+
[ReactNativeBlobUtil emitEvent:streamId body:payload];
226218
}
227219
@finally
228220
{
229221
NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
230-
[event sendDeviceEventWithName:streamId body:payload];
222+
[ReactNativeBlobUtil emitEvent:streamId body:payload];
231223
}
232224

233225
}];
@@ -236,7 +228,7 @@ + (void) readStream:(NSString *)uri
236228
}
237229

238230
// send read stream chunks via native event emitter
239-
+ (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(NSString *)streamId event:(RCTEventDispatcher *)event
231+
+ (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(NSString *)streamId
240232
{
241233
@try
242234
{
@@ -247,12 +239,12 @@ + (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(
247239
@"event": FS_EVENT_DATA,
248240
@"detail" : [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
249241
};
250-
[event sendDeviceEventWithName:streamId body:payload];
242+
[ReactNativeBlobUtil emitEvent:streamId body:payload];
251243
}
252244
else if ([[encoding lowercaseString] isEqualToString:@"base64"])
253245
{
254246
NSDictionary * payload = @{ @"event": FS_EVENT_DATA, @"detail" : [data base64EncodedStringWithOptions:0] };
255-
[event sendDeviceEventWithName:streamId body:payload];
247+
[ReactNativeBlobUtil emitEvent:streamId body:payload];
256248
}
257249
else if([[encoding lowercaseString] isEqualToString:@"ascii"])
258250
{
@@ -270,21 +262,19 @@ + (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(
270262
}
271263

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

276268
}
277269
@catch (NSException * ex)
278270
{
279271
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]];
280-
[event
281-
sendDeviceEventWithName:streamId
272+
[ReactNativeBlobUtil emitEvent:streamId
282273
body:@{
283274
@"event" : MSG_EVENT_ERROR,
284275
@"detail" : message
285276
}];
286-
[event
287-
sendDeviceEventWithName:MSG_EVENT
277+
[ReactNativeBlobUtil emitEvent:MSG_EVENT
288278
body:@{
289279
@"event" : MSG_EVENT_WARN,
290280
@"detail" : message

ios/ReactNativeBlobUtilNetwork.h

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

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

3534
- (nullable id) init;
3635
- (void) sendRequest:(NSDictionary * _Nullable )options
3736
contentLength:(long)contentLength
38-
eventDispatcher:(RCTEventDispatcher * _Nullable)eventDispatcherRef
3937
taskId:(NSString * _Nullable)taskId
4038
withRequest:(NSURLRequest * _Nullable)req
4139
callback:(_Nullable RCTResponseSenderBlock) callback;

ios/ReactNativeBlobUtilNetwork.mm

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

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-
181160
@end

ios/ReactNativeBlobUtilRequest.h

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

3939
- (void) sendRequest:(NSDictionary * _Nullable )options
4040
contentLength:(long)contentLength
41-
eventDispatcher:(RCTEventDispatcher * _Nullable)eventDispatcherRef
4241
taskId:(NSString * _Nullable)taskId
4342
withRequest:(NSURLRequest * _Nullable)req
4443
taskOperationQueue:(NSOperationQueue * _Nonnull)operationQueue

0 commit comments

Comments
 (0)