-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
page-query-runner.js
209 lines (174 loc) · 5.5 KB
/
page-query-runner.js
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
// @flow
import type { QueryJob } from "../query-runner"
/**
* Jobs of this module
* - Ensure on bootstrap that all invalid page queries are run and report
* when this is done
* - Watch for when a page's query is invalidated and re-run it.
*/
const _ = require(`lodash`)
const queue = require(`./query-queue`)
const { store, emitter } = require(`../../redux`)
let queuedDirtyActions = []
let active = false
let running = false
const runQueriesForPathnamesQueue = new Set()
exports.queueQueryForPathname = pathname => {
runQueriesForPathnamesQueue.add(pathname)
}
// Do initial run of graphql queries during bootstrap.
// Afterwards we listen "API_RUNNING_QUEUE_EMPTY" and check
// for dirty nodes before running queries.
exports.runInitialQueries = async () => {
await runQueries()
active = true
return
}
const runQueries = async () => {
// Find paths dependent on dirty nodes
queuedDirtyActions = _.uniq(queuedDirtyActions, a => a.payload.id)
const dirtyIds = findDirtyIds(queuedDirtyActions)
queuedDirtyActions = []
// Find ids without data dependencies (i.e. no queries have been run for
// them before) and run them.
const cleanIds = findIdsWithoutDataDependencies()
// Construct paths for all queries to run
const pathnamesToRun = _.uniq([
...runQueriesForPathnamesQueue,
...dirtyIds,
...cleanIds,
])
runQueriesForPathnamesQueue.clear()
// Run these paths
await runQueriesForPathnames(pathnamesToRun)
return
}
emitter.on(`CREATE_NODE`, action => {
queuedDirtyActions.push(action)
})
emitter.on(`DELETE_NODE`, action => {
queuedDirtyActions.push({ payload: action.payload })
})
emitter.on(`CREATE_PAGE`, action => {
if (action.contextModified) {
exports.queueQueryForPathname(action.payload.path)
}
})
const runQueuedActions = async () => {
if (active && !running) {
try {
running = true
await runQueries()
} finally {
running = false
if (queuedDirtyActions.length > 0) {
runQueuedActions()
}
}
}
}
exports.runQueuedActions = runQueuedActions
// Wait until all plugins have finished running (e.g. various
// transformer plugins) before running queries so we don't
// query things in a 1/2 finished state.
emitter.on(`API_RUNNING_QUEUE_EMPTY`, runQueuedActions)
let seenIdsWithoutDataDependencies = []
// Remove pages from seenIdsWithoutDataDependencies when they're deleted
// so their query will be run again if they're created again.
emitter.on(`DELETE_PAGE`, action => {
seenIdsWithoutDataDependencies = seenIdsWithoutDataDependencies.filter(
p => p !== action.payload.path
)
})
const findIdsWithoutDataDependencies = () => {
const state = store.getState()
const allTrackedIds = _.uniq(
_.flatten(
_.concat(
_.values(state.componentDataDependencies.nodes),
_.values(state.componentDataDependencies.connections)
)
)
)
// Get list of paths not already tracked and run the queries for these
// paths.
const notTrackedIds = _.difference(
[
...Array.from(state.pages.values(), p => p.path),
...[...state.staticQueryComponents.values()].map(c => c.jsonName),
],
[...allTrackedIds, ...seenIdsWithoutDataDependencies]
)
// Add new IDs to our seen array so we don't keep trying to run queries for them.
// Pages without queries can't be tracked.
seenIdsWithoutDataDependencies = _.uniq([
...notTrackedIds,
...seenIdsWithoutDataDependencies,
])
return notTrackedIds
}
const runQueriesForPathnames = pathnames => {
const staticQueries = pathnames.filter(p => p.slice(0, 4) === `sq--`)
const pageQueries = pathnames.filter(p => p.slice(0, 4) !== `sq--`)
const state = store.getState()
staticQueries.forEach(id => {
const staticQueryComponent = store.getState().staticQueryComponents.get(id)
const queryJob: QueryJob = {
id: staticQueryComponent.hash,
hash: staticQueryComponent.hash,
jsonName: staticQueryComponent.jsonName,
query: staticQueryComponent.query,
componentPath: staticQueryComponent.componentPath,
context: { path: staticQueryComponent.jsonName },
}
queue.push(queryJob)
})
const pages = state.pages
let didNotQueueItems = true
pageQueries.forEach(id => {
const page = pages.get(id)
if (page) {
didNotQueueItems = false
queue.push(
({
id: page.path,
jsonName: page.jsonName,
query: store.getState().components.get(page.componentPath).query,
isPage: true,
componentPath: page.componentPath,
context: {
...page,
...page.context,
},
}: QueryJob)
)
}
})
if (didNotQueueItems || !pathnames || pathnames.length === 0) {
return Promise.resolve()
}
return new Promise(resolve => {
const onDrain = () => {
queue.removeListener(`drain`, onDrain)
resolve()
}
queue.on(`drain`, onDrain)
})
}
const findDirtyIds = actions => {
const state = store.getState()
const uniqDirties = _.uniq(
actions.reduce((dirtyIds, action) => {
const node = action.payload
if (!node || !node.id || !node.internal.type) return dirtyIds
// Find components that depend on this node so are now dirty.
dirtyIds = dirtyIds.concat(state.componentDataDependencies.nodes[node.id])
// Find connections that depend on this node so are now invalid.
dirtyIds = dirtyIds.concat(
state.componentDataDependencies.connections[node.internal.type]
)
return _.compact(dirtyIds)
}, [])
)
return uniqDirties
}