client/src/sagas/syncSaga.js
changeset 129 d48946d164c6
child 130 78246db1cbac
equal deleted inserted replaced
128:34a75bd8d0b9 129:d48946d164c6
       
     1 import { put, take, all, select } from 'redux-saga/effects'
       
     2 import * as types from '../constants/actionTypes';
       
     3 import { getLastSync } from './selectors';
       
     4 import moment from 'moment';
       
     5 import { endSynchronize, updateLastSync } from '../actions/syncActions';
       
     6 import SessionSynchronizer from './SessionSyncronizer';
       
     7 import NoteSynchronizer from './NoteSyncronizer';
       
     8 
       
     9 
       
    10 function* watchSync(context) {
       
    11   while (true) {
       
    12     yield take(types.SYNC_DO_SYNC);
       
    13     const lastSync = yield select(getLastSync);
       
    14 
       
    15 
       
    16     //const sessions = yield context.client.get('/api/notes/sessions/', {modified_since: lastSync});
       
    17     const nextLastSync = moment().unix()
       
    18 
       
    19     // TODO: manage errors
       
    20     try {
       
    21       const syncObjects = yield context.client.get('/api/notes/sync/', { modified_since: lastSync });
       
    22 
       
    23       const sessionSynchronizer = new SessionSynchronizer(syncObjects.sessions, context.client);
       
    24       yield sessionSynchronizer.syncObjects();
       
    25 
       
    26       const noteSynchronizer = new NoteSynchronizer(syncObjects.notes, context.client);
       
    27       yield noteSynchronizer.syncObjects();
       
    28 
       
    29       yield put(updateLastSync(nextLastSync));
       
    30     } finally {
       
    31       yield put(endSynchronize());
       
    32     }
       
    33   }
       
    34 }
       
    35 
       
    36 //--- The root saga
       
    37 export default function* rootSaga(context) {
       
    38   yield all([
       
    39     watchSync(context)
       
    40   ]);
       
    41 }