-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpythonCounter.ts
76 lines (69 loc) · 1.74 KB
/
pythonCounter.ts
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
import { Request } from 'zeromq';
import {
Dispatch,
GetState,
PythonCounterT,
PythonActionT,
ThunkDispatchT,
PYTHON_BACKEND_REQUEST,
PYTHON_BACKEND_SUCCESS,
PYTHON_BACKEND_FAILURE
} from '../types';
import pythonConfig from '../constants/python.json';
export function pythonBackendRequest(): PythonActionT {
return {
type: PYTHON_BACKEND_REQUEST
};
}
export function pythonBackendSuccess(v: number): PythonActionT {
return {
type: PYTHON_BACKEND_SUCCESS,
payload: v
};
}
export function pythonBackendFailure(m: string): PythonActionT {
return {
type: PYTHON_BACKEND_FAILURE,
payload: m
};
}
// zeromq send-receive function
async function zeromqMessages(m: PythonCounterT): Promise<number> {
const sock = new Request();
sock.connect(pythonConfig.connString);
await sock.send(JSON.stringify(m));
const [result] = await sock.receive();
return JSON.parse(result.toString()).value;
}
// redux-thunk action for zeromq send-receive
export const pythonCalc = (m: PythonCounterT) => async (dispatch: Dispatch) => {
dispatch(pythonBackendRequest());
return zeromqMessages(m).then(
r => dispatch(pythonBackendSuccess(r)),
e => dispatch(pythonBackendFailure(e.toString))
);
};
export const pythonIncrement = () => async (
dispatch: ThunkDispatchT,
getState: GetState
) => {
// construct message object
const { pythonCounter } = getState();
const message = {
...pythonCounter,
operator: 1
};
dispatch(pythonCalc(message));
};
export const pythonDecrement = () => async (
dispatch: ThunkDispatchT,
getState: GetState
) => {
// construct message object
const { pythonCounter } = getState();
const message = {
...pythonCounter,
operator: -1
};
dispatch(pythonCalc(message));
};