-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
actions.ts
282 lines (250 loc) · 7.71 KB
/
actions.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import {
assign,
AnyEventObject,
ActionFunction,
spawn,
DoneEventObject,
} from "xstate"
import { IBuildContext } from "../../services"
import { actions } from "../../redux/actions"
import { listenForMutations } from "../../services/listen-for-mutations"
import { DataLayerResult } from "../data-layer"
import { saveState } from "../../redux/save-state"
import reporter from "gatsby-cli/lib/reporter"
import { store } from "../../redux"
import { ProgramStatus } from "../../redux/types"
import { createWebpackWatcher } from "../../services/listen-to-webpack"
import { callRealApi } from "../../utils/call-deferred-api"
import {
writeGraphQLFragments,
writeGraphQLSchema,
} from "../../utils/graphql-typegen/file-writes"
import { writeTypeScriptTypes } from "../../utils/graphql-typegen/ts-codegen"
/**
* Handler for when we're inside handlers that should be able to mutate nodes
* Instead of queueing, we call it right away
*/
export const callApi: ActionFunction<IBuildContext, AnyEventObject> = (
{ store },
event
) => callRealApi(event.payload, store)
/**
* Event handler used in all states where we're not ready to process node
* mutations. Instead we add it to a batch to process when we're next idle
*/
export const addNodeMutation = assign<IBuildContext, AnyEventObject>({
nodeMutationBatch: ({ nodeMutationBatch = [] }, { payload }) => {
// It's not pretty, but it's much quicker than concat
nodeMutationBatch.push(payload)
return nodeMutationBatch
},
})
export const assignStoreAndWorkerPool = assign<IBuildContext, DoneEventObject>(
(_context, event) => {
const { store, workerPool } = event.data
return {
store,
workerPool,
}
}
)
const setQueryRunningFinished = async (): Promise<void> => {
store.dispatch(
actions.setProgramStatus(ProgramStatus.BOOTSTRAP_QUERY_RUNNING_FINISHED)
)
}
export const markQueryFilesDirty = assign<IBuildContext>({
queryFilesDirty: true,
})
export const markSourceFilesDirty = assign<IBuildContext, AnyEventObject>(
(context, event) => {
const prev = context.changedSourceFiles ?? new Set()
return {
sourceFilesDirty: true,
changedSourceFiles: prev.add(event.payload ?? event.file),
}
}
)
export const markSourceFilesClean = assign<IBuildContext>({
sourceFilesDirty: false,
changedSourceFiles: () => new Set(),
})
export const setRecompiledFiles = assign<IBuildContext, AnyEventObject>(
context => {
return {
recompiledFiles: context.changedSourceFiles,
}
}
)
export const markNodesDirty = assign<IBuildContext>({
nodesMutatedDuringQueryRun: true,
})
export const markNodesClean = assign<IBuildContext>({
nodesMutatedDuringQueryRun: false,
})
export const incrementRecompileCount = assign<IBuildContext>({
nodesMutatedDuringQueryRunRecompileCount: ({
nodesMutatedDuringQueryRunRecompileCount: count = 0,
}) => {
reporter.verbose(
`Re-running queries because nodes mutated during query run. Count: ${
count + 1
}`
)
return count + 1
},
})
export const resetRecompileCount = assign<IBuildContext>({
nodesMutatedDuringQueryRunRecompileCount: 0,
nodesMutatedDuringQueryRun: false,
})
export const assignServiceResult = assign<IBuildContext, DoneEventObject>(
(_context, { data }): DataLayerResult => data
)
/**
* This spawns the service that listens to the `emitter` for various mutation events
*/
export const spawnMutationListener = assign<IBuildContext>({
// @ts-ignore - TODO: Fixing this seems more involved: https://xstate.js.org/docs/guides/typescript.html#troubleshooting & https://github.com/statelyai/xstate/issues/2664
mutationListener: () => spawn(listenForMutations, `listen-for-mutations`),
})
export const assignServers = assign<IBuildContext, AnyEventObject>(
(_context, { data }) => {
return {
...data,
}
}
)
export const spawnWebpackListener = assign<IBuildContext, AnyEventObject>({
// @ts-ignore - TODO: Fixing this seems more involved: https://xstate.js.org/docs/guides/typescript.html#troubleshooting & https://github.com/statelyai/xstate/issues/2664
webpackListener: ({ compiler }) => {
if (!compiler) {
return undefined
}
return spawn(createWebpackWatcher(compiler))
},
})
export const assignWebhookBody = assign<IBuildContext, AnyEventObject>({
webhookBody: (_context, { payload }) => payload?.webhookBody,
webhookSourcePluginName: (_context, { payload }) => payload?.pluginName,
})
export const clearWebhookBody = assign<IBuildContext, AnyEventObject>({
webhookBody: undefined,
webhookSourcePluginName: undefined,
})
export const finishParentSpan = ({ parentSpan }: IBuildContext): void =>
parentSpan?.finish()
export const saveDbState = (): Promise<void> => saveState()
export const logError: ActionFunction<IBuildContext, AnyEventObject> = (
_context,
event
) => {
reporter.error(event.data)
}
export const panic: ActionFunction<IBuildContext, AnyEventObject> = (
_context,
event
) => {
reporter.panic(event.data)
}
export const panicBecauseOfInfiniteLoop: ActionFunction<
IBuildContext,
AnyEventObject
> = () => {
reporter.panic(
reporter.stripIndent(`
Panicking because nodes appear to be being changed every time we run queries. This would cause the site to recompile infinitely.
Check custom resolvers to see if they are unconditionally creating or mutating nodes on every query.
This may happen if they create nodes with a field that is different every time, such as a timestamp or unique id.`)
)
}
export const trackRequestedQueryRun = assign<IBuildContext, AnyEventObject>({
pendingQueryRuns: (context, { payload }) => {
const pendingQueryRuns = context.pendingQueryRuns || new Set<string>()
if (payload?.pagePath) {
pendingQueryRuns.add(payload.pagePath)
}
return pendingQueryRuns
},
})
export const clearPendingQueryRuns = assign<IBuildContext>(() => {
return {
pendingQueryRuns: new Set<string>(),
}
})
export const schemaTypegen: ActionFunction<
IBuildContext,
AnyEventObject
> = async (context, event) => {
const schema = event.payload.payload
const directory = context.program.directory
context.reporter!.verbose(`Re-Generating schema.graphql`)
try {
await writeGraphQLSchema(directory, schema)
} catch (err) {
context.reporter!.panicOnBuild({
id: `12100`,
context: {
sourceMessage: err,
},
})
}
}
export const definitionsTypegen: ActionFunction<
IBuildContext,
AnyEventObject
> = async (context, event) => {
const definitions = event.payload.payload
const { schema, config } = context.store!.getState()
const directory = context.program.directory
const graphqlTypegenOptions = config.graphqlTypegen
if (!graphqlTypegenOptions) {
throw new Error(`graphqlTypegen option is falsy. This should never happen.`)
}
context.reporter!.verbose(`Re-Generating fragments.graphql & TS Types`)
try {
await writeGraphQLFragments(directory, definitions)
await writeTypeScriptTypes(
directory,
schema,
definitions,
graphqlTypegenOptions
)
} catch (err) {
context.reporter!.panicOnBuild({
id: `12100`,
context: {
sourceMessage: err,
},
})
}
}
export const buildActions = {
callApi,
markNodesDirty,
addNodeMutation,
spawnMutationListener,
assignStoreAndWorkerPool,
assignServiceResult,
assignServers,
markQueryFilesDirty,
assignWebhookBody,
clearWebhookBody,
finishParentSpan,
spawnWebpackListener,
markSourceFilesDirty,
markSourceFilesClean,
setRecompiledFiles,
markNodesClean,
incrementRecompileCount,
resetRecompileCount,
panicBecauseOfInfiniteLoop,
saveDbState,
setQueryRunningFinished,
panic,
logError,
trackRequestedQueryRun,
clearPendingQueryRuns,
schemaTypegen,
definitionsTypegen,
}