| author | ymh <ymh.work@gmail.com> |
| Tue, 01 Aug 2017 12:20:14 +0200 | |
| changeset 132 | 906a6c7c7943 |
| parent 130 | 78246db1cbac |
| child 134 | be36eed5e6e0 |
| 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'; |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
4 |
import { getLastSync, getOnline } from './selectors'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
import moment from 'moment'; |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
6 |
import { startSynchronize, endSynchronize, updateLastSync } from '../actions/syncActions'; |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
7 |
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
|
8 |
import { groupLoadAsync } from '../actions/groupActions'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
import SessionSynchronizer from './SessionSyncronizer'; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
import NoteSynchronizer from './NoteSyncronizer'; |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
11 |
import config from '../config'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
|
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
13 |
function* doSync(context) { |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
|
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
15 |
const online = yield select(getOnline); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
16 |
if(!online) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
17 |
yield put(endSynchronize()); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
18 |
return; |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
19 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
20 |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
const lastSync = yield select(getLastSync); |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
22 |
yield put(startSynchronize()); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
const nextLastSync = moment().unix() |
|
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 |
// TODO: manage errors |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
try { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
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
|
29 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
const sessionSynchronizer = new SessionSynchronizer(syncObjects.sessions, context.client); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
yield sessionSynchronizer.syncObjects(); |
|
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 |
const noteSynchronizer = new NoteSynchronizer(syncObjects.notes, context.client); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
yield noteSynchronizer.syncObjects(); |
|
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 |
yield put(updateLastSync(nextLastSync)); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
} finally { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
yield put(endSynchronize()); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
} |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
40 |
|
|
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 |
|
|
132
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
43 |
function* doLoadGroup() { |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
44 |
yield put(groupLoadAsync()); |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
45 |
} |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
46 |
|
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
47 |
function* watchDoSync(context) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
48 |
while (true) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
49 |
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
|
50 |
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
|
51 |
yield fork(doLoadGroup); |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
52 |
yield take(types.SYNC_END_SYNC); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
53 |
} |
|
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 |
function* delayPutDoSync() { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
57 |
yield put(forceSync()); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
58 |
yield call(delay, config.syncInterval); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
59 |
} |
|
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 |
function* loopDoSync() { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
62 |
while(true) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
63 |
const online = yield select(getOnline); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
64 |
if(!online) { |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
65 |
yield take(types.STATUS_ONLINE); |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
66 |
} |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
67 |
yield race({ |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
68 |
delaySync: call(delayPutDoSync), |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
69 |
offline: take(types.STATUS_OFFLINE) |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
70 |
}); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
} |
|
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 |
//--- The root saga |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
export default function* rootSaga(context) { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
yield all([ |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
77 |
watchDoSync(context), |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
78 |
loopDoSync() |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
]); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
} |