/
index.android.js
55 lines (45 loc) · 1.06 KB
/
index.android.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
// @flow
import { AppRegistry, NativeModules } from 'react-native'
import constants from './contants'
import type { BackgroundTaskInterface } from '../types'
const { BackgroundTask: RNBackgroundTask } = NativeModules
const BackgroundTask: BackgroundTaskInterface = {
...constants,
define: function(task) {
// Register the headless task
const fn = async () => {
task()
}
AppRegistry.registerHeadlessTask('BackgroundTask', () => fn)
},
schedule: function(
{
period = 900, // 15 minutes
timeout = 30,
flex,
} = {}
) {
// Default flex to within 50% of the period
if (!flex) {
flex = Math.floor(period / 2)
}
RNBackgroundTask.schedule({
period,
timeout,
flex,
})
},
finish: function() {
// Needed for iOS, no-op on Android
},
cancel: function() {
RNBackgroundTask.cancel()
},
statusAsync: function() {
// No options exist on Android to block background tasks
return Promise.resolve({
available: true,
})
}
}
module.exports = BackgroundTask