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
2 changes: 2 additions & 0 deletions packages/gatsby/src/bootstrap/remove-stale-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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 (!job) return
gabrieluizramos marked this conversation as resolved.
Show resolved Hide resolved

if (isJobStale(job)) {
actions.push(internalActions.removeStaleJob(job.contentDigest))
} else {
Expand Down
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 @@ -10,6 +10,7 @@ import { pageDataReducer } from "./page-data"
import { themesReducer } from "./themes"
import { webpackCompilationHashReducer } from "./webpack-compilation-hash"
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducer"
import { jobsV2Reducer } from "./jobsv2"

// const backend = process.env.GATSBY_DB_NODES || `redux`
const backend = `redux`
Expand Down Expand Up @@ -67,7 +68,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,11 +1,18 @@
module.exports = (
state = { incomplete: new Map(), complete: new Map() },
action
) => {
import { ActionsUnion, IGatsbyState, IGatsbyJobV2 } 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

if (!job) return state
gabrieluizramos marked this conversation as resolved.
Show resolved Hide resolved

state.incomplete.set(job.contentDigest, {
job,
plugin,
Expand All @@ -16,7 +23,9 @@ module.exports = (

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

if (!job) return state
gabrieluizramos marked this conversation as resolved.
Show resolved Hide resolved
gabrieluizramos marked this conversation as resolved.
Show resolved Hide resolved

state.incomplete.delete(job.contentDigest)

Expand Down
32 changes: 30 additions & 2 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ export interface IGatsbyJobContent {
}

export interface IGatsbyJobV2 {
job: IGatsbyJobContent
plugin: IGatsbyPlugin
job?: IGatsbyJobContent
plugin?: IGatsbyPlugin
traceId?: string
result?: string[]
inputPaths?: string[]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason for these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that were some types that I didn't get right at the beggining, but now things are becoming more clear to me.

I'm removing the optional for the job, plugin, result and inputPaths keys inside this interface now. Since we actually set it inside the reducer we actually need the result and inputPaths in this interface. Am I right?


export interface IGatsbyState {
Expand Down Expand Up @@ -249,6 +251,32 @@ export type ActionsUnion =
| IUpdatePluginsHashAction
| IRemovePageDataAction
| ISetPageDataAction
| ICreateJobV2Action
| IEndJobV2Action
| IRemoveStaleJobV2Action

export interface ICreateJobV2Action {
type: `CREATE_JOB_V2`
payload: {
job: IGatsbyJobV2["job"]
plugin: IGatsbyJobV2["plugin"]
}
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
}

export interface IEndJobV2Action {
type: `END_JOB_V2`
payload: {
jobContentDigest: string
result: string[]
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

export interface ICreatePageDependencyAction {
type: `CREATE_COMPONENT_DEPENDENCY`
Expand Down