如何在taskmanager回调内部使用yield响应本机

时间:2019-08-01 20:55:41

标签: javascript react-redux redux-saga

我想在TaskManager.defineTask回调中使用yield并使用yield调度redux存储,但是我做不到

我已经尝试了一切

import { all, put, call } from 'redux-saga/effects'
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';

function* dispatchLocation(locations){
    console.log("Entro d")
    yield put({ type: 'permission/SET_LOCATION', data: locations })
}

TaskManager.defineTask('LocationWatcher', ({ data: { locations }, error }) => {
    if (error) {
        // check `error.message` for more details.
        return;
    }
    console.log('enn')
    yield dispatchLocation(locations)
});

function* watchLocation(){
    let { status } = yield Permissions.getAsync(Permissions.LOCATION);
    console.log(status)
    if (status === 'granted') {
        Location.startLocationUpdatesAsync('LocationWatcher',{})
    }
}

export default function* rootSaga() {
    //call(watchLocation())
    yield all([
        watchLocation()
    ])
}

1 个答案:

答案 0 :(得分:0)

在javascript中,如果您想使用name,则需要从生成器函数中使用,因此您必须重写yield回调以成为生成器函数,如

LocationWatcher

这表示无法完成您在此处查找的内容-TaskManager.defineTask('LocationWatcher', function* ({ data: { locations }, error }) { ... }); 只能从redux-saga回调没有的传奇中派生出屈服的动作。我认为这里更适合您的路径是导入您的redux存储并直接从您的任务中调度操作。像这样:

LocationWatcher

并完全取消使用import store from './path/to/redux/store'; TaskManager.defineTask('LocationWatcher', ({ data: { locations }, error }) => { if (error) { // check `error.message` for more details. return; } console.log('enn') store.dispatch({ type: 'permission/SET_LOCATION', data: locations }) }); 生成器。