-
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (130 loc) · 4.31 KB
/
index.js
File metadata and controls
140 lines (130 loc) · 4.31 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Platform, AppRegistry } from 'react-native';
import { RNBackgroundActions, nativeEventEmitter } from './RNBackgroundActionsModule';
import EventEmitter from 'eventemitter3';
/**
* @typedef {{taskName: string,
* taskTitle: string,
* taskDesc: string,
* taskIcon: {name: string, type: string, package?: string},
* color?: string
* linkingURI?: string,
* progressBar?: {max: number, value: number, indeterminate?: boolean}
* checkLocationPermissions?: boolean
* }} BackgroundTaskOptions
* @extends EventEmitter<'expiration',any>
*/
class BackgroundServer extends EventEmitter {
constructor() {
super();
/** @private */
this._runnedTasks = 0;
/** @private @type {(arg0?: any) => void} */
this._stopTask = () => {};
/** @private */
this._isRunning = false;
/** @private @type {BackgroundTaskOptions} */
this._currentOptions;
this._addListeners();
}
/**
* @private
*/
_addListeners() {
nativeEventEmitter.addListener('expiration', () => this.emit('expiration'));
}
/**
* **ANDROID ONLY**
*
* Updates the task notification.
*
* *On iOS this method will return immediately*
*
* @param {{taskTitle?: string,
* taskDesc?: string,
* taskIcon?: {name: string, type: string, package?: string},
* color?: string,
* linkingURI?: string,
* progressBar?: {max: number, value: number, indeterminate?: boolean}}} taskData
*/
async updateNotification(taskData) {
if (Platform.OS !== 'android') return;
if (!this.isRunning())
throw new Error('A BackgroundAction must be running before updating the notification');
this._currentOptions = this._normalizeOptions({ ...this._currentOptions, ...taskData });
await RNBackgroundActions.updateNotification(this._currentOptions);
}
/**
* Returns if the current background task is running.
*
* It returns `true` if `start()` has been called and the task has not finished.
*
* It returns `false` if `stop()` has been called, **even if the task has not finished**.
*/
isRunning() {
return this._isRunning;
}
/**
* @template T
*
* @param {(taskData?: T) => Promise<void>} task
* @param {BackgroundTaskOptions & {parameters?: T}} options
* @returns {Promise<void>}
*/
async start(task, options) {
this._runnedTasks++;
this._currentOptions = this._normalizeOptions(options);
const finalTask = this._generateTask(task, options.parameters);
if (Platform.OS === 'android') {
AppRegistry.registerHeadlessTask(this._currentOptions.taskName, () => finalTask);
await RNBackgroundActions.start(this._currentOptions);
this._isRunning = true;
} else {
await RNBackgroundActions.start(this._currentOptions);
this._isRunning = true;
finalTask();
}
}
/**
* @private
* @template T
* @param {(taskData?: T) => Promise<void>} task
* @param {T} [parameters]
*/
_generateTask(task, parameters) {
const self = this;
return async () => {
await new Promise((resolve) => {
self._stopTask = resolve;
task(parameters).then(() => self.stop());
});
};
}
/**
* @private
* @param {BackgroundTaskOptions} options
*/
_normalizeOptions(options) {
return {
taskName: options.taskName + this._runnedTasks,
taskTitle: options.taskTitle,
taskDesc: options.taskDesc,
taskIcon: { ...options.taskIcon },
color: options.color || '#ffffff',
linkingURI: options.linkingURI,
progressBar: options.progressBar,
checkLocationPermissions: options.checkLocationPermissions,
};
}
/**
* Stops the background task.
*
* @returns {Promise<void>}
*/
async stop() {
this._stopTask();
await RNBackgroundActions.stop();
this._isRunning = false;
}
}
const backgroundServer = new BackgroundServer();
export default backgroundServer;