Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Add RTK listenerMiddleware #2828

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 28 additions & 13 deletions packages/mobile/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@react-navigation/native": "6.0.8",
"@react-navigation/native-stack": "6.6.1",
"@react-navigation/stack": "6.2.0",
"@reduxjs/toolkit": "1.6.1",
"@reduxjs/toolkit": "1.9.2",
"@sayem314/react-native-keep-awake": "1.1.0",
"@sentry/react-native": "3.2.8",
"@snapchat/snap-kit-react-native": "0.4.0",
Expand Down
13 changes: 13 additions & 0 deletions packages/mobile/src/store/listenerMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { TypedStartListening } from '@reduxjs/toolkit'
import { createListenerMiddleware } from '@reduxjs/toolkit'

import type { AppState } from './store'
import { addListeners as addThemeListeners } from './theme/listeners'

export const listenerMiddleware = createListenerMiddleware()

export type AppStartListening = TypedStartListening<AppState>

const startListening = listenerMiddleware.startListening as AppStartListening

addThemeListeners(startListening)
2 changes: 0 additions & 2 deletions packages/mobile/src/store/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import rateCtaSagas from './rate-cta/sagas'
import settingsSagas from './settings/sagas'
import signOutSagas from './sign-out/sagas'
import signUpSagas from './sign-up/sagas'
import themeSagas from './theme/sagas'
import walletsSagas from './wallet-connect/sagas'

export default function* rootSaga() {
Expand Down Expand Up @@ -148,7 +147,6 @@ export default function* rootSaga() {
...deletePlaylistConfirmationModalSagas(),
...shareModalSagas(),
...vipDiscordModalSagas(),
...themeSagas(),
...tokenDashboardSagas(),
...mobileUiSagas(),
...uploadSagas(),
Expand Down
3 changes: 2 additions & 1 deletion packages/mobile/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { DrawersState } from './drawers/slice'
import drawers from './drawers/slice'
import type { KeyboardState } from './keyboard/slice'
import keyboard from './keyboard/slice'
import { listenerMiddleware } from './listenerMiddleware'
import mobileUi from './mobileUi/slice'
import type { MobileUiState } from './mobileUi/slice'
import type { OAuthState } from './oauth/reducer'
Expand Down Expand Up @@ -137,7 +138,7 @@ const sagaMiddleware = createSagaMiddleware({
onError: onSagaError
})

const middlewares = [sagaMiddleware]
const middlewares = [sagaMiddleware, listenerMiddleware.middleware]

if (__DEV__) {
const createDebugger = require('redux-flipper').default
Expand Down
31 changes: 31 additions & 0 deletions packages/mobile/src/store/theme/listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { themeSelectors, themeActions } from '@audius/common'

import { THEME_STORAGE_KEY } from 'app/constants/storage-keys'
import { localStorage } from 'app/services/local-storage'
import { updateStatusBarTheme } from 'app/utils/theme'

import type { AppStartListening } from '../listenerMiddleware'
const { setTheme, setSystemAppearance } = themeActions
const { getSystemAppearance, getTheme } = themeSelectors

export const addListeners = (startListening: AppStartListening) => {
startListening({
actionCreator: setTheme,
effect: async function setThemeEffect(action, listenerApi) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, is there a reason this is a named fn instead of an async (action, listenAPI) => ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes the callstack nicer when debugging!

const systemAppearance = getSystemAppearance(listenerApi.getState())
const { theme } = action.payload
updateStatusBarTheme(theme, systemAppearance)

await localStorage.setItem(THEME_STORAGE_KEY, theme)
}
})

startListening({
actionCreator: setSystemAppearance,
effect: async function setSystemAppearanceEffect(action, listenerApi) {
const { systemAppearance } = action.payload
const theme = getTheme(listenerApi.getState())
updateStatusBarTheme(theme, systemAppearance)
}
})
}
Loading