Skip to content

Commit 5014c36

Browse files
committed
refactor: added a queue to process messages in series
1 parent dfdfbf1 commit 5014c36

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/sagas/walletConnect.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ import {
6060
select,
6161
race,
6262
spawn,
63+
delay,
64+
actionChannel,
6365
} from 'redux-saga/effects';
6466
import { eventChannel } from 'redux-saga';
6567
import { get, values } from 'lodash';
@@ -177,9 +179,9 @@ function* init() {
177179
// Refresh redux with the active sessions, loaded from storage
178180
// Pass extend = true so session expiration date get renewed
179181
yield call(refreshActiveSessions, true);
180-
181-
yield fork(listenForNetworkChange);
182182
yield fork(listenForAppStateChange);
183+
yield fork(listenForNetworkChange);
184+
yield fork(requestsListener);
183185

184186
// If the wallet is reset, we should cancel all listeners
185187
yield take(types.RESET_WALLET);
@@ -221,6 +223,7 @@ export function* checkForPendingRequests() {
221223
const { web3wallet } = yield select((state) => state.walletConnect.client);
222224

223225
yield call([web3wallet, web3wallet.getPendingAuthRequests]);
226+
yield call([web3wallet, web3wallet.getPendingSessionRequests]);
224227
}
225228

226229
export function* refreshActiveSessions(extend = false) {
@@ -327,11 +330,26 @@ export function* clearSessions() {
327330
yield call(refreshActiveSessions);
328331
}
329332

333+
function* requestsListener() {
334+
const requestsChannel = yield actionChannel('WC_SESSION_REQUEST');
335+
336+
let action;
337+
while (true) {
338+
try {
339+
action = yield take(requestsChannel);
340+
yield call(processRequest, action);
341+
} catch (error) {
342+
log.error('Error processing request.', error);
343+
yield put(onExceptionCaptured(error));
344+
}
345+
}
346+
}
347+
330348
/**
331349
* This saga will be called (dispatched from the event listener) when a session
332350
* is requested from a dApp
333351
*/
334-
export function* onSessionRequest(action) {
352+
export function* processRequest(action) {
335353
const { payload } = action;
336354
const { params } = payload;
337355

@@ -402,7 +420,7 @@ export function* onSessionRequest(action) {
402420
if (retry) {
403421
shouldAnswer = false;
404422
// Retry the action, exactly as it came:
405-
yield spawn(onSessionRequest, action);
423+
yield* processRequest(action);
406424
}
407425
} break;
408426
case CreateTokenError: {
@@ -418,7 +436,7 @@ export function* onSessionRequest(action) {
418436
if (retry) {
419437
shouldAnswer = false;
420438
// Retry the action, exactly as it came:
421-
yield spawn(onSessionRequest, action);
439+
yield* processRequest(action);
422440
}
423441
} break;
424442
default:
@@ -911,7 +929,6 @@ export function* saga() {
911929
takeLatest(types.SHOW_SIGN_MESSAGE_REQUEST_MODAL, onSignMessageRequest),
912930
takeLatest(types.SHOW_SIGN_ORACLE_DATA_REQUEST_MODAL, onSignOracleDataRequest),
913931
takeLatest(types.SHOW_CREATE_TOKEN_REQUEST_MODAL, onCreateTokenRequest),
914-
takeLeading('WC_SESSION_REQUEST', onSessionRequest),
915932
takeEvery('WC_SESSION_PROPOSAL', onSessionProposal),
916933
takeEvery('WC_SESSION_DELETE', onSessionDelete),
917934
takeEvery('WC_CANCEL_SESSION', onCancelSession),

0 commit comments

Comments
 (0)