forked from transistorsoft/react-native-background-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (50 loc) · 1.61 KB
/
index.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { NativeModules, NativeEventEmitter } from "react-native";
const RNBackgroundFetch = NativeModules.RNBackgroundFetch;
const EventEmitter = new NativeEventEmitter(RNBackgroundFetch);
const EVENT_FETCH = "fetch";
const TAG = "RNBackgroundFetch";
const EVENTS = ["fetch"];
const STATUS_RESTRICTED = 0;
const STATUS_DENIED = 1;
const STATUS_AVAILABLE = 2;
class API {
static configure(config, callback, failure) {
if (typeof(callback) !== 'function') {
throw "RNBackgroundFetch requires a fetch callback at 2nd argument";
}
config = config || {};
failure = failure || function() {};
RNBackgroundFetch.configure(config, failure);
return this.addListener(EVENT_FETCH, callback);
}
static on(event, callback) {
return this.addListener(event, callback);
}
static addListener(event, callback) {
if (EVENTS.indexOf(event) < 0) {
throw "RNBackgroundFetch: Unknown event '" + event + '"';
}
return EventEmitter.addListener(event, callback);
}
static start(success, failure) {
success = success || function() {};
failure = failure || function() {};
RNBackgroundFetch.start(success, failure);
}
static stop() {
RNBackgroundFetch.stop();
}
static finish() {
RNBackgroundFetch.finish();
}
static status(callback) {
if (typeof(callback) !== 'function') {
throw "RNBackgroundFetch#status requires a callback as 1st argument";
}
RNBackgroundFetch.status(callback);
}
}
API.STATUS_RESTRICTED = STATUS_RESTRICTED;
API.STATUS_DENIED = STATUS_DENIED;
API.STATUS_AVAILABLE = STATUS_AVAILABLE;
module.exports = API;