-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redux toolkit setup with socket middleware
- Loading branch information
1 parent
efe901f
commit 48517f3
Showing
11 changed files
with
283 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-restricted-syntax */ | ||
import type { Middleware } from 'redux' | ||
import type { Socket } from 'socket.io-client' | ||
import { io } from 'socket.io-client' | ||
|
||
import { socketActions, socketConnected } from '@/store/slice/socketSlice' | ||
|
||
import { socketListeners } from './util' | ||
|
||
export const socketMiddleware: Middleware = ({ dispatch, getState }) => { | ||
let socket: Socket | ||
return (next) => { | ||
socket = io('http://localhost:8000', {}) | ||
socket.on('connect', () => { | ||
dispatch(socketConnected('')) | ||
console.log('socket connected!!') | ||
}) | ||
|
||
// Register All Listeners Here | ||
if (socket) { | ||
for (const listner of socketListeners) { | ||
if (!socket.hasListeners(listner)) { | ||
socket.on(listner, (data: string) => console.log(data)) | ||
} | ||
} | ||
} | ||
return (action) => { | ||
// Socket | ||
const isConnected = getState().socket.isConnected && socket | ||
|
||
if (socketActions.socketDisconnected.match(action)) { | ||
socket.on('disconnect', () => { | ||
socket.close() | ||
// @ts-ignore | ||
socket = null | ||
console.log('socket disconnected!!') | ||
}) | ||
} | ||
|
||
if (socketActions.socketGreet.match(action) && isConnected) { | ||
socket.emit('greet', action.payload) | ||
} | ||
|
||
// Socket Ends | ||
next(action) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const socketListeners = ['greet', 'ping'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { combineReducers } from '@reduxjs/toolkit' | ||
|
||
import { socketSliceReducer } from '@/store/slice/socketSlice' | ||
|
||
const rootReducer = combineReducers({ | ||
socket: socketSliceReducer | ||
}) | ||
|
||
export default rootReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Slice } from '@reduxjs/toolkit' | ||
import { createSlice } from '@reduxjs/toolkit' | ||
|
||
import type { InitialState } from '@/store/types' | ||
|
||
const initialState: InitialState = { | ||
isConnecting: false, | ||
isConnected: false, | ||
socket: null | ||
} | ||
|
||
export const socketSlice: Slice<InitialState> = createSlice({ | ||
name: 'socketSlice', | ||
initialState, | ||
reducers: { | ||
socketConnected: (state, action) => { | ||
const { payload } = action | ||
return { | ||
...state, | ||
isConnected: true, | ||
isConnecting: false, | ||
socket: payload | ||
} | ||
}, | ||
socketDisconnected: (state) => { | ||
return { | ||
...state, | ||
isConnected: false, | ||
isConnecting: false, | ||
socket: null | ||
} | ||
}, | ||
socketConnecting: (state) => { | ||
return state | ||
}, | ||
socketGreet: (state) => { | ||
return state | ||
} | ||
} | ||
}) | ||
|
||
export const { socketConnected, socketConnecting, socketGreet } = | ||
socketSlice.actions | ||
export const socketActions = socketSlice.actions | ||
|
||
export const socketSliceReducer = socketSlice.reducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { configureStore } from '@reduxjs/toolkit' | ||
|
||
import { socketMiddleware } from '@/store/middlewares/socketMiddleware' | ||
import rootReducer from '@/store/rootReducer' | ||
|
||
const store = configureStore({ | ||
reducer: rootReducer, | ||
middleware: (getDefaultMiddleware) => { | ||
return getDefaultMiddleware().concat([socketMiddleware]) | ||
} | ||
}) | ||
|
||
export default store | ||
|
||
export type RootState = ReturnType<typeof store.getState> | ||
export type AppDispatch = typeof store.dispatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type InitialState = { | ||
isConnecting: boolean | ||
isConnected: boolean | ||
socket: null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.