From 106c0f305846c287b42629c819eb781ecb71a278 Mon Sep 17 00:00:00 2001 From: gabrieluizramos Date: Sat, 2 May 2020 16:55:21 -0300 Subject: [PATCH] chore(ts): convert jobsv2 reducer to TS --- .../gatsby/src/bootstrap/remove-stale-jobs.ts | 2 ++ packages/gatsby/src/redux/__tests__/jobsv2.js | 2 +- packages/gatsby/src/redux/reducers/index.js | 3 +- .../redux/reducers/{jobsv2.js => jobsv2.ts} | 19 ++++++++--- packages/gatsby/src/redux/types.ts | 32 +++++++++++++++++-- 5 files changed, 49 insertions(+), 9 deletions(-) rename packages/gatsby/src/redux/reducers/{jobsv2.js => jobsv2.ts} (66%) diff --git a/packages/gatsby/src/bootstrap/remove-stale-jobs.ts b/packages/gatsby/src/bootstrap/remove-stale-jobs.ts index c4473ed25c239..d53abb7fa6038 100644 --- a/packages/gatsby/src/bootstrap/remove-stale-jobs.ts +++ b/packages/gatsby/src/bootstrap/remove-stale-jobs.ts @@ -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 + if (isJobStale(job)) { actions.push(internalActions.removeStaleJob(job.contentDigest)) } else { diff --git a/packages/gatsby/src/redux/__tests__/jobsv2.js b/packages/gatsby/src/redux/__tests__/jobsv2.js index 3bc3398b689c5..662e530d11da8 100644 --- a/packages/gatsby/src/redux/__tests__/jobsv2.js +++ b/packages/gatsby/src/redux/__tests__/jobsv2.js @@ -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 = { diff --git a/packages/gatsby/src/redux/reducers/index.js b/packages/gatsby/src/redux/reducers/index.js index 1bc90fd0b9bbd..b7a2edf48312a 100644 --- a/packages/gatsby/src/redux/reducers/index.js +++ b/packages/gatsby/src/redux/reducers/index.js @@ -6,6 +6,7 @@ import { statusReducer } from "./status" import { webpackReducer } from "./webpack" 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` @@ -63,7 +64,7 @@ module.exports = { components: require(`./components`), staticQueryComponents: staticQueryComponentsReducer, jobs: require(`./jobs`), - jobsV2: require(`./jobsv2`), + jobsV2: jobsV2Reducer, webpack: webpackReducer, webpackCompilationHash: webpackCompilationHashReducer, redirects: redirectsReducer, diff --git a/packages/gatsby/src/redux/reducers/jobsv2.js b/packages/gatsby/src/redux/reducers/jobsv2.ts similarity index 66% rename from packages/gatsby/src/redux/reducers/jobsv2.js rename to packages/gatsby/src/redux/reducers/jobsv2.ts index ee5319ee6465c..1995c46bcfc08 100644 --- a/packages/gatsby/src/redux/reducers/jobsv2.js +++ b/packages/gatsby/src/redux/reducers/jobsv2.ts @@ -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 + state.incomplete.set(job.contentDigest, { job, plugin, @@ -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 state.incomplete.delete(job.contentDigest) diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index 5b0c643d6d8d9..2023e0281776b 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -97,9 +97,11 @@ export interface IGatsbyJobContent { } export interface IGatsbyJobV2 { - job: IGatsbyJobContent - plugin: IGatsbyPlugin + job?: IGatsbyJobContent + plugin?: IGatsbyPlugin traceId?: string + result?: string[] + inputPaths?: string[] } export interface IGatsbyState { @@ -240,6 +242,32 @@ export type ActionsUnion = | ISetWebpackCompilationHashAction | ISetWebpackConfigAction | IUpdatePluginsHashAction + | ICreateJobV2Action + | IEndJobV2Action + | IRemoveStaleJobV2Action + +export interface ICreateJobV2Action { + type: `CREATE_JOB_V2` + payload: { + job: IGatsbyJobV2["job"] + plugin: IGatsbyJobV2["plugin"] + } +} + +export interface IEndJobV2Action { + type: `END_JOB_V2` + payload: { + jobContentDigest: string + result: string[] + } +} + +export interface IRemoveStaleJobV2Action { + type: `REMOVE_STALE_JOB_V2` + payload: { + contentDigest: string + } +} export interface ICreatePageDependencyAction { type: `CREATE_COMPONENT_DEPENDENCY`