Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/save-read-state-slices' into pqr…
Browse files Browse the repository at this point in the history
…-worker-inf-metadata
  • Loading branch information
pieh committed Jun 10, 2021
2 parents 342b306 + af20d74 commit 871b4c1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/gatsby/src/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { mett } from "../utils/mett"
import thunk, { ThunkMiddleware } from "redux-thunk"
import * as reducers from "./reducers"
import { writeToCache, readFromCache } from "./persist"
import { IGatsbyState, ActionsUnion, GatsbyStateSlices } from "./types"
import { IGatsbyState, ActionsUnion, GatsbyStateKeys } from "./types"

// Create event emitter for actions
export const emitter = mett()
Expand Down Expand Up @@ -126,15 +126,15 @@ export const saveState = (): void => {
})
}

export const saveStateForWorkers = (slices: Array<GatsbyStateSlices>): void => {
export const saveStateForWorkers = (slices: Array<GatsbyStateKeys>): void => {
const state = store.getState()
const contents = _.pick(state, slices)

return writeToCache(contents, slices)
}

export const loadStateInWorker = (
slices: Array<GatsbyStateSlices>
slices: Array<GatsbyStateKeys>
): DeepPartial<IGatsbyState> => {
try {
return readFromCache(slices) as DeepPartial<IGatsbyState>
Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby/src/redux/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ICachedReduxState,
IGatsbyNode,
IGatsbyPage,
GatsbyStateSlices,
GatsbyStateKeys,
} from "./types"
import { sync as globSync } from "glob"
import { createContentDigest } from "gatsby-core-utils"
Expand All @@ -38,7 +38,7 @@ function reduxWorkerSlicesPrefix(dir: string): string {
}

export function readFromCache(
slices?: Array<GatsbyStateSlices>
slices?: Array<GatsbyStateKeys>
): DeepPartial<ICachedReduxState> {
// The cache is stored in two steps; the nodes and pages in chunks and the rest
// First we revive the rest, then we inject the nodes and pages into that obj (if any)
Expand Down Expand Up @@ -123,7 +123,7 @@ export function guessSafeChunkSize(
function prepareCacheFolder(
targetDir: string,
contents: DeepPartial<ICachedReduxState>,
slices?: Array<GatsbyStateSlices>
slices?: Array<GatsbyStateKeys>
): void {
if (slices) {
writeFileSync(
Expand Down Expand Up @@ -215,7 +215,7 @@ function safelyRenameToBak(reduxCacheFolder: string): string {

export function writeToCache(
contents: DeepPartial<ICachedReduxState>,
slices?: Array<GatsbyStateSlices>
slices?: Array<GatsbyStateKeys>
): void {
// Note: this should be a transactional operation. So work in a tmp dir and
// make sure the cache cannot be left in a corruptable state due to errors.
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export interface IGatsbyState {
}
}

export type GatsbyStateSlices = keyof IGatsbyState
export type GatsbyStateKeys = keyof IGatsbyState

export interface ICachedReduxState {
nodes?: IGatsbyState["nodes"]
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby/src/utils/worker/__tests__/share-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
saveStateForWorkers,
loadStateInWorker,
} from "../../../redux"
import { GatsbyStateSlices } from "../../../redux/types"
import { GatsbyStateKeys } from "../../../redux/types"

let worker: GatsbyTestWorkerPool | undefined

Expand Down Expand Up @@ -75,8 +75,8 @@ describe(`worker (share-state)`, () => {
},
})

const slicesOne: Array<GatsbyStateSlices> = [`components`]
const slicesTwo: Array<GatsbyStateSlices> = [
const slicesOne: Array<GatsbyStateKeys> = [`components`]
const slicesTwo: Array<GatsbyStateKeys> = [
`components`,
`staticQueryComponents`,
]
Expand Down Expand Up @@ -139,7 +139,7 @@ describe(`worker (share-state)`, () => {
},
})

const slices: Array<GatsbyStateSlices> = []
const slices: Array<GatsbyStateKeys> = []

saveStateForWorkers(slices)
const result = loadStateInWorker(slices)
Expand All @@ -156,7 +156,7 @@ describe(`worker (share-state)`, () => {
},
})

const slices: Array<GatsbyStateSlices> = [`staticQueryComponents`]
const slices: Array<GatsbyStateKeys> = [`staticQueryComponents`]

saveStateForWorkers(slices)
const result = loadStateInWorker(slices)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require(`@babel/register`)({
extensions: [`.js`, `.ts`],
configFile: require.resolve(`../../../../../babel.config.js`),
ignore: [/node_modules/],
})

module.exports = require(`./child-for-tests`)
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/worker/child/state.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Calls loadStateInWorker(slices) and mutates the redux store with the results
import { GatsbyStateSlices } from "../../../redux/types"
import { GatsbyStateKeys } from "../../../redux/types"
import { loadStateInWorker, store } from "../../../redux"

export function setState(slices: Array<GatsbyStateSlices>): void {
export function setState(slices: Array<GatsbyStateKeys>): void {
const res = loadStateInWorker(slices)

Object.entries(res).forEach(([key, val]) => {
Expand Down

0 comments on commit 871b4c1

Please sign in to comment.