Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow thunks to be dispatched #32282

Merged
merged 2 commits into from
Jul 7, 2021
Merged
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
14 changes: 6 additions & 8 deletions packages/gatsby/src/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import _ from "lodash"
import telemetry from "gatsby-telemetry"

import { mett } from "../utils/mett"
import thunk, { ThunkMiddleware, ThunkAction } from "redux-thunk"
import thunk, { ThunkMiddleware, ThunkAction, ThunkDispatch } from "redux-thunk"
import * as reducers from "./reducers"
import { writeToCache, readFromCache } from "./persist"
import { IGatsbyState, ActionsUnion, GatsbyStateKeys } from "./types"
Expand Down Expand Up @@ -76,19 +76,17 @@ const multi: Middleware<IMultiDispatch> = ({ dispatch }) => next => (
): ActionsUnion | Array<ActionsUnion> =>
Array.isArray(action) ? action.filter(Boolean).map(dispatch) : next(action)

// We're using the inferred type here because manually typing it would be very complicated
// and error-prone. Instead we'll make use of the createStore return value, and export that type.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const configureStore = (
initialState: IGatsbyState
): Store<IGatsbyState> =>
export type GatsbyReduxStore = Store<IGatsbyState> & {
dispatch: ThunkDispatch<IGatsbyState, any, ActionsUnion> & IMultiDispatch
}

export const configureStore = (initialState: IGatsbyState): GatsbyReduxStore =>
pieh marked this conversation as resolved.
Show resolved Hide resolved
createStore(
combineReducers<IGatsbyState>({ ...reducers }),
initialState,
applyMiddleware(thunk as ThunkMiddleware<IGatsbyState, ActionsUnion>, multi)
)

export type GatsbyReduxStore = ReturnType<typeof configureStore>
export const store: GatsbyReduxStore = configureStore(
process.env.GATSBY_WORKER_POOL_WORKER ? ({} as IGatsbyState) : readState()
)
Expand Down