Skip to content

Commit

Permalink
Merge pull request reduxjs#1876 from FaberVitale/refactor/listener-mi…
Browse files Browse the repository at this point in the history
…ddleware-polish-counter-template
  • Loading branch information
markerikson authored Dec 31, 2021
2 parents fdb8b67 + 838af71 commit a7f1dfc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
31 changes: 23 additions & 8 deletions examples/action-listener/counter/src/services/counter/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,31 @@ async function onUpdateAsync(
}
}

/**
* Subscribes counter listeners and returns a `teardown` function.
* @example
* ```ts
* useEffect(() => {
* const unsubscribe = setupCounterListeners();
* return unsubscribe;
* }, []);
* ```
*/
export function setupCounterListeners(
actionListener: AppActionListenerMiddleware
) {
actionListener.addListener({
actionCreator: counterActions.updateByPeriodically,
listener: onUpdateByPeriodically,
})
const subscriptions = [
actionListener.addListener({
actionCreator: counterActions.updateByPeriodically,
listener: onUpdateByPeriodically,
}),
actionListener.addListener({
actionCreator: counterActions.updateByAsync,
listener: onUpdateAsync,
}),
]

actionListener.addListener({
actionCreator: counterActions.updateByAsync,
listener: onUpdateAsync,
})
return () => {
subscriptions.forEach((unsubscribe) => unsubscribe())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function onChangeColorScheme(
export function setupThemeListeners(
actionListener: AppActionListenerMiddleware
) {
actionListener.addListener({
return actionListener.addListener({
actionCreator: themeActions.changeColorScheme,
listener: onChangeColorScheme,
})
Expand Down
12 changes: 7 additions & 5 deletions examples/action-listener/counter/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { themeSlice } from './services/theme/slice'
import { setupCounterListeners } from './services/counter/listeners'
import { setupThemeListeners } from './services/theme/listeners'

export const actionListener = createActionListenerMiddleware({
const actionListenerMiddleware = createActionListenerMiddleware({
onError: () => console.error,
})

Expand All @@ -19,7 +19,7 @@ const store = configureStore({
[counterSlice.name]: counterSlice.reducer,
[themeSlice.name]: themeSlice.reducer,
},
middleware: (gDM) => gDM().prepend(actionListener),
middleware: (gDM) => gDM().prepend(actionListenerMiddleware),
})

export { store }
Expand All @@ -35,11 +35,13 @@ export type AppActionListenerMiddleware = ActionListenerMiddleware<
AppDispatch
>

actionListener as AppActionListenerMiddleware
// Typed version of `actionListenerMiddleware`
export const appActionListener =
actionListenerMiddleware as AppActionListenerMiddleware

// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector

setupCounterListeners(actionListener as AppActionListenerMiddleware)
setupThemeListeners(actionListener as AppActionListenerMiddleware)
setupCounterListeners(appActionListener)
setupThemeListeners(appActionListener)

0 comments on commit a7f1dfc

Please sign in to comment.