diff --git a/packages/mobile/src/store/init/init.slice.ts b/packages/mobile/src/store/init/init.slice.ts index f9106199d6..92abfb4a45 100644 --- a/packages/mobile/src/store/init/init.slice.ts +++ b/packages/mobile/src/store/init/init.slice.ts @@ -44,9 +44,6 @@ export const initSlice = createSlice({ setStoreReady: state => { state.ready = true }, - setCryptoEngineInitialized: (state, action: PayloadAction) => { - state.isCryptoEngineInitialized = action.payload - }, updateInitDescription: (state, action: PayloadAction) => { state.initDescription = action.payload }, diff --git a/packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.test.ts b/packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.test.ts deleted file mode 100644 index efd5b18bb6..0000000000 --- a/packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.test.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { combineReducers } from '@reduxjs/toolkit' -import { expectSaga } from 'redux-saga-test-plan' -import { call } from 'redux-saga-test-plan/matchers' -import { StoreKeys } from '../../store.keys' -import { initActions, initReducer, InitState } from '../init.slice' -import { initCryptoEngine, setupCryptoSaga } from './setupCrypto.saga' - -describe('setupCryptoSaga', () => { - test('should be defined', async () => { - await expectSaga(setupCryptoSaga) - .withReducer(combineReducers({ [StoreKeys.Init]: initReducer }), { - [StoreKeys.Init]: { - ...new InitState(), - }, - }) - .provide([[call.fn(initCryptoEngine), null]]) - .call(initCryptoEngine) - .put(initActions.setCryptoEngineInitialized(true)) - .hasFinalState({ - [StoreKeys.Init]: { - ...new InitState(), - isCryptoEngineInitialized: true, - }, - }) - .run() - }) -}) diff --git a/packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.ts b/packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.ts deleted file mode 100644 index 61320653a7..0000000000 --- a/packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { setEngine, CryptoEngine } from 'pkijs' - -import { select, call, put } from 'typed-redux-saga' -import { initSelectors } from '../init.selectors' -import { initActions } from '../init.slice' - -export function* setupCryptoSaga(): Generator { - const isCryptoEngineInitialized = yield* select(initSelectors.isCryptoEngineInitialized) - if (!isCryptoEngineInitialized) { - yield* call(initCryptoEngine) - yield* put(initActions.setCryptoEngineInitialized(true)) - } -} - -export const initCryptoEngine = () => { - setEngine( - 'newEngine', - new CryptoEngine({ - name: '', - crypto, - subtle: crypto.subtle, - }) - ) -} diff --git a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts index 2bd11689ba..4f77f32552 100644 --- a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts +++ b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts @@ -10,7 +10,6 @@ import { takeLeading, takeEvery, FixedTask, - delay, apply, } from 'typed-redux-saga' import { PayloadAction } from '@reduxjs/toolkit' @@ -27,16 +26,6 @@ export function* startConnectionSaga( const isAlreadyConnected = yield* select(initSelectors.isWebsocketConnected) if (isAlreadyConnected) return - while (true) { - const isCryptoEngineInitialized = yield* select(initSelectors.isCryptoEngineInitialized) - console.log('WEBSOCKET', 'Waiting for crypto engine to initialize') - if (!isCryptoEngineInitialized) { - yield* delay(500) - } else { - break - } - } - const { dataPort, socketIOSecret } = action.payload console.log('WEBSOCKET', 'Entered start connection saga', dataPort) @@ -74,8 +63,8 @@ function* setConnectedSaga(socket: Socket): Generator { yield* apply(socket, socket.emit, [SocketActionTypes.START]) // Handle suspending current connection - const suspendAction = yield* take(initActions.suspendWebsocketConnection) - yield* call(cancelRootTaskSaga, task, suspendAction) + yield* take(initActions.suspendWebsocketConnection) + yield* call(cancelRootTaskSaga, task) } function* handleSocketLifecycleActions(socket: Socket, socketIOData: WebsocketConnectionPayload): Generator { diff --git a/packages/mobile/src/store/root.saga.ts b/packages/mobile/src/store/root.saga.ts index 9aa9d7caac..e3978f3cf4 100644 --- a/packages/mobile/src/store/root.saga.ts +++ b/packages/mobile/src/store/root.saga.ts @@ -1,18 +1,51 @@ -import { all, takeEvery, takeLeading, fork, cancelled } from 'typed-redux-saga' +import { all, call, take, takeEvery, takeLeading, fork, cancelled } from 'typed-redux-saga' import { nativeServicesMasterSaga } from './nativeServices/nativeServices.master.saga' import { navigationMasterSaga } from './navigation/navigation.master.saga' import { initMasterSaga } from './init/init.master.saga' import { initActions } from './init/init.slice' -import { setupCryptoSaga } from './init/setupCrypto/setupCrypto.saga' import { publicChannels } from '@quiet/state-manager' import { showNotificationSaga } from './nativeServices/showNotification/showNotification.saga' import { clearReduxStore } from './nativeServices/leaveCommunity/leaveCommunity.saga' +import { setEngine, CryptoEngine } from 'pkijs' + +const initCryptoEngine = () => { + setEngine( + 'newEngine', + new CryptoEngine({ + name: '', + crypto, + subtle: crypto.subtle, + }) + ) +} export function* rootSaga(): Generator { console.log('rootSaga starting') + try { + console.log('Initializing crypto engine') + yield* call(initCryptoEngine) + // We don't want to start any sagas until the store is ready in + // case they use the store. Currently, we run these sagas once per + // application lifecycle. However, when we leave the community and + // clear the Redux store, if the Redux store is cleared while a + // saga is running, I suppose there is a possibility of corrupted + // state. Perhaps, it would make more sense to stop this saga, + // clear the store and then restart it, but that requires some + // refactoring. + yield* take(initActions.setStoreReady) + yield* call(storeReadySaga) + } finally { + console.log('rootSaga stopping') + if (yield cancelled()) { + console.log('rootSaga cancelled') + } + } +} + +function* storeReadySaga(): Generator { + console.log('storeReadySaga starting') try { yield all([ - fork(setupCryptoSaga), fork(initMasterSaga), fork(navigationMasterSaga), fork(nativeServicesMasterSaga), @@ -21,9 +54,9 @@ export function* rootSaga(): Generator { takeLeading(initActions.canceledRootTask.type, clearReduxStore), ]) } finally { - console.log('rootSaga stopping') + console.log('storeReadySaga stopping') if (yield cancelled()) { - console.log('rootSaga cancelled') + console.log('storeReadySaga cancelled') } } } diff --git a/packages/mobile/src/store/store.ts b/packages/mobile/src/store/store.ts index 106867a42f..62cfcab17e 100644 --- a/packages/mobile/src/store/store.ts +++ b/packages/mobile/src/store/store.ts @@ -71,5 +71,6 @@ export const store = configureStore({ }) export const persistor = persistStore(store, {}, () => { + console.log('Redux store is ready!') store.dispatch(initActions.setStoreReady()) })