From 3eaf916b276d2f23da83e9a4c65dd03be43c9931 Mon Sep 17 00:00:00 2001 From: Vin Kabuki Date: Wed, 18 Oct 2023 10:14:04 +0200 Subject: [PATCH] Extend lastKnownPort to lastKnownSocketIOData on android --- .../store/init/deepLink/deepLink.saga.test.ts | 5 ++++ .../mobile/src/store/init/init.selectors.ts | 4 +-- packages/mobile/src/store/init/init.slice.ts | 10 +++++--- .../mobile/src/store/init/init.transform.ts | 2 +- .../init/onConnected/onConnected.saga.test.ts | 2 ++ .../restoreConnection.saga.test.ts | 25 ++++++++++--------- .../restoreConnection.saga.ts | 12 +++------ .../startConnection/startConnection.saga.ts | 16 +++++------- .../mobile/src/tests/utils/prepareStore.ts | 2 +- 9 files changed, 40 insertions(+), 38 deletions(-) diff --git a/packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts b/packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts index a2fb46277e..bfda410be1 100644 --- a/packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts +++ b/packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts @@ -52,6 +52,7 @@ describe('deepLinkSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) @@ -82,6 +83,7 @@ describe('deepLinkSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) @@ -118,6 +120,7 @@ describe('deepLinkSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) @@ -144,6 +147,7 @@ describe('deepLinkSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) @@ -181,6 +185,7 @@ describe('deepLinkSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) diff --git a/packages/mobile/src/store/init/init.selectors.ts b/packages/mobile/src/store/init/init.selectors.ts index 914c1ead2d..e6db008c3d 100644 --- a/packages/mobile/src/store/init/init.selectors.ts +++ b/packages/mobile/src/store/init/init.selectors.ts @@ -12,7 +12,7 @@ export const isCryptoEngineInitialized = createSelector( export const isWebsocketConnected = createSelector(initSlice, reducerState => reducerState.isWebsocketConnected) -export const lastKnownDataPort = createSelector(initSlice, reducerState => reducerState.lastKnownDataPort) +export const lastKnownSocketIOData = createSelector(initSlice, reducerState => reducerState.lastKnownSocketIOData) export const initDescription = createSelector(initSlice, reducerState => reducerState.initDescription) @@ -27,7 +27,7 @@ export const ready = createSelector(initSlice, reducerState => reducerState.read export const initSelectors = { isCryptoEngineInitialized, isWebsocketConnected, - lastKnownDataPort, + lastKnownSocketIOData, initDescription, initChecks, deepLinking, diff --git a/packages/mobile/src/store/init/init.slice.ts b/packages/mobile/src/store/init/init.slice.ts index de00bafcdb..2be28a2fe6 100644 --- a/packages/mobile/src/store/init/init.slice.ts +++ b/packages/mobile/src/store/init/init.slice.ts @@ -10,7 +10,10 @@ export class InitState { public deepLinking: boolean = false public isCryptoEngineInitialized: boolean = false public isWebsocketConnected: boolean = false - public lastKnownDataPort: number = 0 + public lastKnownSocketIOData: WebsocketConnectionPayload = { + dataPort: 0, + socketIOSecret: '', + } public initDescription: string = 'Starting Quiet' public initChecks: EntityState = initChecksAdapter.setAll(initChecksAdapter.getInitialState(), []) public ready: boolean = false @@ -23,7 +26,7 @@ export interface InitCheckPayload { export interface WebsocketConnectionPayload { dataPort: number - socketIOSecret?: string + socketIOSecret: string } export interface CloseConnectionPayload { @@ -60,9 +63,8 @@ export const initSlice = createSlice({ state.isWebsocketConnected = false }, setWebsocketConnected: (state, action: PayloadAction) => { - const { dataPort } = action.payload state.isWebsocketConnected = true - state.lastKnownDataPort = dataPort + state.lastKnownSocketIOData = action.payload const event = InitCheckKeys.Backend initChecksAdapter.updateOne(state.initChecks, { changes: { diff --git a/packages/mobile/src/store/init/init.transform.ts b/packages/mobile/src/store/init/init.transform.ts index aa3329da1f..db042bc717 100644 --- a/packages/mobile/src/store/init/init.transform.ts +++ b/packages/mobile/src/store/init/init.transform.ts @@ -9,7 +9,7 @@ export const InitTransform = createTransform( (outboundState: InitState, _key) => { return { ...new InitState(), - lastKnownDataPort: outboundState.lastKnownDataPort, + lastKnownSocketIOData: outboundState.lastKnownSocketIOData, } }, { whitelist: [StoreKeys.Init] } diff --git a/packages/mobile/src/store/init/onConnected/onConnected.saga.test.ts b/packages/mobile/src/store/init/onConnected/onConnected.saga.test.ts index e95f8fcad0..ed2533ca05 100644 --- a/packages/mobile/src/store/init/onConnected/onConnected.saga.test.ts +++ b/packages/mobile/src/store/init/onConnected/onConnected.saga.test.ts @@ -74,6 +74,7 @@ describe('onConnectedSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) @@ -98,6 +99,7 @@ describe('onConnectedSaga', () => { store.dispatch( initActions.setWebsocketConnected({ dataPort: 5001, + socketIOSecret: 'secret', }) ) diff --git a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts b/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts index 4cb6f820a7..c9fe7cc4dd 100644 --- a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts +++ b/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts @@ -1,3 +1,4 @@ +import { socket } from '@quiet/state-manager' import { combineReducers } from '@reduxjs/toolkit' import { expectSaga } from 'redux-saga-test-plan' import { StoreKeys } from '../../../store.keys' @@ -6,35 +7,35 @@ import { restoreConnectionSaga } from './restoreConnection.saga' describe('restoreConnectionSaga', () => { test('do nothing if connection is already started', async () => { + const socketIOData = { + dataPort: 9477, + socketIOSecret: 'secret', + } await expectSaga(restoreConnectionSaga) .withReducer(combineReducers({ [StoreKeys.Init]: initReducer }), { [StoreKeys.Init]: { ...new InitState(), isWebsocketConnected: true, - lastKnownDataPort: 9477, + lastKnownSocketIOData: socketIOData, }, }) - .not.put( - initActions.startWebsocketConnection({ - dataPort: 9477, - }) - ) + .not.put(initActions.startWebsocketConnection(socketIOData)) .run() }) test('do nothing if last known data port is not set', async () => { + const socketIOData = { + dataPort: 0, + socketIOSecret: 'secret', + } await expectSaga(restoreConnectionSaga) .withReducer(combineReducers({ [StoreKeys.Init]: initReducer }), { [StoreKeys.Init]: { ...new InitState(), isWebsocketConnected: false, - lastKnownDataPort: 0, + lastKnownSocketIOData: socketIOData, }, }) - .not.put( - initActions.startWebsocketConnection({ - dataPort: 0, - }) - ) + .not.put(initActions.startWebsocketConnection(socketIOData)) .run() }) }) diff --git a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts b/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts index 23621b1b89..b7dde652ff 100644 --- a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts +++ b/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts @@ -1,4 +1,4 @@ -import { delay, put, select, take } from 'typed-redux-saga' +import { delay, put, select } from 'typed-redux-saga' import { initSelectors } from '../../init.selectors' import { initActions } from '../../init.slice' @@ -9,13 +9,9 @@ export function* restoreConnectionSaga(): Generator { yield* delay(WEBSOCKET_CONNECTION_DELAY + 1000) const isWebsocketConnected = yield* select(initSelectors.isWebsocketConnected) - const dataPort = yield* select(initSelectors.lastKnownDataPort) + const socketIOData = yield* select(initSelectors.lastKnownSocketIOData) - if (!isWebsocketConnected && dataPort !== 0) { - yield* put( - initActions.startWebsocketConnection({ - dataPort, - }) - ) + if (!isWebsocketConnected && socketIOData.dataPort !== 0) { + yield* put(initActions.startWebsocketConnection(socketIOData)) } } diff --git a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts index 8d867c8e78..cfcf8272c7 100644 --- a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts +++ b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts @@ -3,7 +3,7 @@ import { put, call, cancel, fork, takeEvery, FixedTask } from 'typed-redux-saga' import { PayloadAction } from '@reduxjs/toolkit' import { socket as stateManager } from '@quiet/state-manager' import { encodeSecret } from '@quiet/common' -import { initActions } from '../init.slice' +import { initActions, WebsocketConnectionPayload } from '../init.slice' import { eventChannel } from 'redux-saga' export function* startConnectionSaga( @@ -22,7 +22,7 @@ export function* startConnectionSaga( authorization: `Basic ${token}`, }, }) - yield* fork(handleSocketLifecycleActions, socket, dataPort) + yield* fork(handleSocketLifecycleActions, socket, action.payload) // Handle opening/restoring connection yield* takeEvery(initActions.setWebsocketConnected, setConnectedSaga, socket) } @@ -33,24 +33,20 @@ function* setConnectedSaga(socket: Socket): Generator { yield* takeEvery(initActions.suspendWebsocketConnection, cancelRootTaskSaga, task) } -function* handleSocketLifecycleActions(socket: Socket, dataPort: number): Generator { - const socketChannel = yield* call(subscribeSocketLifecycle, socket, dataPort) +function* handleSocketLifecycleActions(socket: Socket, socketIOData: WebsocketConnectionPayload): Generator { + const socketChannel = yield* call(subscribeSocketLifecycle, socket, socketIOData) yield takeEvery(socketChannel, function* (action) { yield put(action) }) } -function subscribeSocketLifecycle(socket: Socket, dataPort: number) { +function subscribeSocketLifecycle(socket: Socket, socketIOData: WebsocketConnectionPayload) { return eventChannel< ReturnType | ReturnType >(emit => { socket.on('connect', async () => { console.log('websocket connected') - emit( - initActions.setWebsocketConnected({ - dataPort, - }) - ) + emit(initActions.setWebsocketConnected(socketIOData)) }) socket.on('disconnect', () => { console.log('closing socket connection') diff --git a/packages/mobile/src/tests/utils/prepareStore.ts b/packages/mobile/src/tests/utils/prepareStore.ts index bec14aeee2..149c2fe576 100644 --- a/packages/mobile/src/tests/utils/prepareStore.ts +++ b/packages/mobile/src/tests/utils/prepareStore.ts @@ -92,5 +92,5 @@ function* mockSocketConnectionSaga(socket: MockedSocket): Generator { socket.socketClient.emit('connect') }) }) - yield* put(initActions.startWebsocketConnection({ dataPort: 4677 })) + yield* put(initActions.startWebsocketConnection({ dataPort: 4677, socketIOSecret: 'secret' })) }