Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit d283d48

Browse files
author
Elad Gil
committed
some chages
1 parent 7d79cb0 commit d283d48

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

__tests__/mainTest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('begin event', () => {
4141
});
4242
NativeEventEmitter.listeners.downloadBegin({
4343
id: 'testBegin',
44-
expctedBytes: 9001
44+
expectedBytes: 9001
4545
});
4646
});
4747
});
@@ -159,17 +159,17 @@ test('wrong handler type', () => {
159159

160160
expect(() => {
161161
dt.begin('not function');
162-
}).toThrow('handler must be a function');
162+
}).toThrow();
163163

164164
expect(() => {
165165
dt.progress(7);
166-
}).toThrow('handler must be a function');
166+
}).toThrow();
167167

168168
expect(() => {
169169
dt.done({iamnota: 'function'});
170-
}).toThrow('handler must be a function');
170+
}).toThrow();
171171

172172
expect(() => {
173173
dt.error('not function');
174-
}).toThrow('handler must be a function');
174+
}).toThrow();
175175
});

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function checkForExistingDownloads() {
5353
if (taskInfo.bytesWritten === taskInfo.totalBytes) {
5454
task.state = 'DONE';
5555
} else {
56+
// IOS completed the download but it was not done.
5657
return null;
5758
}
5859
}

lib/downloadTask.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { NativeModules } from 'react-native';
22
const { RNBackgroundDownload } = NativeModules;
33

4+
function validateHandler(handler) {
5+
if (!(typeof handler === 'function')) {
6+
throw new TypeError(`[RNBackgroundDownload] expected argument to be a function, got: ${typeof handler}`);
7+
}
8+
}
49
export default class DownloadTask {
510
state = 'PENDING'
611
percent = 0
@@ -19,33 +24,25 @@ export default class DownloadTask {
1924
}
2025

2126
begin(handler) {
22-
if (!(typeof handler === 'function')) {
23-
throw new TypeError('handler must be a function');
24-
}
27+
validateHandler(handler);
2528
this._beginHandler = handler;
2629
return this;
2730
}
2831

2932
progress(handler) {
30-
if (!(typeof handler === 'function')) {
31-
throw new TypeError('handler must be a function');
32-
}
33+
validateHandler(handler);
3334
this._progressHandler = handler;
3435
return this;
3536
}
3637

3738
done(handler) {
38-
if (!(typeof handler === 'function')) {
39-
throw new TypeError('handler must be a function');
40-
}
39+
validateHandler(handler);
4140
this._doneHandler = handler;
4241
return this;
4342
}
4443

4544
error(handler) {
46-
if (!(typeof handler === 'function')) {
47-
throw new TypeError('handler must be a function');
48-
}
45+
validateHandler(handler);
4946
this._errorHandler = handler;
5047
return this;
5148
}

0 commit comments

Comments
 (0)