Skip to content

Commit 6a9eb7b

Browse files
author
Ron Radtke
committed
js side
1 parent 132051c commit 6a9eb7b

File tree

7 files changed

+3152
-2254
lines changed

7 files changed

+3152
-2254
lines changed

class/ReactNativeBlobUtilSession.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
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';
9-
10-
import ReactNativeBlobUtil from "../codegenSpecs/NativeBlobUtils";
5+
import ReactNativeBlobUtil from '../codegenSpecs/NativeBlobUtils';
116

127
let sessions = {};
138

@@ -29,8 +24,8 @@ export default class ReactNativeBlobUtilSession {
2924

3025
constructor(name:string, list:Array<string>) {
3126
this.name = name;
32-
if(!sessions[name]) {
33-
if(Array.isArray(list))
27+
if (!sessions[name]) {
28+
if (Array.isArray(list))
3429
sessions[name] = list;
3530
else
3631
sessions[name] = [];
@@ -44,8 +39,8 @@ export default class ReactNativeBlobUtilSession {
4439

4540
remove(path:string):ReactNativeBlobUtilSession {
4641
let list = sessions[this.name];
47-
for(let i of list) {
48-
if(list[i] === path) {
42+
for (let i of list) {
43+
if (list[i] === path) {
4944
sessions[this.name].splice(i, 1);
5045
break;
5146
}
@@ -60,7 +55,7 @@ export default class ReactNativeBlobUtilSession {
6055
dispose():Promise {
6156
return new Promise((resolve, reject) => {
6257
ReactNativeBlobUtil.removeSession(sessions[this.name], (err) => {
63-
if(err)
58+
if (err)
6459
reject(new Error(err));
6560
else {
6661
delete sessions[this.name];

class/ReactNativeBlobUtilWriteStream.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
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';
9-
10-
import ReactNativeBlobUtil from "../codegenSpecs/NativeBlobUtils";
5+
import ReactNativeBlobUtil from '../codegenSpecs/NativeBlobUtils';
116
export default class ReactNativeBlobUtilWriteStream {
127

138
id : string;
@@ -24,17 +19,17 @@ export default class ReactNativeBlobUtilWriteStream {
2419
return new Promise((resolve, reject) => {
2520
try {
2621
let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk';
27-
if(this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) {
22+
if (this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) {
2823
reject(new Error('ascii input data must be an Array'));
2924
return;
3025
}
3126
ReactNativeBlobUtil[method](this.id, data, (error) => {
32-
if(error)
27+
if (error)
3328
reject(new Error(error));
3429
else
3530
resolve(this);
3631
});
37-
} catch(err) {
32+
} catch (err) {
3833
reject(new Error(err));
3934
}
4035
});

codegenSpecs/NativeBlobUtils.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,17 @@ export interface Spec extends TurboModule {
6666
// Android Only APIs
6767
+actionViewIntent: (path: string, mime: string, chooserTitle: string) => Promise<void>;
6868
+addCompleteDownload: (config: Object) => Promise<void>;
69-
+copyToInternal: (contentUri: string, destpath: string) => Promise<string>;
69+
+copyToInternal: (contentUri: string, destpath: string) => Promise<string>;
7070
+copyToMediaStore: (filedata: Object, mt: string, path: string) => Promise<string>;
71-
+createMediaFile: (filedata: Object, mt: string) => Promise<string>;
71+
+createMediaFile: (filedata: Object, mt: string) => Promise<string>;
7272
+getBlob: (contentUri: string, encoding: string) => Promise<Array<any>>;
7373
+getContentIntent: (mime: string) => Promise<string>;
7474
+getSDCardDir: () => Promise<string>;
7575
+getSDCardApplicationDir: () => Promise<string>;
7676
+scanFile: (pairs: Array<any>, callback: (value: Array<any>) => void) => void;
7777
+writeToMediaFile: (fileUri: string, path: string, transformFile: boolean) => Promise<string>;
7878
}
79-
80-
export default (TurboModuleRegistry.get<Spec>(
81-
'ReactNativeBlobUtil'
82-
): ?Spec);
83-
84-
85-
79+
80+
export default (TurboModuleRegistry.get<Spec>('ReactNativeBlobUtil'): ?Spec);
81+
82+

fetch.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {ReactNativeBlobUtilConfig} from "./types";
2-
import URIUtil from "./utils/uri";
3-
import fs from "./fs";
4-
import getUUID from "./utils/uuid";
5-
import {DeviceEventEmitter} from "react-native";
6-
import {FetchBlobResponse} from "./class/ReactNativeBlobUtilBlobResponse";
7-
import CanceledFetchError from "./class/ReactNativeBlobUtilCanceledFetchError";
8-
import ReactNativeBlobUtil from "./codegenSpecs/NativeBlobUtils";
1+
import {ReactNativeBlobUtilConfig} from 'types';
2+
import URIUtil from './utils/uri';
3+
import fs from './fs';
4+
import getUUID from './utils/uuid';
5+
import {DeviceEventEmitter, NativeEventEmitter, NativeModules} from 'react-native';
6+
import {FetchBlobResponse} from './class/ReactNativeBlobUtilBlobResponse';
7+
import CanceledFetchError from './class/ReactNativeBlobUtilCanceledFetchError';
8+
import ReactNativeBlobUtil from './codegenSpecs/NativeBlobUtils';
99

10-
const emitter = DeviceEventEmitter;
10+
const eventEmitter = new NativeEventEmitter(ReactNativeBlobUtil);
1111

1212
// register message channel event handler.
13-
emitter.addListener("ReactNativeBlobUtilMessage", (e) => {
13+
eventEmitter.addListener('ReactNativeBlobUtilMessage', (e) => {
1414

1515
if (e.event === 'warn') {
1616
console.warn(e.detail);
@@ -19,7 +19,7 @@ emitter.addListener("ReactNativeBlobUtilMessage", (e) => {
1919
throw e.detail;
2020
}
2121
else {
22-
console.log("ReactNativeBlobUtil native message", e.detail);
22+
console.log('ReactNativeBlobUtil native message', e.detail);
2323
}
2424
});
2525

@@ -105,7 +105,7 @@ function fetchFile(options = {}, method, url, headers = {}, body): Promise {
105105
.then((stream) => new Promise((resolve, reject) => {
106106
stream.open();
107107
info = {
108-
state: "2",
108+
state: '2',
109109
headers: {'source': 'system-fs'},
110110
status: 200,
111111
respType: 'text',
@@ -188,31 +188,31 @@ export function fetch(...args: any): Promise {
188188
let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob';
189189

190190
// on progress event listener
191-
subscription = emitter.addListener('ReactNativeBlobUtilProgress', (e) => {
191+
subscription = eventEmitter.addListener('ReactNativeBlobUtilProgress', (e) => {
192192
if (e.taskId === taskId && promise.onProgress) {
193193
promise.onProgress(e.written, e.total, e.chunk);
194194
}
195195
});
196196

197-
subscriptionUpload = emitter.addListener('ReactNativeBlobUtilProgress-upload', (e) => {
197+
subscriptionUpload = eventEmitter.addListener('ReactNativeBlobUtilProgress-upload', (e) => {
198198
if (e.taskId === taskId && promise.onUploadProgress) {
199199
promise.onUploadProgress(e.written, e.total);
200200
}
201201
});
202202

203-
stateEvent = emitter.addListener('ReactNativeBlobUtilState', (e) => {
203+
stateEvent = eventEmitter.addListener('ReactNativeBlobUtilState', (e) => {
204204
if (e.taskId === taskId)
205205
respInfo = e;
206206
promise.onStateChange && promise.onStateChange(e);
207207
});
208208

209-
subscription = emitter.addListener('ReactNativeBlobUtilExpire', (e) => {
209+
subscription = eventEmitter.addListener('ReactNativeBlobUtilExpire', (e) => {
210210
if (e.taskId === taskId && promise.onExpire) {
211211
promise.onExpire(e);
212212
}
213213
});
214214

215-
partEvent = emitter.addListener('ReactNativeBlobUtilServerPush', (e) => {
215+
partEvent = eventEmitter.addListener('ReactNativeBlobUtilServerPush', (e) => {
216216
if (e.taskId === taskId && promise.onPartData) {
217217
promise.onPartData(e.chunk);
218218
}
@@ -244,11 +244,11 @@ export function fetch(...args: any): Promise {
244244
subscriptionUpload.remove();
245245
stateEvent.remove();
246246
partEvent.remove();
247-
delete promise['progress'];
248-
delete promise['uploadProgress'];
249-
delete promise['stateChange'];
250-
delete promise['part'];
251-
delete promise['cancel'];
247+
delete promise.progress;
248+
delete promise.uploadProgress;
249+
delete promise.stateChange;
250+
delete promise.part;
251+
delete promise.cancel;
252252
// delete promise['expire']
253253
promise.cancel = () => {
254254
};
@@ -329,7 +329,7 @@ export function fetch(...args: any): Promise {
329329
subscriptionUpload.remove();
330330
stateEvent.remove();
331331
ReactNativeBlobUtil.cancelRequest(taskId, fn);
332-
promiseReject(new CanceledFetchError("canceled"));
332+
promiseReject(new CanceledFetchError('canceled'));
333333
};
334334
promise.taskId = taskId;
335335

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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 {AppState, DeviceEventEmitter, Platform,} from 'react-native';
5+
import {AppState, Platform,} from 'react-native';
66
import ReactNativeBlobUtil from './codegenSpecs/NativeBlobUtils';
77

88
//import StatefulPromise from './class/StatefulPromise.js'
@@ -13,9 +13,9 @@ import polyfill from './polyfill';
1313
import android from './android';
1414
import ios from './ios';
1515
import JSONStream from './json-stream';
16-
import {config, fetch} from './fetch';
17-
import URIUtil from "./utils/uri";
18-
import CanceledFetchError from "./class/ReactNativeBlobUtilCanceledFetchError";
16+
import {config, fetch} from 'fetch';
17+
import URIUtil from './utils/uri';
18+
import CanceledFetchError from './class/ReactNativeBlobUtilCanceledFetchError';
1919

2020
const {
2121
ReactNativeBlobUtilSession,
@@ -34,7 +34,6 @@ const {
3434
} = fs;
3535

3636
const Blob = polyfill.Blob;
37-
const emitter = DeviceEventEmitter;
3837
const wrap = URIUtil.wrap;
3938

4039
// when app resumes, check if there's any expired network task and trigger

0 commit comments

Comments
 (0)