-
Notifications
You must be signed in to change notification settings - Fork 503
/
sagas.js
91 lines (82 loc) · 3.04 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
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
import { call, put, select, take } from 'redux-saga/effects'
import { compose } from 'ramda'
import * as A from '../../actions'
import { Wrapper } from '../../../types/index'
import * as walletSelectors from '../../wallet/selectors'
import { Socket } from '../../../network/index'
import * as btcActions from '../../data/bitcoin/actions'
import * as btcAT from '../../data/bitcoin/actionTypes'
// TO REVIEW
export default ({ api, btcSocket }) => {
const send = btcSocket.send.bind(btcSocket)
const onOpen = function * () {
const subscribeInfo = yield select(walletSelectors.getInitialSocketContext)
yield call(compose(send, Socket.onOpenMessage), subscribeInfo)
}
const dispatchLogoutEvent = function * () {
yield window.dispatchEvent(new window.Event('wallet.core.logout'))
}
const onMessage = function * (action) {
const message = action.payload
switch (message.op) {
case 'on_change':
const newChecksum = message.x.checksum
const wrapper = yield select(walletSelectors.getWrapper)
const oldChecksum = Wrapper.selectPayloadChecksum(wrapper)
if (oldChecksum !== newChecksum) {
yield call(refreshWrapper)
const walletContext = yield select(walletSelectors.getWalletContext)
yield put(btcActions.fetchData(walletContext))
}
break
case 'utx':
const spendableContext = yield select(walletSelectors.getSpendableContext)
yield put(btcActions.fetchTransactions('', true))
yield put(btcActions.fetchSpendableBalance(spendableContext))
const transactions = yield take(btcAT.FETCH_BITCOIN_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.bitcoin.setBitcoinLatestBlock(newBlock.blockIndex, newBlock.hash, newBlock.height, newBlock.time))
break
case 'pong':
break
case 'email_verified':
yield put(A.settings.setEmailVerified())
break
case 'wallet_logout':
yield call(dispatchLogoutEvent)
break
default:
console.log('unknown type for ', message)
break
}
}
const onClose = function * (action) {
}
const refreshWrapper = function * () {
const guid = yield select(walletSelectors.getGuid)
const skey = yield select(walletSelectors.getSharedKey)
const password = yield select(walletSelectors.getMainPassword)
try {
const newWrapper = yield call(api.fetchWallet, guid, skey, undefined, password)
yield put(A.wallet.refreshWrapper(newWrapper))
} catch (e) {
console.log('REFRESH WRAPPER FAILED (WEBSOCKET) :: should dispatch error action ?')
}
}
return {
onOpen,
onMessage,
onClose
}
}