Skip to content

Commit

Permalink
Extend lastKnownPort to lastKnownSocketIOData on android
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Oct 18, 2023
1 parent 140801b commit 3eaf916
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
5 changes: 5 additions & 0 deletions packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('deepLinkSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand Down Expand Up @@ -82,6 +83,7 @@ describe('deepLinkSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand Down Expand Up @@ -118,6 +120,7 @@ describe('deepLinkSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand All @@ -144,6 +147,7 @@ describe('deepLinkSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand Down Expand Up @@ -181,6 +185,7 @@ describe('deepLinkSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand Down
4 changes: 2 additions & 2 deletions packages/mobile/src/store/init/init.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -27,7 +27,7 @@ export const ready = createSelector(initSlice, reducerState => reducerState.read
export const initSelectors = {
isCryptoEngineInitialized,
isWebsocketConnected,
lastKnownDataPort,
lastKnownSocketIOData,
initDescription,
initChecks,
deepLinking,
Expand Down
10 changes: 6 additions & 4 deletions packages/mobile/src/store/init/init.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InitCheck> = initChecksAdapter.setAll(initChecksAdapter.getInitialState(), [])
public ready: boolean = false
Expand All @@ -23,7 +26,7 @@ export interface InitCheckPayload {

export interface WebsocketConnectionPayload {
dataPort: number
socketIOSecret?: string
socketIOSecret: string
}

export interface CloseConnectionPayload {
Expand Down Expand Up @@ -60,9 +63,8 @@ export const initSlice = createSlice({
state.isWebsocketConnected = false
},
setWebsocketConnected: (state, action: PayloadAction<WebsocketConnectionPayload>) => {
const { dataPort } = action.payload
state.isWebsocketConnected = true
state.lastKnownDataPort = dataPort
state.lastKnownSocketIOData = action.payload
const event = InitCheckKeys.Backend
initChecksAdapter.updateOne(state.initChecks, {
changes: {
Expand Down
2 changes: 1 addition & 1 deletion packages/mobile/src/store/init/init.transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const InitTransform = createTransform(
(outboundState: InitState, _key) => {
return {
...new InitState(),
lastKnownDataPort: outboundState.lastKnownDataPort,
lastKnownSocketIOData: outboundState.lastKnownSocketIOData,
}
},
{ whitelist: [StoreKeys.Init] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('onConnectedSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand All @@ -98,6 +99,7 @@ describe('onConnectedSaga', () => {
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
socketIOSecret: 'secret',
})
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
})
})
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
Expand All @@ -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<typeof initActions.setWebsocketConnected> | ReturnType<typeof initActions.suspendWebsocketConnection>
>(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')
Expand Down
2 changes: 1 addition & 1 deletion packages/mobile/src/tests/utils/prepareStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }))
}

0 comments on commit 3eaf916

Please sign in to comment.