Skip to content

Commit

Permalink
[#495]: remove sagas from rooms duck
Browse files Browse the repository at this point in the history
  • Loading branch information
Themezv committed Apr 20, 2024
1 parent e6f6e5c commit 0550443
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 87 deletions.
2 changes: 1 addition & 1 deletion apps/ligretto-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"react-redux": "^8.0.5",
"react-router": "^6.8.1",
"react-router-dom": "^6.8.1",
"redux-first-history": "^5.1.1",
"redux-first-history": "^5.2.0",
"redux-saga": "^1.3.0",
"socket.io-client": "^4.5.1"
},
Expand Down
6 changes: 5 additions & 1 deletion apps/ligretto-frontend/src/app/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import { IS_DEV_MODE } from '#shared/constants/config'
import rootSaga from './rootSaga'
import { routerMiddleware, createReduxHistory } from './reduxHistoryContext'
import { rootReducer } from './rootReducer'
import { listenerMiddleware } from './listenerModdleware'

const sagaMiddleware = createSagaMiddleware()

export const store = configureStore({
reducer: rootReducer,
devTools: IS_DEV_MODE,
middleware: getDefaultMiddleware => getDefaultMiddleware({ serializableCheck: false, thunk: false }).concat([routerMiddleware, sagaMiddleware]),
middleware: getDefaultMiddleware =>
getDefaultMiddleware({ serializableCheck: false, thunk: false })
.concat([routerMiddleware, sagaMiddleware])
.prepend(listenerMiddleware.middleware),
})

export const history = createReduxHistory(store)
Expand Down
10 changes: 10 additions & 0 deletions apps/ligretto-frontend/src/app/store/listenerModdleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createListenerMiddleware } from '@reduxjs/toolkit'

import { addListeners as roomsAddListener } from '#ducks/rooms'
import type { All } from '#types/store'

export const listenerMiddleware = createListenerMiddleware()

const startAppListening = listenerMiddleware.startListening.withTypes<All>()

roomsAddListener(startAppListening)
3 changes: 1 addition & 2 deletions apps/ligretto-frontend/src/app/store/rootSaga.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { all, fork, takeLatest } from 'redux-saga/effects'

import { socketSaga } from '#entities/socket'
import { roomsRootSaga } from '#ducks/rooms'
import { gameRootSaga } from '#ducks/game'
import { usersRootSaga } from '#ducks/users'
import { authRootSaga, getMeSuccess } from '#ducks/auth'
import { analyticsRootSaga } from '#ducks/analytics/sagas'

export default function* rootSaga() {
yield all([fork(roomsRootSaga), fork(analyticsRootSaga), fork(gameRootSaga), fork(authRootSaga), fork(usersRootSaga), fork(blockedSocketSaga)])
yield all([fork(analyticsRootSaga), fork(gameRootSaga), fork(authRootSaga), fork(usersRootSaga), fork(blockedSocketSaga)])
}

function* blockedSocketSaga() {
Expand Down
3 changes: 1 addition & 2 deletions apps/ligretto-frontend/src/ducks/auth/authSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export const authSlice = createSlice({
name: '@@auth',
reducers: {
logout: () => authInitialState,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getMeRequest: (state, { payload }: PayloadAction<{ token?: string }>) => {
getMeRequest: (state, _action: PayloadAction<{ token?: string }>) => {
state.isLoading = true
},
getMeSuccess: (state, { payload }: PayloadAction<{ userId: string; token: string; isTemporary: boolean }>) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/ligretto-frontend/src/ducks/rooms/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './slice'
export { roomsRootSaga } from './sagas'
export * from './selectors'
export { addListeners } from './listeners'
80 changes: 80 additions & 0 deletions apps/ligretto-frontend/src/ducks/rooms/listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { type TypedStartListening } from '@reduxjs/toolkit'
import { replace, push } from 'redux-first-history'
import { generatePath } from 'react-router-dom'

import {
getRoomsEmitAction,
createRoomEmitAction,
updateRoomsAction as updateRoomsServerAction,
removeRoomAction as removeRoomServerAction,
connectToRoomErrorAction,
connectToRoomEmitAction,
createRoomErrorAction,
createRoomSuccessAction,
} from '@memebattle/ligretto-shared'

import { routes } from '#shared/constants'
import type { RoomsState } from './slice'
import { getRoomsAction, createRoomAction } from './slice'
import { connectToRoomAction, updateRoomsAction, setErrorRoomsAction, removeRoomAction } from './slice'

export function addListeners(startListener: TypedStartListening<{ rooms: RoomsState }>) {
startListener({
actionCreator: getRoomsAction,
effect: (_action, listenerApi) => {
listenerApi.dispatch(getRoomsEmitAction())
},
})

startListener({
actionCreator: createRoomAction,
effect: (action, listenerApi) => {
listenerApi.dispatch(createRoomEmitAction(action.payload))
},
})

startListener({
actionCreator: connectToRoomAction,
effect: (action, listenerApi) => {
listenerApi.dispatch(connectToRoomEmitAction(action.payload))
},
})

startListener({
actionCreator: updateRoomsServerAction,
effect: (action, listenerApi) => {
const rooms = action.payload.rooms
listenerApi.dispatch(updateRoomsAction({ rooms }))
},
})

startListener({
actionCreator: removeRoomServerAction,
effect: (action, listenerApi) => {
const uuid = action.payload.uuid
listenerApi.dispatch(removeRoomAction({ uuid }))
},
})

startListener({
actionCreator: connectToRoomErrorAction,
effect: (_action, listenerApi) => {
listenerApi.dispatch(replace(routes.HOME))
},
})

startListener({
actionCreator: createRoomSuccessAction,
effect: (action, listenerApi) => {
listenerApi.dispatch(setErrorRoomsAction({ error: null }))
listenerApi.dispatch(push(generatePath(routes.GAME, { roomUuid: action.payload.game.id })))
},
})

startListener({
actionCreator: createRoomErrorAction,
effect: (action, listenerApi) => {
listenerApi.dispatch(setErrorRoomsAction({ error: action.payload }))
},
})
}
64 changes: 0 additions & 64 deletions apps/ligretto-frontend/src/ducks/rooms/sagas.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/ligretto-frontend/src/ducks/rooms/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const roomsEntityAdapter = createEntityAdapter({
selectId: (room: Room) => room.uuid,
})

interface RoomsState extends ReturnType<typeof roomsEntityAdapter.getInitialState> {
export interface RoomsState extends ReturnType<typeof roomsEntityAdapter.getInitialState> {
isLoading: boolean
search: string
error: CreateRoomError | null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
createRoomErrorAction,
CreateRoomErrorCode,
createRoomSuccessAction,
updateRooms,
updateRoomsAction,
connectToRoomEmitAction,
connectToRoomErrorAction,
updateGameAction,
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('Games Controller', () => {
expect(socketMockImpl.emit).toBeCalledTimes(2)
expect(socketMockImpl.emit).toBeCalledWith('event', createRoomSuccessAction({ game: state.games[gameId] }))
expect(socketMockImpl.to).toBeCalledWith(SOCKET_ROOM_LOBBY)
expect(socketMockImpl.emit).toBeCalledWith('event', updateRooms({ rooms: [gameToRoom(state.games[gameId])] }))
expect(socketMockImpl.emit).toBeCalledWith('event', updateRoomsAction({ rooms: [gameToRoom(state.games[gameId])] }))
})

it('Should emit createRoomErrorAction if room already exists', async () => {
Expand Down Expand Up @@ -216,7 +216,7 @@ describe('Games Controller', () => {
const state = await database.get(storage => storage)
expect(state).toMatchSnapshot()
expect(socketOne.to).toBeCalledTimes(4)
expect(socketOne.emit).toBeCalledWith('event', updateRooms({ rooms: [gameToRoom(state.games[roomUuid])] }))
expect(socketOne.emit).toBeCalledWith('event', updateRoomsAction({ rooms: [gameToRoom(state.games[roomUuid])] }))
expect(socketOne.emit).toBeCalledWith('event', updateGameAction(state.games[roomUuid]))
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getRoomsEmitAction,
setPlayerStatusEmitAction,
updateGameAction,
updateRooms,
updateRoomsAction,
userJoinToRoomAction,
} from '@memebattle/ligretto-shared'
import { SOCKET_ROOM_LOBBY } from '../config'
Expand All @@ -46,13 +46,13 @@ export class GamesController extends Controller {
}

socket.emit('event', createRoomSuccessAction({ game: newGame }))
socket.to(SOCKET_ROOM_LOBBY).emit('event', updateRooms({ rooms: [gameToRoom(newGame)] }))
socket.to(SOCKET_ROOM_LOBBY).emit('event', updateRoomsAction({ rooms: [gameToRoom(newGame)] }))
}

private async getRooms(socket: Socket) {
socket.join(SOCKET_ROOM_LOBBY)
const games = await this.gameService.getGames()
socket.emit('event', updateRooms({ rooms: games.map(gameToRoom) }))
socket.emit('event', updateRoomsAction({ rooms: games.map(gameToRoom) }))

Check warning on line 55 in apps/ligretto-gameplay-backend/src/controllers/games-controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ export class GamesController extends Controller {
? await this.gameService.addSpectator(roomUuid, { id: userId })
: await this.gameService.addPlayer(roomUuid, { id: userId })

socket.to(SOCKET_ROOM_LOBBY).emit('event', updateRooms({ rooms: [gameToRoom(updatedGame)] }))
socket.to(SOCKET_ROOM_LOBBY).emit('event', updateRoomsAction({ rooms: [gameToRoom(updatedGame)] }))
socket.to(roomUuid).emit('event', updateGameAction(updatedGame))
socket.to(roomUuid).emit('event', userJoinToRoomAction({ userId }))
socket.emit('event', connectToRoomSuccessAction({ game: updatedGame }))
Expand Down Expand Up @@ -137,7 +137,7 @@ export class GamesController extends Controller {

if (game) {
socket.to(game.id).emit('event', updateGameAction(game))
socket.to(SOCKET_ROOM_LOBBY).emit('event', updateRooms({ rooms: [gameToRoom(game)] }))
socket.to(SOCKET_ROOM_LOBBY).emit('event', updateRoomsAction({ rooms: [gameToRoom(game)] }))
} else {
socket.to(SOCKET_ROOM_LOBBY).emit('event', removeRoomAction({ uuid: user.currentGameId }))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ligretto-shared/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const createRoomEmitAction = createAction<dto.CreateGame>('@@rooms/WEBSOC

export const getRoomsEmitAction = createAction('@@rooms/WEBSOCKET/GET_ROOMS')

export const updateRooms = createAction<dto.UpdateRooms>('@@rooms/SERVER/UPDATE_ROOMS_LIST')
export const updateRoomsAction = createAction<dto.UpdateRooms>('@@rooms/SERVER/UPDATE_ROOMS_LIST')

export const removeRoomAction = createAction<dto.RemoveRoom>('@@rooms/SERVER/REMOVE_ROOM_FROM_LIST')

Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3763,7 +3763,7 @@ __metadata:
react-router: ^6.8.1
react-router-dom: ^6.8.1
react-test-renderer: ^16.9.0
redux-first-history: ^5.1.1
redux-first-history: ^5.2.0
redux-saga: ^1.3.0
sass: ^1.53.0
socket.io-client: ^4.5.1
Expand Down Expand Up @@ -20448,13 +20448,13 @@ __metadata:
languageName: node
linkType: hard

"redux-first-history@npm:^5.1.1":
version: 5.1.1
resolution: "redux-first-history@npm:5.1.1"
"redux-first-history@npm:^5.2.0":
version: 5.2.0
resolution: "redux-first-history@npm:5.2.0"
peerDependencies:
history: ^4.7.2 || ^5.0
redux: ^3.6.0 || ^4.0.0
checksum: a13648752aafdbc5f537ccbcae54d7894d9415820a6f0a687f788fdb5fba1dc8d8593b89be8efd29ce551b407f27510c9bfe6c659cbce8d37717fae0482cf394
redux: ^3.6.0 || ^4.0.0 || ^5.0.0
checksum: 81e7d50f409f5b1cd258448f4ca7bfb66be3a6b2bd16aa9a8d5615da7f4afbc2f662f897dfb71f8e32eb7756c2d037661ad1c671e2bbdc09606d38719b234d3d
languageName: node
linkType: hard

Expand Down

0 comments on commit 0550443

Please sign in to comment.