Skip to content

Commit

Permalink
Merge pull request #2 from luminos-software/master
Browse files Browse the repository at this point in the history
Added method for continuously watching position updates
  • Loading branch information
itinance committed Jun 16, 2017
2 parents f440ce9 + 78c1aeb commit 8586bc6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ In the root saga spawn the channel watcher:

```javascript

import {watchLocationChannel, getCurrentPosition} from 'redux-saga-location';
import {watchLocationChannel, getCurrentPosition, watchCurrentPosition} from 'redux-saga-location';

export default function * rootSaga() {
yield [
Expand All @@ -49,6 +49,8 @@ export default function * rootSaga() {

# Usage

## Usage of getCurrentPosition

We call `getCurrentPosition` according to the [web-standard-method](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) and (if user permits) the current position will be put into the redux store.

```javascript
Expand All @@ -57,6 +59,16 @@ yield call(getCurrentPosition)

The received data is the same as from [navigator.geolocation.getCurrentPosition](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition).

## Usage of watchCurrentPosition

We call `watchCurrentPosition` according to the [web-standard-method](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition) and (if user permits) the position will be put into the redux store as often as it gets updated.

```javascript
yield call(watchCurrentPosition)
```

The received data is the same as from [navigator.geolocation.watchPosition](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).

We can listen to the event in our reducers per "REDUX_SAGA_LOCATION_SET_POSITION", while errors can be detected with "REDUX_SAGA_LOCATION_SET_ERROR".

If we want to use constans for the actions, we can import them with:
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

'use strict';

export { watchLocationChannel, getCurrentPosition } from './saga/location';
export { watchLocationChannel, getCurrentPosition, watchCurrentPosition } from './saga/location';
export { default as locationReducer } from './reducer/location';
19 changes: 15 additions & 4 deletions saga/location.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import { channel } from 'redux-saga';
import { take, put, call } from 'redux-saga/effects';

export const locationChannel = channel()

import {REDUX_SAGA_LOCATION_ACTION_SET_POSITION
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR
, REDUX_SAGA_LOCATION_ACTION_REQUEST} from '../actions';
import {
REDUX_SAGA_LOCATION_ACTION_SET_POSITION,
REDUX_SAGA_LOCATION_ACTION_SET_ERROR,
REDUX_SAGA_LOCATION_ACTION_REQUEST
} from '../actions';

export function * watchLocationChannel() {
while (true) {
Expand All @@ -26,3 +27,13 @@ export function * getCurrentPosition(options) {
);
}

export function * watchCurrentPosition(options) {
locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_REQUEST})
navigator.geolocation.watchPosition(
position => {
locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_SET_POSITION, position})
},
(error) => locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_SET_ERROR, error}),
options
);
}

0 comments on commit 8586bc6

Please sign in to comment.