| author | Riwad Salim |
| Wed, 15 Aug 2018 23:39:02 +0200 | |
| changeset 145 | 5d2bc8c877ea |
| parent 134 | be36eed5e6e0 |
| child 168 | ea92f4fe783d |
| permissions | -rw-r--r-- |
|
132
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
1 |
import { put, take, all, select, spawn, race, call, fork} from 'redux-saga/effects' |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
2 |
import { delay } from 'redux-saga'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
import * as types from '../constants/actionTypes'; |
|
134
be36eed5e6e0
add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
132
diff
changeset
|
4 |
import { getLastSync } from '../selectors/syncSelectors'; |
|
be36eed5e6e0
add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
132
diff
changeset
|
5 |
import { getOnline } from '../selectors/authSelectors'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
import moment from 'moment'; |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
7 |
import { startSynchronize, endSynchronize, updateLastSync } from '../actions/syncActions'; |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
8 |
import { forceSync } from '../actions/networkActions'; |
|
132
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
9 |
import { groupLoadAsync } from '../actions/groupActions'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
import SessionSynchronizer from './SessionSyncronizer'; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
import NoteSynchronizer from './NoteSyncronizer'; |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
12 |
import config from '../config'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
|
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
14 |
function* doSync(context) { |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
|
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
16 |
const online = yield select(getOnline); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
17 |
if(!online) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
18 |
yield put(endSynchronize()); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
19 |
return; |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
20 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
21 |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
const lastSync = yield select(getLastSync); |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
23 |
yield put(startSynchronize()); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
const nextLastSync = moment().unix() |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
// TODO: manage errors |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
try { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
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
|
30 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
const sessionSynchronizer = new SessionSynchronizer(syncObjects.sessions, context.client); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
yield sessionSynchronizer.syncObjects(); |
|
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 |
const noteSynchronizer = new NoteSynchronizer(syncObjects.notes, context.client); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
yield noteSynchronizer.syncObjects(); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
yield put(updateLastSync(nextLastSync)); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
} finally { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
yield put(endSynchronize()); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
} |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
41 |
|
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
42 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
43 |
|
|
132
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
44 |
function* doLoadGroup() { |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
45 |
yield put(groupLoadAsync()); |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
46 |
} |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
47 |
|
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
48 |
function* watchDoSync(context) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
49 |
while (true) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
50 |
yield take(types.SYNC_DO_SYNC); |
|
132
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
51 |
yield spawn(doSync, context); //we use spawn + take, because we do not want it to be interupted |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
52 |
yield fork(doLoadGroup); |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
53 |
yield take(types.SYNC_END_SYNC); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
54 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
55 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
56 |
|
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
57 |
function* delayPutDoSync() { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
58 |
yield put(forceSync()); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
59 |
yield call(delay, config.syncInterval); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
60 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
61 |
|
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
62 |
function* loopDoSync() { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
63 |
while(true) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
64 |
const online = yield select(getOnline); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
65 |
if(!online) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
66 |
yield take(types.STATUS_ONLINE); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
67 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
68 |
yield race({ |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
69 |
delaySync: call(delayPutDoSync), |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
70 |
offline: take(types.STATUS_OFFLINE) |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
71 |
}); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
//--- The root saga |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
export default function* rootSaga(context) { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
yield all([ |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
78 |
watchDoSync(context), |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
79 |
loopDoSync() |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
]); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
} |