Skip to content

Commit

Permalink
chore: allow thunks to be dispatched (#32282)
Browse files Browse the repository at this point in the history
* chore: allow thunks to be dispatched

* remove comment about using of inferred return type
  • Loading branch information
pieh authored Jul 7, 2021
1 parent 906170f commit cd4571c
Showing 1 changed file with 6 additions and 8 deletions.
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 =>
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

0 comments on commit cd4571c

Please sign in to comment.