Forked from sectore/01-createObservableFromDeviceEventEmitter.js
Created
November 15, 2021 14:20
Revisions
-
Jens Krause renamed this gist
Oct 28, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Jens Krause revised this gist
Oct 28, 2015 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
Jens Krause created this gist
Oct 28, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ import React, {DeviceEventEmitter} from 'react-native'; import {Observable} from 'rx-lite' /** * Creates an Observable to listen to any event of DeviceEventEmitter * @param type {string} Event type */ export default createObservableFromDeviceEventEmitter$ = type => { let subscription; return Observable.fromEventPattern( handler => subscription = DeviceEventEmitter.addListener(type, handler), handler => subscription.remove() ) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ import Location from 'react-native-location'; import locationDidUpdate$ from './locationDidUpdate'; // init native location Location.requestAlwaysAuthorization(); Location.startUpdatingLocation(); // subscribe to changes const subscription = locationDidUpdate$(2000/* delay ms */) .subscribe( (location) => { // do anything with the location console.log('location updated', location) }, (e) => console.log('onError: %s', e); ); // to unsubscribe just call // subscription.dispose();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ import createObservableFromDeviceEventEmitter$ from './createObservableFromDeviceEventEmitter'; /** * Creates an Observable to listen to `locationUpdated` event * dispatched by Location (react-native-location) */ export default locationDidUpdate$ = (delay=1000) => { return createObservableFromDeviceEventEmitter$('locationUpdated') .throttle(delay) // delay of listening to events .map((location) => { // map latitude + longitude into simple object return { latitude: location.coords.latitude, longitude: location.coords.longitude } }); }