/
jobsv2.ts
75 lines (62 loc) · 1.66 KB
/
jobsv2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
ICreateJobV2Action,
IRemoveStaleJobV2Action,
IEndJobV2Action,
IGatsbyState,
IGatsbyIncompleteJobV2,
IGatsbyCompleteJobV2,
IDeleteCacheAction,
} from "../types"
const initialState = (): IGatsbyState["jobsV2"] => {
return {
incomplete: new Map(),
complete: new Map(),
}
}
export const jobsV2Reducer = (
state = initialState(),
action:
| ICreateJobV2Action
| IRemoveStaleJobV2Action
| IEndJobV2Action
| IDeleteCacheAction
): IGatsbyState["jobsV2"] => {
switch (action.type) {
case `DELETE_CACHE`:
return (action as IDeleteCacheAction).cacheIsCorrupt
? initialState()
: state
case `CREATE_JOB_V2`: {
const { job } = action.payload
state.incomplete.set(job.contentDigest, {
job,
} as IGatsbyIncompleteJobV2)
return state
}
case `END_JOB_V2`: {
const { jobContentDigest, result } = action.payload
const { job } = state.incomplete.get(
jobContentDigest
) as IGatsbyIncompleteJobV2
if (!job) {
throw new Error(
`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
}
case `REMOVE_STALE_JOB_V2`: {
const { contentDigest } = action.payload
state.incomplete.delete(contentDigest)
state.complete.delete(contentDigest)
return state
}
}
return state
}