-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
243 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,26 @@ | ||
import { configureStore, Middleware } from "@reduxjs/toolkit"; | ||
import rootReducers from "./reducer"; | ||
import logger from "redux-logger"; | ||
import createSagaMiddleware from "redux-saga"; | ||
import rootSaga from "./saga"; | ||
|
||
const sagaMiddleware = createSagaMiddleware(); | ||
const middlewares: Middleware[] = [sagaMiddleware]; | ||
|
||
if (process.env.NODE_ENV === "development") { | ||
middlewares.push(logger); | ||
} | ||
|
||
const store = configureStore({ | ||
reducer: rootReducers, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware().concat(...middlewares), | ||
}); | ||
|
||
sagaMiddleware.run(rootSaga); | ||
|
||
export type RootState = ReturnType<typeof store.getState>; | ||
|
||
export type AppDispatch = typeof store.dispatch; | ||
|
||
export default store; |
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,8 @@ | ||
import { combineReducers } from "@reduxjs/toolkit"; | ||
import message from "./slices/message"; | ||
|
||
const rootReducers = combineReducers({ | ||
messageReducer: message, | ||
}); | ||
|
||
export default rootReducers; |
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,23 @@ | ||
import { all, call, spawn } from "redux-saga/effects"; | ||
import { watchAddMessage } from "./sagas/message"; | ||
|
||
export default function* rootSaga() { | ||
let sagas = [watchAddMessage]; | ||
|
||
// Spawn a detached generater for each 'watcher' saga, that is meant to stay alive for the entire app life-time | ||
// Will catch any otherwise uncaught errors in sagas, and restart those crashed sagas. | ||
yield all( | ||
sagas.map((saga) => | ||
spawn(function* () { | ||
while (true) { | ||
try { | ||
yield call(saga); | ||
break; | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
}), | ||
), | ||
); | ||
} |
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,25 @@ | ||
import { PayloadAction } from "@reduxjs/toolkit"; | ||
import { | ||
Message, | ||
addMessage, | ||
removeMessage, | ||
setMessageExiting, | ||
} from "../slices/message"; | ||
import { put, takeEvery } from "redux-saga/effects"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
function* addMessageSaga(action: PayloadAction<Message>) { | ||
action.payload.id = action.payload.id || uuidv4(); | ||
yield put(addMessage(action.payload)); | ||
// Remove the message after the duration | ||
yield new Promise((resolve) => setTimeout(resolve, action.payload.duration)); | ||
yield put(setMessageExiting(action.payload.id)); | ||
yield new Promise((resolve) => setTimeout(resolve, 1000)); | ||
yield put(removeMessage(action.payload.id)); | ||
} | ||
|
||
export const ADD_MESSAGE_SAGA = "message/addMessage/saga"; | ||
|
||
export function* watchAddMessage() { | ||
yield takeEvery("message/addMessage/saga", addMessageSaga); | ||
} |
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,45 @@ | ||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"; | ||
import { RootState } from ".."; | ||
|
||
export interface Message { | ||
id: string; | ||
content: string; | ||
exiting?: boolean; | ||
duration: number; | ||
} | ||
|
||
export interface MessageState { | ||
messageMap: { [key: string]: Message }; | ||
} | ||
|
||
const initialState: MessageState = { | ||
messageMap: {}, | ||
}; | ||
|
||
const messageSlice = createSlice({ | ||
name: "message", | ||
initialState, | ||
reducers: { | ||
addMessage(state, action: PayloadAction<Message>) { | ||
let addingMessage = action.payload; | ||
state.messageMap[addingMessage.id] = addingMessage; | ||
}, | ||
setMessageExiting(state, action: PayloadAction<string>) { | ||
let exitingMessage = state.messageMap[action.payload]; | ||
if (exitingMessage) { | ||
exitingMessage.exiting = true; | ||
} | ||
}, | ||
removeMessage(state, action: PayloadAction<string>) { | ||
delete state.messageMap[action.payload]; | ||
}, | ||
}, | ||
}); | ||
|
||
export const messageMapSelector = (state: RootState) => | ||
state.messageReducer.messageMap; | ||
|
||
export const { addMessage, setMessageExiting, removeMessage } = | ||
messageSlice.actions; | ||
|
||
export default messageSlice.reducer; |
Oops, something went wrong.