-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (35 loc) · 1.11 KB
/
index.js
File metadata and controls
40 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { NativeEventEmitter, NativeModules, } from 'react-native';
const { RNVideoHelper } = NativeModules;
const videoHelperEmitter = new NativeEventEmitter(RNVideoHelper);
const LISTENERS = '__listeners';
class ProgressPromise extends Promise {
constructor(executor) {
super((resolve, reject) => executor(resolve, reject,
// Pass method for passing progress to listener
value => {
try {
return this[LISTENERS].forEach(listener => listener(value));
} catch(error) {
reject(error);
}
}));
this[LISTENERS] = [];
}
progress(handler) {
if(typeof handler !== 'function')
throw new Error('PROGRESS_REQUIRES_FUNCTION');
this[LISTENERS].push(handler);
return this;
}
}
export default {
compress: (source, options) => {
return new ProgressPromise((resolve, reject, progress) => {
const subscription = videoHelperEmitter.addListener('progress', p => progress(p));
RNVideoHelper.compress(source, options).then(output => {
subscription.remove();
resolve(output);
}).catch(err => reject(err));
});
},
}