1
- const _ = require ( `lodash` )
2
- const fastq = require ( `fastq` )
3
- const { store } = require ( `../redux` )
4
- const { hasFlag, FLAG_ERROR_EXTRACTION } = require ( `../redux/reducers/queries` )
5
- const { queryRunner } = require ( `./query-runner` )
6
- const { websocketManager } = require ( `../utils/websocket-manager` )
7
- const { GraphQLRunner } = require ( `./graphql-runner` )
1
+ import _ from "lodash"
2
+ import fastq from "fastq"
3
+ import { IProgressReporter } from "gatsby-cli/lib/reporter/reporter-progress"
4
+ import { store } from "../redux"
5
+ import { IGatsbyPage , IGatsbyState } from "../redux/types"
6
+ import { hasFlag , FLAG_ERROR_EXTRACTION } from "../redux/reducers/queries"
7
+ import { IQueryJob , queryRunner } from "./query-runner"
8
+ import {
9
+ IStaticQueryResult ,
10
+ websocketManager ,
11
+ } from "../utils/websocket-manager"
12
+ import { GraphQLRunner } from "./graphql-runner"
13
+ import { IGroupedQueryIds } from "../services"
8
14
9
15
if ( process . env . GATSBY_EXPERIMENTAL_QUERY_CONCURRENCY ) {
10
16
console . info (
@@ -21,7 +27,7 @@ const concurrency =
21
27
* Dirty state is tracked in `queries` reducer, here we simply filter
22
28
* them from all tracked queries.
23
29
*/
24
- function calcDirtyQueryIds ( state ) {
30
+ export function calcDirtyQueryIds ( state : IGatsbyState ) : Array < string > {
25
31
const { trackedQueries, trackedComponents, deletedQueries } = state . queries
26
32
27
33
const queriesWithBabelErrors = new Set ( )
@@ -33,7 +39,7 @@ function calcDirtyQueryIds(state) {
33
39
}
34
40
}
35
41
// Note: trackedQueries contains both - page and static query ids
36
- const dirtyQueryIds = [ ]
42
+ const dirtyQueryIds : Array < string > = [ ]
37
43
for ( const [ queryId , query ] of trackedQueries ) {
38
44
if ( deletedQueries . has ( queryId ) ) {
39
45
continue
@@ -45,32 +51,49 @@ function calcDirtyQueryIds(state) {
45
51
return dirtyQueryIds
46
52
}
47
53
54
+ export { calcDirtyQueryIds as calcInitialDirtyQueryIds }
55
+
48
56
/**
49
- * groups queryIds by whether they are static or page queries.
57
+ * Groups queryIds by whether they are static or page queries.
50
58
*/
51
- function groupQueryIds ( queryIds ) {
59
+ export function groupQueryIds ( queryIds : Array < string > ) : IGroupedQueryIds {
52
60
const grouped = _ . groupBy ( queryIds , p =>
53
61
p . slice ( 0 , 4 ) === `sq--` ? `static` : `page`
54
62
)
63
+
64
+ const { pages } = store . getState ( )
65
+
55
66
return {
56
- staticQueryIds : grouped . static || [ ] ,
57
- pageQueryIds : grouped . page || [ ] ,
67
+ staticQueryIds : grouped ?. static || [ ] ,
68
+ pageQueryIds :
69
+ grouped ?. page
70
+ ?. map ( path => pages . get ( path ) as IGatsbyPage )
71
+ ?. filter ( Boolean ) || [ ] ,
58
72
}
59
73
}
60
74
61
- function createQueue ( {
75
+ function createQueue < QueryIDType > ( {
62
76
createJobFn,
63
77
state,
64
78
activity,
65
79
graphqlRunner,
66
80
graphqlTracing,
67
- } ) {
81
+ } : {
82
+ createJobFn : (
83
+ state : IGatsbyState ,
84
+ queryId : QueryIDType
85
+ ) => IQueryJob | undefined
86
+ state : IGatsbyState
87
+ activity : IProgressReporter
88
+ graphqlRunner : GraphQLRunner
89
+ graphqlTracing : boolean
90
+ } ) : fastq . queue < QueryIDType , any > {
68
91
if ( ! graphqlRunner ) {
69
92
graphqlRunner = new GraphQLRunner ( store , { graphqlTracing } )
70
93
}
71
94
state = state || store . getState ( )
72
95
73
- function worker ( queryId , cb ) {
96
+ function worker ( queryId : QueryIDType , cb ) : void {
74
97
const job = createJobFn ( state , queryId )
75
98
if ( ! job ) {
76
99
cb ( null , undefined )
@@ -91,15 +114,28 @@ function createQueue({
91
114
return fastq ( worker , concurrency )
92
115
}
93
116
94
- async function processQueries ( {
117
+ async function processQueries < QueryIDType > ( {
95
118
queryIds,
96
119
createJobFn,
97
120
onQueryDone,
98
121
state,
99
122
activity,
100
123
graphqlRunner,
101
124
graphqlTracing,
102
- } ) {
125
+ } : {
126
+ queryIds : Array < QueryIDType >
127
+ createJobFn : (
128
+ state : IGatsbyState ,
129
+ queryId : QueryIDType
130
+ ) => IQueryJob | undefined
131
+ onQueryDone :
132
+ | ( ( { job, result } : { job : IQueryJob ; result : unknown } ) => void )
133
+ | undefined
134
+ state : IGatsbyState
135
+ activity : IProgressReporter
136
+ graphqlRunner : GraphQLRunner
137
+ graphqlTracing : boolean
138
+ } ) : Promise < void > {
103
139
return new Promise ( ( resolve , reject ) => {
104
140
const fastQueue = createQueue ( {
105
141
createJobFn,
@@ -109,7 +145,7 @@ async function processQueries({
109
145
graphqlTracing,
110
146
} )
111
147
112
- queryIds . forEach ( queryId => {
148
+ queryIds . forEach ( ( queryId : QueryIDType ) => {
113
149
fastQueue . push ( queryId , ( err , res ) => {
114
150
if ( err ) {
115
151
fastQueue . kill ( )
@@ -123,40 +159,57 @@ async function processQueries({
123
159
} )
124
160
125
161
if ( ! fastQueue . idle ( ) ) {
126
- fastQueue . drain = ( ) => resolve ( )
162
+ fastQueue . drain = ( ) : any => resolve ( )
127
163
} else {
128
164
resolve ( )
129
165
}
130
166
} )
131
167
}
132
168
133
- function createStaticQueryJob ( state , queryId ) {
169
+ function createStaticQueryJob (
170
+ state : IGatsbyState ,
171
+ queryId : string
172
+ ) : IQueryJob | undefined {
134
173
const component = state . staticQueryComponents . get ( queryId )
174
+
135
175
if ( ! component ) {
136
176
return undefined
137
177
}
178
+
138
179
const { hash, id, query, componentPath } = component
180
+
139
181
return {
140
182
id : queryId ,
141
- hash,
142
183
query,
184
+ isPage : false ,
185
+ hash,
143
186
componentPath,
144
187
context : { path : id } ,
145
188
}
146
189
}
147
190
148
- function onDevelopStaticQueryDone ( { job, result } ) {
191
+ function onDevelopStaticQueryDone ( {
192
+ job,
193
+ result,
194
+ } : {
195
+ job : IQueryJob
196
+ result : IStaticQueryResult [ "result" ]
197
+ } ) : void {
198
+ if ( ! job . hash ) {
199
+ return
200
+ }
201
+
149
202
websocketManager . emitStaticQueryData ( {
150
203
result,
151
204
id : job . hash ,
152
205
} )
153
206
}
154
207
155
- async function processStaticQueries (
156
- queryIds ,
208
+ export async function processStaticQueries (
209
+ queryIds : IGroupedQueryIds [ "staticQueryIds" ] ,
157
210
{ state, activity, graphqlRunner, graphqlTracing }
158
- ) {
159
- return processQueries ( {
211
+ ) : Promise < void > {
212
+ return processQueries < string > ( {
160
213
queryIds,
161
214
createJobFn : createStaticQueryJob ,
162
215
onQueryDone :
@@ -170,33 +223,34 @@ async function processStaticQueries(
170
223
} )
171
224
}
172
225
173
- async function processPageQueries (
174
- queryIds ,
226
+ export async function processPageQueries (
227
+ queryIds : IGroupedQueryIds [ "pageQueryIds" ] ,
175
228
{ state, activity, graphqlRunner, graphqlTracing }
176
- ) {
177
- return processQueries ( {
229
+ ) : Promise < void > {
230
+ return processQueries < IGatsbyPage > ( {
178
231
queryIds,
179
232
createJobFn : createPageQueryJob ,
233
+ onQueryDone : undefined ,
180
234
state,
181
235
activity,
182
236
graphqlRunner,
183
237
graphqlTracing,
184
238
} )
185
239
}
186
240
187
- function createPageQueryJob ( state , queryId ) {
188
- const page = state . pages . get ( queryId )
241
+ function createPageQueryJob (
242
+ state : IGatsbyState ,
243
+ page : IGatsbyPage
244
+ ) : IQueryJob | undefined {
245
+ const component = state . components . get ( page . componentPath )
189
246
190
- // Make sure we filter out pages that don't exist. An example is
191
- // /dev-404-page/, whose SitePage node is created via
192
- // `internal-data-bridge`, but the actual page object is only
193
- // created during `gatsby develop`.
194
- if ( ! page ) {
247
+ if ( ! component ) {
195
248
return undefined
196
249
}
197
- const component = state . components . get ( page . componentPath )
250
+
198
251
const { path, componentPath, context } = page
199
252
const { query } = component
253
+
200
254
return {
201
255
id : path ,
202
256
query,
@@ -208,11 +262,3 @@ function createPageQueryJob(state, queryId) {
208
262
} ,
209
263
}
210
264
}
211
-
212
- module . exports = {
213
- calcInitialDirtyQueryIds : calcDirtyQueryIds ,
214
- calcDirtyQueryIds,
215
- processPageQueries,
216
- processStaticQueries,
217
- groupQueryIds,
218
- }
0 commit comments