Skip to content

Commit

Permalink
chore: More functional example
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapsssito committed Feb 20, 2020
1 parent 1fa3a7d commit 0f12188
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
56 changes: 27 additions & 29 deletions examples/backgroundExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -82,7 +98,7 @@ class App extends React.Component {
<View style={styles.body}>
<TouchableOpacity
style={{height: 100, width: 100, backgroundColor: 'red'}}
onPress={this.initBackground}></TouchableOpacity>
onPress={this.toggleBackground}></TouchableOpacity>
</View>
</ScrollView>
</SafeAreaView>
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions examples/backgroundExample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions examples/backgroundExample/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0f12188

Please sign in to comment.