From 0f1218883016a60710e0bdaa739cd71ee80560aa Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Thu, 20 Feb 2020 18:28:09 +0100 Subject: [PATCH] chore: More functional example --- examples/backgroundExample/App.js | 56 ++++++++++++------------- examples/backgroundExample/package.json | 1 + examples/backgroundExample/yarn.lock | 5 +++ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/examples/backgroundExample/App.js b/examples/backgroundExample/App.js index 26c3b95..8f31e73 100644 --- a/examples/backgroundExample/App.js +++ b/examples/backgroundExample/App.js @@ -17,21 +17,33 @@ import { TouchableOpacity, Platform, } from 'react-native'; +import {Header, Colors} from 'react-native/Libraries/NewAppScreen'; +import EventEmitter from 'events'; import BackgroundJob from 'react-native-background-actions'; -import {Header, Colors} from 'react-native/Libraries/NewAppScreen'; - const sleep = time => new Promise(resolve => setTimeout(() => resolve(), time)); +const eventEmitter = new EventEmitter(); + const taskRandom = async taskData => { - if (Platform.OS === 'ios') - console.warn('This task will not keep your app alive in the background by itself, use other library like react-native-track-player that use audio, geolocalization, etc. to keep your app alive in the background while you excute the JS from this library.'); - const args = taskData.arguments; - for (let i = 0; i < 1000; i++) { - console.log('Runned -> ', i); - await sleep(args.delay); + if (Platform.OS === 'ios') { + console.warn( + 'This task will not keep your app alive in the background by itself, use other library like react-native-track-player that use audio,', + 'geolocalization, etc. to keep your app alive in the background while you excute the JS from this library.', + ); } + await new Promise(async resolve => { + let keepRunning = true; + // We add a listener to stop running + eventEmitter.addListener('close', () => (keepRunning = false)); + // For loop with a delay + const {delay} = taskData.arguments; + for (let i = 0; keepRunning; i++) { + console.log('Runned -> ', i); + await sleep(delay); + } + }); }; const options = { @@ -51,7 +63,10 @@ const options = { class App extends React.Component { playing = false; - initBackground = async () => { + /** + * Toggles the background task + */ + toggleBackground = async () => { this.playing = !this.playing; if (this.playing) { try { @@ -62,7 +77,8 @@ class App extends React.Component { console.log('Error', e); } } else { - await BackgroundJob.stop(); + console.log('Stop background service'); + eventEmitter.emit('close'); } }; render() { @@ -82,7 +98,7 @@ class App extends React.Component { + onPress={this.toggleBackground}> @@ -102,24 +118,6 @@ const styles = StyleSheet.create({ body: { backgroundColor: Colors.white, }, - sectionContainer: { - marginTop: 32, - paddingHorizontal: 24, - }, - sectionTitle: { - fontSize: 24, - fontWeight: '600', - color: Colors.black, - }, - sectionDescription: { - marginTop: 8, - fontSize: 18, - fontWeight: '400', - color: Colors.dark, - }, - highlight: { - fontWeight: '700', - }, footer: { color: Colors.dark, fontSize: 12, diff --git a/examples/backgroundExample/package.json b/examples/backgroundExample/package.json index 613b69c..8a8a6a2 100644 --- a/examples/backgroundExample/package.json +++ b/examples/backgroundExample/package.json @@ -11,6 +11,7 @@ "pods": "cd ios && pod install" }, "dependencies": { + "events": "^3.1.0", "react": "16.9.0", "react-native": "0.61.5", "react-native-background-actions": "^1.1.0" diff --git a/examples/backgroundExample/yarn.lock b/examples/backgroundExample/yarn.lock index edc73c0..8e19023 100644 --- a/examples/backgroundExample/yarn.lock +++ b/examples/backgroundExample/yarn.lock @@ -2373,6 +2373,11 @@ eventemitter3@^3.0.0: resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7" integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q== +events@^3.1.0: + version "3.1.0" + resolved "https://registry.npmjs.org/events/-/events-3.1.0.tgz#84279af1b34cb75aa88bf5ff291f6d0bd9b31a59" + integrity sha512-Rv+u8MLHNOdMjTAFeT3nCjHn2aGlx435FP/sDHNaRhDEMwyI/aB22Kj2qIN8R0cw3z28psEQLYwxVKLsKrMgWg== + exec-sh@^0.3.2: version "0.3.4" resolved "https://registry.npmjs.org/exec-sh/-/exec-sh-0.3.4.tgz#3a018ceb526cc6f6df2bb504b2bfe8e3a4934ec5"