diff --git a/src/sagas/device.js b/src/sagas/device.js index f3f80016..d3db3bd4 100644 --- a/src/sagas/device.js +++ b/src/sagas/device.js @@ -3,58 +3,23 @@ import { channel } from '@redux-saga/core'; import { sign } from 'obyte/lib/internal'; import { oClient, getDeviceMessageHashToSign } from './../lib/OCustom'; import { actionTypes } from './../constants'; +import { setToastMessage } from './../actions/app'; import { setExchangeRates } from './../actions/exchangeRates'; import { - selectDeviceAddress, selectPermanentDeviceKeyObj, selectDeviceTempKeyData, } from './../selectors/device'; +let oChannel; -export const oChannel = channel(); - -export function* subscribeToHub() { - try { - oClient.subscribe((err, result) => { - if (err) { - throw new Error('Hub socket error'); - } else { - const [messageType, message] = result; - oChannel.put({ type: messageType, payload: message }); - } - }); - yield call(setInterval, () => oClient.api.heartbeat(), 10 * 1000); - } catch (error) { - yield put(setToastMessage({ - type: 'ERROR', - message: 'Hub connection error', - })); - console.log(error); - } -} - -export function* watchHubMessages() { - while (true) { - const { type, payload } = yield take(oChannel) - - if (type === 'justsaying') { - switch (payload.subject) { - case 'hub/challenge': - yield call(loginToHub, payload.body); - return; - default: - console.log(payload.body); - } - } - console.log(type, payload); - } +if (!oChannel) { + oChannel = channel(); } export function* loginToHub(challenge) { - const deviceAddress = yield select(selectDeviceAddress()); const permanentDeviceKey = yield select(selectPermanentDeviceKeyObj()); const tempDeviceKeyData = yield select(selectDeviceTempKeyData()); - + const objLogin = { challenge, pubkey: permanentDeviceKey.pubB64 }; objLogin.signature = sign( getDeviceMessageHashToSign(objLogin), @@ -74,6 +39,48 @@ export function* loginToHub(challenge) { oClient.justsaying('hub/refresh', null); } +export function* subscribeToHub() { + try { + yield oClient.subscribe((err, result) => { + if (err) { + throw new Error('Hub socket error'); + } else { + const [type, payload] = result; + oChannel.put({ type, payload }); + } + }); + yield call(setInterval, () => oClient.api.heartbeat(), 10 * 1000); + } catch (error) { + yield put(setToastMessage({ + type: 'ERROR', + message: 'Hub connection error', + })); + console.log(error); + } +} + +export function* watchHubMessages() { + try { + while (true) { + const { type, payload } = yield take(oChannel); + + if (type === 'justsaying') { + switch (payload.subject) { + case 'hub/challenge': + yield call(loginToHub, payload.body); + case 'hub/message': + console.log('TODO - parse message'); + case 'exchange_rates': + yield put(setExchangeRates(payload)); + default: + } + } + } + } catch (error) { + console.log(error); + } +} + export default function* watch() { yield watchHubMessages(); } \ No newline at end of file diff --git a/src/sagas/wallet.js b/src/sagas/wallet.js index 74eba324..2a6d5692 100644 --- a/src/sagas/wallet.js +++ b/src/sagas/wallet.js @@ -32,7 +32,7 @@ import { } from './../selectors/wallet'; -export function* initWallet(action) { +export function* initWallet() { try { const walletData = yield select(selectWallet()); if (walletData.password === null || walletData.seedWords === null) { @@ -40,10 +40,10 @@ export function* initWallet(action) { } // Handle websocket traffic - yield call(subscribeToHub, action); + yield call(subscribeToHub); // Fetch wallet data from hub - yield call(fetchBalances, action); - yield call(fetchWitnesses, action); + yield call(fetchBalances); + yield call(fetchWitnesses); yield put(loadWalletHistory()); yield put(initWalletSuccess()); } catch (error) {