-
Notifications
You must be signed in to change notification settings - Fork 503
/
sagas.js
58 lines (52 loc) · 1.83 KB
/
sagas.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
import { call, put, select, take } from 'redux-saga/effects'
import { compose } from 'ramda'
import * as A from '../../actions'
import * as bchSelectors from '../../kvStore/bch/selectors'
import * as walletSelectors from '../../wallet/selectors'
import { Socket } from '../../../network/index'
import * as bchActions from '../../data/bch/actions'
import * as bchAT from '../../data/bch/actionTypes'
// TO REVIEW
export default ({ api, bchSocket }) => {
const send = bchSocket.send.bind(bchSocket)
const onOpen = function * () {
const subscribeInfo = yield select(walletSelectors.getInitialSocketContext)
yield call(compose(send, Socket.onOpenMessage), subscribeInfo)
}
const onMessage = function * (action) {
const message = action.payload
switch (message.op) {
case 'utx':
const spendableContext = yield select(walletSelectors.getSpendableContext)
yield put(bchActions.fetchTransactions('', true))
yield put(bchActions.fetchSpendableBalance(spendableContext))
const transactions = yield take(bchAT.FETCH_BCH_TRANSACTIONS_SUCCESS)
for (let i in transactions.payload.transactions) {
const tx = transactions.payload.transactions[i]
if (tx.hash === message.x.hash) {
if (tx.result > 0) {
yield put(A.webSocket.bitcoin.paymentReceived('You\'ve just received a bitcoin payment.'))
}
break
}
}
break
case 'block':
const newBlock = message.x
yield put(A.data.bch.setBCHLatestBlock(newBlock.blockIndex, newBlock.hash, newBlock.height, newBlock.time))
break
case 'pong':
break
default:
console.log('unknown type for ', message)
break
}
}
const onClose = function * (action) {
}
return {
onOpen,
onMessage,
onClose
}
}