rn-bg-actions 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js ADDED
@@ -0,0 +1,138 @@
1
+ import { Platform, AppRegistry } from 'react-native';
2
+ import { RNBackgroundActions, nativeEventEmitter } from './RNBackgroundActionsModule';
3
+ import EventEmitter from 'eventemitter3';
4
+
5
+ /**
6
+ * @typedef {{taskName: string,
7
+ * taskTitle: string,
8
+ * taskDesc: string,
9
+ * taskIcon: {name: string, type: string, package?: string},
10
+ * color?: string
11
+ * linkingURI?: string,
12
+ * progressBar?: {max: number, value: number, indeterminate?: boolean}
13
+ * }} BackgroundTaskOptions
14
+ * @extends EventEmitter<'expiration',any>
15
+ */
16
+ class BackgroundServer extends EventEmitter {
17
+ constructor() {
18
+ super();
19
+ /** @private */
20
+ this._runnedTasks = 0;
21
+ /** @private @type {(arg0?: any) => void} */
22
+ this._stopTask = () => {};
23
+ /** @private */
24
+ this._isRunning = false;
25
+ /** @private @type {BackgroundTaskOptions} */
26
+ this._currentOptions;
27
+ this._addListeners();
28
+ }
29
+
30
+ /**
31
+ * @private
32
+ */
33
+ _addListeners() {
34
+ nativeEventEmitter.addListener('expiration', () => this.emit('expiration'));
35
+ }
36
+
37
+ /**
38
+ * **ANDROID ONLY**
39
+ *
40
+ * Updates the task notification.
41
+ *
42
+ * *On iOS this method will return immediately*
43
+ *
44
+ * @param {{taskTitle?: string,
45
+ * taskDesc?: string,
46
+ * taskIcon?: {name: string, type: string, package?: string},
47
+ * color?: string,
48
+ * linkingURI?: string,
49
+ * progressBar?: {max: number, value: number, indeterminate?: boolean}}} taskData
50
+ */
51
+ async updateNotification(taskData) {
52
+ if (Platform.OS !== 'android') return;
53
+ if (!this.isRunning())
54
+ throw new Error('A BackgroundAction must be running before updating the notification');
55
+ this._currentOptions = this._normalizeOptions({ ...this._currentOptions, ...taskData });
56
+ await RNBackgroundActions.updateNotification(this._currentOptions);
57
+ }
58
+
59
+ /**
60
+ * Returns if the current background task is running.
61
+ *
62
+ * It returns `true` if `start()` has been called and the task has not finished.
63
+ *
64
+ * It returns `false` if `stop()` has been called, **even if the task has not finished**.
65
+ */
66
+ isRunning() {
67
+ return this._isRunning;
68
+ }
69
+
70
+ /**
71
+ * @template T
72
+ *
73
+ * @param {(taskData?: T) => Promise<void>} task
74
+ * @param {BackgroundTaskOptions & {parameters?: T}} options
75
+ * @returns {Promise<void>}
76
+ */
77
+ async start(task, options) {
78
+ this._runnedTasks++;
79
+ this._currentOptions = this._normalizeOptions(options);
80
+ const finalTask = this._generateTask(task, options.parameters);
81
+ if (Platform.OS === 'android') {
82
+ AppRegistry.registerHeadlessTask(this._currentOptions.taskName, () => finalTask);
83
+ await RNBackgroundActions.start(this._currentOptions);
84
+ this._isRunning = true;
85
+ } else {
86
+ await RNBackgroundActions.start(this._currentOptions);
87
+ this._isRunning = true;
88
+ finalTask();
89
+ }
90
+ }
91
+
92
+ /**
93
+ * @private
94
+ * @template T
95
+ * @param {(taskData?: T) => Promise<void>} task
96
+ * @param {T} [parameters]
97
+ */
98
+ _generateTask(task, parameters) {
99
+ const self = this;
100
+ return async () => {
101
+ await new Promise((resolve) => {
102
+ self._stopTask = resolve;
103
+ task(parameters).then(() => self.stop());
104
+ });
105
+ };
106
+ }
107
+
108
+ /**
109
+ * @private
110
+ * @param {BackgroundTaskOptions} options
111
+ */
112
+ _normalizeOptions(options) {
113
+ return {
114
+ taskName: options.taskName + this._runnedTasks,
115
+ taskTitle: options.taskTitle,
116
+ taskDesc: options.taskDesc,
117
+ taskIcon: { ...options.taskIcon },
118
+ color: options.color || '#ffffff',
119
+ linkingURI: options.linkingURI,
120
+ progressBar: options.progressBar,
121
+ };
122
+ }
123
+
124
+ /**
125
+ * Stops the background task.
126
+ *
127
+ * @returns {Promise<void>}
128
+ */
129
+ async stop() {
130
+ this._stopTask();
131
+ await RNBackgroundActions.stop();
132
+ this._isRunning = false;
133
+ }
134
+ }
135
+
136
+ const backgroundServer = new BackgroundServer();
137
+
138
+ export default backgroundServer;