Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. Jens Krause renamed this gist Oct 28, 2015. 1 changed file with 0 additions and 0 deletions.
  2. Jens Krause revised this gist Oct 28, 2015. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. Jens Krause created this gist Oct 28, 2015.
    14 changes: 14 additions & 0 deletions createObservableFromDeviceEventEmitter$.js
    Original 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()
    )
    }
    20 changes: 20 additions & 0 deletions locationCheckExample.js
    Original 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();

    18 changes: 18 additions & 0 deletions locationDidUpdate.js
    Original 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
    }
    });
    }