Skip to content

Commit 552d2a7

Browse files
author
Ron Radtke
committed
filesystem rework
1 parent 9d8b4eb commit 552d2a7

4 files changed

+24
-20
lines changed

class/ReactNativeBlobUtilReadStream.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
// Use of this source code is governed by a MIT-style license that can be
33
// found in the LICENSE file.
44

5-
import {
6-
DeviceEventEmitter,
7-
NativeAppEventEmitter,
8-
} from 'react-native';
5+
import {NativeEventEmitter} from 'react-native';
96
import UUID from '../utils/uuid';
107

118
import ReactNativeBlobUtil from "../codegenSpecs/NativeBlobUtils";
12-
const emitter = DeviceEventEmitter;
9+
const emitter = new NativeEventEmitter(ReactNativeBlobUtil);
1310

1411
export default class ReactNativeBlobUtilReadStream {
1512

@@ -33,7 +30,9 @@ export default class ReactNativeBlobUtilReadStream {
3330
this.streamId = 'RNFBRS'+ UUID();
3431

3532
// register for file stream event
36-
let subscription = emitter.addListener(this.streamId, (e) => {
33+
let subscription = emitter.addListener('ReactNativeBlobUtilFilesystem', (e) => {
34+
e = JSON.parse(e);
35+
if(e.streamId !== this.streamId) return; // wrong stream
3736
let {event, code, detail} = e;
3837
if(this._onData && event === 'data') {
3938
this._onData(detail);

ios/ReactNativeBlobUtilConst.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern NSString *const EVENT_PROGRESS;
2222
extern NSString *const EVENT_SERVER_PUSH;
2323
extern NSString *const EVENT_PROGRESS_UPLOAD;
2424
extern NSString *const EVENT_STATE_CHANGE;
25+
extern NSString *const EVENT_FILESYSTEM;
2526

2627
extern NSString *const FILE_PREFIX;
2728
extern NSString *const ASSET_PREFIX;

ios/ReactNativeBlobUtilConst.mm

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
NSString *const EVENT_PROGRESS = @"ReactNativeBlobUtilProgress";
2828
NSString *const EVENT_PROGRESS_UPLOAD = @"ReactNativeBlobUtilProgress-upload";
2929
NSString *const EVENT_EXPIRE = @"ReactNativeBlobUtilExpire";
30+
NSString *const EVENT_FILESYSTEM = @"ReactNativeBlobUtilFilesystem";
3031

3132
NSString *const MSG_EVENT = @"ReactNativeBlobUtilMessage";
3233
NSString *const MSG_EVENT_LOG = @"log";

ios/ReactNativeBlobUtilFS.mm

+17-14
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ + (void) readStream:(NSString *)uri
169169
if([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
170170
{
171171
NSString * message = [NSString stringWithFormat:@"File does not exist at path %@", path];
172-
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"ENOENT", @"detail": message };
173-
[baseModule emitEventDict:streamId body:payload];
172+
NSDictionary * payload = @{ @"streamId":streamId, @"event": FS_EVENT_ERROR, @"code": @"ENOENT", @"detail": message };
173+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
174174
free(buffer);
175175
return ;
176176
}
@@ -202,8 +202,8 @@ + (void) readStream:(NSString *)uri
202202
}
203203
else
204204
{
205-
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EINVAL", @"detail": @"Unable to resolve URI" };
206-
[baseModule emitEventDict:streamId body:payload];
205+
NSDictionary * payload = @{ @"streamId":streamId, @"event": FS_EVENT_ERROR, @"code": @"EINVAL", @"detail": @"Unable to resolve URI" };
206+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
207207
}
208208
// release buffer
209209
if(buffer != nil)
@@ -212,13 +212,13 @@ + (void) readStream:(NSString *)uri
212212
}
213213
@catch (NSError * err)
214214
{
215-
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EUNSPECIFIED", @"detail": [err description] };
216-
[baseModule emitEventDict:streamId body:payload];
215+
NSDictionary * payload = @{ @"streamId":streamId, @"event": FS_EVENT_ERROR, @"code": @"EUNSPECIFIED", @"detail": [err description] };
216+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
217217
}
218218
@finally
219219
{
220-
NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
221-
[baseModule emitEventDict:streamId body:payload];
220+
NSDictionary * payload = @{ @"streamId":streamId, @"event": FS_EVENT_END, @"detail": @"" };
221+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
222222
}
223223

224224
}];
@@ -235,15 +235,16 @@ + (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(
235235
if([[encoding lowercaseString] isEqualToString:@"utf8"])
236236
{
237237
NSDictionary * payload = @{
238+
@"streamId":streamId,
238239
@"event": FS_EVENT_DATA,
239240
@"detail" : [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
240241
};
241-
[baseModule emitEventDict:streamId body:payload];
242+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
242243
}
243244
else if ([[encoding lowercaseString] isEqualToString:@"base64"])
244245
{
245-
NSDictionary * payload = @{ @"event": FS_EVENT_DATA, @"detail" : [data base64EncodedStringWithOptions:0] };
246-
[baseModule emitEventDict:streamId body:payload];
246+
NSDictionary * payload = @{ @"streamId":streamId, @"event": FS_EVENT_DATA, @"detail" : [data base64EncodedStringWithOptions:0] };
247+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
247248
}
248249
else if([[encoding lowercaseString] isEqualToString:@"ascii"])
249250
{
@@ -260,23 +261,25 @@ + (void) emitDataChunks:(NSData *)data encoding:(NSString *) encoding streamId:(
260261
}
261262
}
262263

263-
NSDictionary * payload = @{ @"event": FS_EVENT_DATA, @"detail" : asciiArray };
264-
[baseModule emitEventDict:streamId body:payload];
264+
NSDictionary * payload = @{ @"streamId":streamId, @"event": FS_EVENT_DATA, @"detail" : asciiArray };
265+
[baseModule emitEventDict:EVENT_FILESYSTEM body:payload];
265266
}
266267

267268
}
268269
@catch (NSException * ex)
269270
{
270271
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]];
271272
[baseModule
272-
emitEventDict:streamId
273+
emitEventDict:EVENT_FILESYSTEM
273274
body:@{
275+
@"streamId":streamId,
274276
@"event" : MSG_EVENT_ERROR,
275277
@"detail" : message
276278
}];
277279
[baseModule
278280
emitEventDict:MSG_EVENT
279281
body:@{
282+
@"streamId":streamId,
280283
@"event" : MSG_EVENT_WARN,
281284
@"detail" : message
282285
}];

0 commit comments

Comments
 (0)