Skip to content

Commit

Permalink
chore(ts): convert jobsv2 reducer to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieluizramos committed May 6, 2020
1 parent be3b54a commit 106c0f3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
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

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 @@ -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`
Expand Down Expand Up @@ -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,
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

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

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 @@ -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 {
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 106c0f3

Please sign in to comment.