-
Notifications
You must be signed in to change notification settings - Fork 56
/
old.js
145 lines (115 loc) · 4.4 KB
/
old.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
const FETCH_API = 'FETCH_API';
const fetchApi = function* fetchApi(input:string, init={}, update): Generator<IOEffect, void, any> {
while (true) {
if (call(isServerDown)) {
yield put(update({ other:'pingDown' }));
yield take(SERVER_UP);
}
init.headers = Object.assign({ Accept:'application/json', 'Content-Type':'application/json' }, init.headers);
if (init.body) init.body = JSON.stringify(init.body);
if (!input.startsWith('http')) input = `${SERVER}/api/${input}`;
let res;
try {
res = yield call(fetch, input, init);
} catch(ex) {
// is offline probably
console.log('fetchApi :: fetch ex:', ex.message);
yield put(update({ isDown:ex.message }));
continue;
}
return res;
}
}
// pings server until it is back up
const PING_DOWN = A`PING_DOWN`;
type PingDownAction = { type:typeof PING_DOWN };
const pingDown = (): PingDownAction => ({ type:PING_DOWN });
const pingDownSaga = function* pingDownSaga(): Generator<IOEffect, void, any> {
const updateThis = (thisData?:{}, otherData?:{}) => update({ isLoggedIn:thisData, ...otherData });
while (true) {
yield take(PING_DOWN);
while(true) {
yield put(updateThis({ code:'FETCHING', placeholders:undefined }));
let res;
try {
res = fetch(SERVER)
} catch(ex) {
console.warn('pingDownSaga fetch ex:', ex.message);
yield put(updateThis({ code:'WAITING', placeholders:['5'] }));
yield call(delay, 5000);
}
if (res.status === 200) {
yield put(updateThis(undefined));
break;
}
}
}
}
sagas.push(pingDownSaga);
// pings server until it is back up
const PING_CWS_DOWN = A`PING_CWS_DOWN`;
type PingCwsDownAction = { type:typeof PING_CWS_DOWN };
const pingCwsDown = (): PingCwsDownAction => ({ type:PING_CWS_DOWN });
const pingCwsDownSaga = function* pingCwsDownSaga(): Generator<IOEffect, void, any> {
const updateThis = (thisData?:{}, otherData?:{}) => update({ isCwsDown:thisData, ...otherData });
while (true) {
yield take(PING_CWS_DOWN);
while (true) {
yield put(updateThis({ code:'FETCHING', placeholders:undefined }));
let res;
try {
res = fetch(CWS_SERVER)
} catch(ex) {
console.warn('pingCwsDownSaga fetch ex:', ex.message);
yield call(delay, 5000)
continue;
}
if (res.status === 200) {
yield put(updateThis(undefined));
break;
}
}
}
}
sagas.push(pingCwsDownSaga);
// pings server until it is logged in
const PING_LOGGEDIN = A`PING_LOGGEDIN`;
type PingLoggedInAction = { type:typeof PING_LOGGEDIN };
const pingLoggedIn = (): PingLoggedInAction => ({ type:PING_LOGGEDIN });
const pingLoggedInSaga = function* pingLoggedInSaga(): Generator<IOEffect, void, any> {
const updateThis = (thisData?:{}, otherData?:{}) => update({ isLoggedIn:thisData, ...otherData });
while (true) {
yield take(PING_LOGGEDIN);
while (true) {
yield put(updateThis({ code:'FETCHING' }));
const res = yield call(fetchApi, SERVER);
if (res.status === 401) {
console.warn('pingLoggedInSaga still logged out, got 401');
yield call(delay, 5000);
continue;
}
if (res.status === 200) {
yield put(updateThis(undefined));
break;
}
}
}
}
sagas.push(pingLoggedInSaga);
//
const SIGN = A`SIGN`;
type SignAction = { type:typeof SIGN, values:{| name:string, email:string, password:string, passwordConfirmation:string |} };
const sign = (values): SignAction => ({ type:SIGN, values });
const signSaga = function* sign(action: SignAction): Generator<IOEffect, void, any> {
while (true) {
const action: SignAction = yield take(SIGN);
const { values } = action;
console.log('in sign saga, values:', values);
const { updateThis, actionId } = yield call(standardApi, 'validate', action);
yield put(updateThis({ code:'FETCHING' }));
yield call(delay, 5000);
yield put(updateThis({ code:'OK' }));
}
}
sagas.push(signSaga);