-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.ts
132 lines (129 loc) · 3.65 KB
/
index.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
import { MachineConfig, createMachine, assign } from "xstate"
import { IQueryRunningContext } from "./types"
import { queryRunningServices } from "./services"
import { queryActions } from "./actions"
/**
* This is a child state machine, spawned to perform the query running
*/
const PAGE_QUERY_ENQUEUING_TIMEOUT = 50
export const queryStates: MachineConfig<IQueryRunningContext, any, any> = {
predictableActionArguments: true,
initial: `extractingQueries`,
id: `queryRunningMachine`,
on: {
SOURCE_FILE_CHANGED: {
actions: `markSourceFilesDirty`,
},
QUERY_RUN_REQUESTED: {
actions: `trackRequestedQueryRun`,
},
},
states: {
extractingQueries: {
id: `extracting-queries`,
invoke: {
id: `extracting-queries`,
src: `extractQueries`,
onDone: {
target: `waitingPendingQueries`,
},
},
},
// This state exists solely because "extractQueries" finishes too early.
// It finishes before extracted queries are enqueued for execution.
// As a result calculateDirtyQueries doesn't see them and they are not executed.
//
// This happens because extracted queries are enqueued for execution with setTimeout(x, 0)
// wrapper in actions of redux/machines/page-component which fires after "extractQueries" finishes.
//
// see https://github.com/gatsbyjs/gatsby/issues/26580
//
// FIXME: this has to be fixed properly by not leaving "extractingQueries" state
// until all extracted queries are enqueued for execution (but requires a refactor)
waitingPendingQueries: {
id: `waiting-pending-queries`,
after: {
[PAGE_QUERY_ENQUEUING_TIMEOUT]: {
target: `writingRequires`,
actions: `markSourceFilesClean`,
},
},
},
writingRequires: {
invoke: {
src: `writeOutRequires`,
id: `writing-requires`,
onDone: {
target: `calculatingDirtyQueries`,
},
},
},
calculatingDirtyQueries: {
entry: assign<IQueryRunningContext>(({ pendingQueryRuns }) => {
return {
pendingQueryRuns: new Set(),
currentlyHandledPendingQueryRuns: pendingQueryRuns,
}
}),
invoke: {
id: `calculating-dirty-queries`,
src: `calculateDirtyQueries`,
onDone: {
target: `runningStaticQueries`,
actions: [
`assignDirtyQueries`,
`clearCurrentlyHandledPendingQueryRuns`,
],
},
},
},
runningStaticQueries: {
invoke: {
src: `runStaticQueries`,
id: `running-static-queries`,
onDone: {
target: `runningPageQueries`,
},
},
},
runningPageQueries: {
invoke: {
src: `runPageQueries`,
id: `running-page-queries`,
onDone: {
target: `waitingForJobs`,
actions: `flushPageData`,
},
},
},
// This waits for the jobs API to finish
waitingForJobs: {
// If files are dirty go and extract again
always: [
{
cond: (ctx): boolean => !!ctx.filesDirty,
target: `extractingQueries`,
},
{
cond: ({ pendingQueryRuns }): boolean =>
!!pendingQueryRuns && pendingQueryRuns.size > 0,
target: `calculatingDirtyQueries`,
},
],
invoke: {
src: `waitUntilAllJobsComplete`,
id: `waiting-for-jobs`,
onDone: {
target: `done`,
},
},
},
done: {
type: `final`,
},
},
}
export const queryRunningMachine = createMachine(queryStates, {
actions: queryActions,
services: queryRunningServices,
})