client/src/sagas/syncSaga.js
author ymh <ymh.work@gmail.com>
Fri, 28 Jul 2017 19:40:35 +0200
changeset 129 d48946d164c6
child 130 78246db1cbac
permissions -rw-r--r--
Add a first version of synchronisation Remove redux-offline dependency make the redux state fully immutable TODO: better error management TODO: make syncronization work automatically
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import { put, take, all, select } from 'redux-saga/effects'
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import * as types from '../constants/actionTypes';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import { getLastSync } from './selectors';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import moment from 'moment';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import { endSynchronize, updateLastSync } from '../actions/syncActions';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import SessionSynchronizer from './SessionSyncronizer';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import NoteSynchronizer from './NoteSyncronizer';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
function* watchSync(context) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
  while (true) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    yield take(types.SYNC_DO_SYNC);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    const lastSync = yield select(getLastSync);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    //const sessions = yield context.client.get('/api/notes/sessions/', {modified_since: lastSync});
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    const nextLastSync = moment().unix()
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    // TODO: manage errors
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    try {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
      const syncObjects = yield context.client.get('/api/notes/sync/', { modified_since: lastSync });
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
      const sessionSynchronizer = new SessionSynchronizer(syncObjects.sessions, context.client);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
      yield sessionSynchronizer.syncObjects();
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
      const noteSynchronizer = new NoteSynchronizer(syncObjects.notes, context.client);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
      yield noteSynchronizer.syncObjects();
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
      yield put(updateLastSync(nextLastSync));
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    } finally {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
      yield put(endSynchronize());
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
}
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
//--- The root saga
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
export default function* rootSaga(context) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
  yield all([
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    watchSync(context)
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
  ]);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
}