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(gatsby): Convert jobsV2 reducer to TS #23708

Merged
merged 9 commits into from
May 11, 2020
19 changes: 11 additions & 8 deletions packages/gatsby/src/bootstrap/remove-stale-jobs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
IGatsbyState,
IRemoveStaleJobAction,
IGatsbyJobV2,
IGatsbyIncompleteJobV2,
IGatsbyCompleteJobV2,
} from "../redux/types"

import { isJobStale } from "../utils/jobs-manager"
Expand All @@ -14,7 +15,7 @@ export const removeStaleJobs = (

// If any of our finished jobs are stale we remove them to keep our cache small
state.jobsV2.complete.forEach(
(job: IGatsbyJobV2, contentDigest: string): void => {
(job: IGatsbyCompleteJobV2, contentDigest: string): void => {
if (isJobStale(job)) {
actions.push(internalActions.removeStaleJob(contentDigest))
}
Expand All @@ -23,13 +24,15 @@ export const removeStaleJobs = (

// If any of our pending jobs do not have an existing inputPath or the inputPath changed
// we remove it from the queue as they would fail anyway
state.jobsV2.incomplete.forEach(({ job, plugin }: IGatsbyJobV2): void => {
if (isJobStale(job)) {
actions.push(internalActions.removeStaleJob(job.contentDigest))
} else {
actions.push(publicActions.createJobV2(job, plugin))
state.jobsV2.incomplete.forEach(
({ job, plugin }: IGatsbyIncompleteJobV2): void => {
if (isJobStale(job)) {
actions.push(internalActions.removeStaleJob(job.contentDigest))
} else {
actions.push(publicActions.createJobV2(job, plugin))
}
}
})
)

return actions
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/__tests__/jobsv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jest.spyOn(jobsManager, `enqueueJob`)
jest.spyOn(jobsManager, `removeInProgressJob`)
jest.mock(`uuid/v4`, () => () => `1234`)

const jobsReducer = require(`../reducers/jobsv2`)
import { jobsV2Reducer as jobsReducer } from "../reducers/jobsv2"

describe(`Job v2 actions/reducer`, () => {
const plugin = {
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/redux/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { themesReducer } from "./themes"
import { webpackCompilationHashReducer } from "./webpack-compilation-hash"
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducer"
import { lastAction } from "./last-action"
import { jobsV2Reducer } from "./jobsv2"

/**
* @property exports.nodesTouched Set<string>
Expand All @@ -31,7 +32,7 @@ module.exports = {
components: require(`./components`),
staticQueryComponents: staticQueryComponentsReducer,
jobs: require(`./jobs`),
jobsV2: require(`./jobsv2`),
jobsV2: jobsV2Reducer,
webpack: webpackReducer,
webpackCompilationHash: webpackCompilationHashReducer,
redirects: redirectsReducer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
module.exports = (
state = { incomplete: new Map(), complete: new Map() },
action
) => {
import {
ActionsUnion,
IGatsbyState,
IGatsbyIncompleteJobV2,
IGatsbyCompleteJobV2,
} from "../types"

export const jobsV2Reducer = (
state: IGatsbyState["jobsV2"] = {
incomplete: new Map(),
complete: new Map(),
},
action: ActionsUnion
): IGatsbyState["jobsV2"] => {
switch (action.type) {
case `CREATE_JOB_V2`: {
const { job, plugin } = action.payload

state.incomplete.set(job.contentDigest, {
job,
plugin,
})
} as IGatsbyIncompleteJobV2)

return state
}

case `END_JOB_V2`: {
const { jobContentDigest, result } = action.payload
const { job } = state.incomplete.get(jobContentDigest)
const { job } = state.incomplete.get(
jobContentDigest
) as IGatsbyIncompleteJobV2

if (!job) {
throw new Error(
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
`If you encounter this error, it's probably a Gatsby internal bug. Please open an issue reporting us this.`
)
}

state.incomplete.delete(job.contentDigest)

// inputPaths is used to make sure the job is not stale
state.complete.set(job.contentDigest, {
result,
inputPaths: job.inputPaths,
})
} as IGatsbyCompleteJobV2)

return state
}
Expand Down
44 changes: 35 additions & 9 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GraphQLFieldExtensionDefinition } from "../schema/extensions"
import { DocumentNode, GraphQLSchema } from "graphql"
import { SchemaComposer } from "graphql-compose"
import { IGatsbyCLIState } from "gatsby-cli/src/reporter/redux/types"
import { InternalJobInterface, JobResultInterface } from "../utils/jobs-manager"

type SystemPath = string
type Identifier = string
Expand Down Expand Up @@ -95,15 +96,14 @@ export interface IGatsbyStaticQueryComponents {

type GatsbyNodes = Map<string, IGatsbyNode>

export interface IGatsbyJobContent {
inputPaths: string[]
contentDigest: string
export interface IGatsbyIncompleteJobV2 {
job: InternalJobInterface
plugin: IGatsbyPlugin
}

export interface IGatsbyJobV2 {
job: IGatsbyJobContent
plugin: IGatsbyPlugin
traceId?: string
export interface IGatsbyCompleteJobV2 {
result: JobResultInterface
inputPaths: InternalJobInterface["inputPaths"]
}

export interface IGatsbyState {
Expand Down Expand Up @@ -171,8 +171,8 @@ export interface IGatsbyState {
done: any[] // TODO
}
jobsV2: {
incomplete: Map<Identifier, IGatsbyJobV2>
complete: Map<Identifier, IGatsbyJobV2>
incomplete: Map<Identifier, IGatsbyIncompleteJobV2>
complete: Map<Identifier, IGatsbyCompleteJobV2>
}
webpack: any // TODO This should be the output from ./utils/webpack.config.js
webpackCompilationHash: string
Expand Down Expand Up @@ -254,6 +254,32 @@ export type ActionsUnion =
| IUpdatePluginsHashAction
| IRemovePageDataAction
| ISetPageDataAction
| ICreateJobV2Action
| IEndJobV2Action
| IRemoveStaleJobV2Action

export interface ICreateJobV2Action {
type: `CREATE_JOB_V2`
payload: {
job: IGatsbyIncompleteJobV2["job"]
plugin: IGatsbyIncompleteJobV2["plugin"]
}
}

export interface IEndJobV2Action {
type: `END_JOB_V2`
payload: {
jobContentDigest: string
result: JobResultInterface
}
}

export interface IRemoveStaleJobV2Action {
type: `REMOVE_STALE_JOB_V2`
payload: {
contentDigest: string
}
}

export interface ICreatePageDependencyAction {
type: `CREATE_COMPONENT_DEPENDENCY`
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/jobs-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const createFileHash = path => hasha.fromFileSync(path, { algorithm: `sha1` })
* @property {{path: string, contentDigest: string}[]} inputPaths
* @property {{name: string, version: string, resolve: string, isLocal: boolean}} plugin
*
* @typedef {Record<string, unknown>} JobResultInterface
*
* I know this sucks but this is the only way to do it properly in jsdoc..
* @typedef {BaseJobInterface & JobInputInterface} JobInput
* @typedef {BaseJobInterface & InternalJobInterface} InternalJob
Expand Down