From dbb3fb9747ae62eaebac7ce59fcca88cb48229f8 Mon Sep 17 00:00:00 2001 From: webraptor Date: Fri, 16 Jun 2017 13:54:20 +0300 Subject: [PATCH 1/2] Added method for continuously watching position updates --- README.md | 14 +++++++++++++- saga/location.js | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b25ef85..93469d4 100644 --- a/README.md +++ b/README.md @@ -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 [ @@ -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 @@ -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: diff --git a/saga/location.js b/saga/location.js index bbb250c..774b30d 100644 --- a/saga/location.js +++ b/saga/location.js @@ -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) { @@ -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 + ); +} From 8c2e521bfdcc40768eb8fefdff49ca760316d03c Mon Sep 17 00:00:00 2001 From: webraptor Date: Fri, 16 Jun 2017 13:54:20 +0300 Subject: [PATCH 2/2] Added method for continuously watching position updates --- README.md | 14 +++++++++++++- index.js | 2 +- saga/location.js | 19 +++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b25ef85..93469d4 100644 --- a/README.md +++ b/README.md @@ -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 [ @@ -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 @@ -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: diff --git a/index.js b/index.js index 675b2e9..23d0790 100644 --- a/index.js +++ b/index.js @@ -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'; diff --git a/saga/location.js b/saga/location.js index bbb250c..774b30d 100644 --- a/saga/location.js +++ b/saga/location.js @@ -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) { @@ -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 + ); +}