Skip to content

Commit 1dab861

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

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/sagas/walletConnect.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ import {
5555
cancel,
5656
cancelled,
5757
takeLatest,
58-
takeLeading,
5958
takeEvery,
6059
select,
6160
race,
62-
spawn,
61+
actionChannel,
6362
} from 'redux-saga/effects';
6463
import { eventChannel } from 'redux-saga';
6564
import { get, values } from 'lodash';
@@ -177,9 +176,9 @@ function* init() {
177176
// Refresh redux with the active sessions, loaded from storage
178177
// Pass extend = true so session expiration date get renewed
179178
yield call(refreshActiveSessions, true);
180-
181-
yield fork(listenForNetworkChange);
182179
yield fork(listenForAppStateChange);
180+
yield fork(listenForNetworkChange);
181+
yield fork(requestsListener);
183182

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

223222
yield call([web3wallet, web3wallet.getPendingAuthRequests]);
223+
yield call([web3wallet, web3wallet.getPendingSessionRequests]);
224224
}
225225

226226
export function* refreshActiveSessions(extend = false) {
@@ -327,11 +327,26 @@ export function* clearSessions() {
327327
yield call(refreshActiveSessions);
328328
}
329329

330+
function* requestsListener() {
331+
const requestsChannel = yield actionChannel('WC_SESSION_REQUEST');
332+
333+
let action;
334+
while (true) {
335+
try {
336+
action = yield take(requestsChannel);
337+
yield call(processRequest, action);
338+
} catch (error) {
339+
log.error('Error processing request.', error);
340+
yield put(onExceptionCaptured(error));
341+
}
342+
}
343+
}
344+
330345
/**
331346
* This saga will be called (dispatched from the event listener) when a session
332347
* is requested from a dApp
333348
*/
334-
export function* onSessionRequest(action) {
349+
export function* processRequest(action) {
335350
const { payload } = action;
336351
const { params } = payload;
337352

@@ -402,7 +417,7 @@ export function* onSessionRequest(action) {
402417
if (retry) {
403418
shouldAnswer = false;
404419
// Retry the action, exactly as it came:
405-
yield spawn(onSessionRequest, action);
420+
yield* processRequest(action);
406421
}
407422
} break;
408423
case CreateTokenError: {
@@ -418,7 +433,7 @@ export function* onSessionRequest(action) {
418433
if (retry) {
419434
shouldAnswer = false;
420435
// Retry the action, exactly as it came:
421-
yield spawn(onSessionRequest, action);
436+
yield* processRequest(action);
422437
}
423438
} break;
424439
default:
@@ -911,7 +926,6 @@ export function* saga() {
911926
takeLatest(types.SHOW_SIGN_MESSAGE_REQUEST_MODAL, onSignMessageRequest),
912927
takeLatest(types.SHOW_SIGN_ORACLE_DATA_REQUEST_MODAL, onSignOracleDataRequest),
913928
takeLatest(types.SHOW_CREATE_TOKEN_REQUEST_MODAL, onCreateTokenRequest),
914-
takeLeading('WC_SESSION_REQUEST', onSessionRequest),
915929
takeEvery('WC_SESSION_PROPOSAL', onSessionProposal),
916930
takeEvery('WC_SESSION_DELETE', onSessionDelete),
917931
takeEvery('WC_CANCEL_SESSION', onCancelSession),

0 commit comments

Comments
 (0)