Skip to content

Commit

Permalink
Fix mobile root sagas
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Leblow committed May 9, 2024
1 parent 6a0a5a8 commit 05390e6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 72 deletions.
3 changes: 0 additions & 3 deletions packages/mobile/src/store/init/init.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export const initSlice = createSlice({
setStoreReady: state => {
state.ready = true
},
setCryptoEngineInitialized: (state, action: PayloadAction<boolean>) => {
state.isCryptoEngineInitialized = action.payload
},
updateInitDescription: (state, action: PayloadAction<string>) => {
state.initDescription = action.payload
},
Expand Down

This file was deleted.

24 changes: 0 additions & 24 deletions packages/mobile/src/store/init/setupCrypto/setupCrypto.saga.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
takeLeading,
takeEvery,
FixedTask,
delay,
apply,
} from 'typed-redux-saga'
import { PayloadAction } from '@reduxjs/toolkit'
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 38 additions & 5 deletions packages/mobile/src/store/root.saga.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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')
}
}
}
1 change: 1 addition & 0 deletions packages/mobile/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ export const store = configureStore({
})

export const persistor = persistStore(store, {}, () => {
console.log('Redux store is ready!')
store.dispatch(initActions.setStoreReady())
})

0 comments on commit 05390e6

Please sign in to comment.