Run Redux Saga on startup

Asked

Viewed 228 times

1

I created an offline app first to perform offline actions and send when online, and it works fine, but when the app closes and restarts the action queue does not perform because whoever checks the connection and triggers the action queue is a saga-middleware, and I need that saga to run when the app starts.

aquivo offlineSaga:

import { put, take } from 'redux-saga/effects';
import { eventChannel } from 'redux-saga';
import { NetInfo } from 'react-native';
import { OFFLINE, ONLINE } from 'redux-offline-queue';


export function* startWatchingNetworkConnectivity(){

    const channel = eventChannel(emitter => {
        NetInfo.isConnected.addEventListener("connectionChange", emitter);

        return () => NetInfo.isConnected.removeEventListener("connectionChange", emitter);
    });

    try{

        while(true){

            const isConnected = yield take(channel);

            if(isConnected)
                yield put({ type: ONLINE });
            else
                yield put({ type: OFFLINE });
        }

    } finally {

        channel.close();

    }

}

filing cabinet saga:

import { takeEvery, put, call, all, spawn } from 'redux-saga/effects';
import api from './services/api';
// import errorMessage from './errorMessage';
import { startWatchingNetworkConnectivity } from './offlineSaga';


function* asyncEndScheduling(action){

    try{

        const url = `api/auth/driver/updateScheduling/${action.idScheduling}/key/0bff134c-0227-4d31-9c3b-d922f75f3d98`;

        const response = yield call(api.post, url);

        const { schedulings } = response.data.agendamentos;

        yield put({
            type: 'SUCCESS_END_SCHEDULING',
            newSchedulings: schedulings,
        });

    } catch(err){

        yield put({
            type: 'ERROR_END_SCHEDULING',
            error: 'Erro',
        });

    }

}

export default function* root(){

    yield all([

        spawn(startWatchingNetworkConnectivity),

        takeEvery('REQUEST_END_SCHEDULING', asyncEndScheduling),
    ]);

}

I need to execute the offlineSaga when the app is started, how can I do this?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.