@@ -7,6 +7,7 @@ import sourceNodesAndRemoveStaleNodes from "../../source-nodes"
7
7
import {
8
8
savePartialStateToDisk ,
9
9
store ,
10
+ emitter ,
10
11
loadPartialStateFromDisk ,
11
12
} from "../../../redux"
12
13
import { loadConfigAndPlugins } from "../../../bootstrap/load-config-and-plugins"
@@ -167,7 +168,6 @@ describeWhenLMDB(`worker (queries)`, () => {
167
168
savePartialStateToDisk ( [ `components` , `staticQueryComponents` ] )
168
169
169
170
await Promise . all ( worker . all . buildSchema ( ) )
170
- await worker . single . runQueries ( queryIdsSmall )
171
171
} )
172
172
173
173
afterAll ( ( ) => {
@@ -180,9 +180,15 @@ describeWhenLMDB(`worker (queries)`, () => {
180
180
}
181
181
} )
182
182
183
+ // This was the original implementation of state syncing between a worker and the main process.
184
+ // We switched to "replaying actions" as a mechanism for state syncing.
185
+ // But we can get back to state saving / merging if "replaying actions" proves to be too expensive
186
+ // TODO: delete or re-activate depending on results yielded by "replaying actions" approach.
187
+ // The logic for `loadPartialStateFromDisk` itself is tested in `share-state` tests
183
188
it ( `should save worker "queries" state to disk` , async ( ) => {
184
189
if ( ! worker ) fail ( `worker not defined` )
185
190
191
+ await worker . single . runQueries ( queryIdsSmall )
186
192
await Promise . all ( worker . all . saveQueries ( ) )
187
193
// Pass "1" as workerId as the test only have one worker
188
194
const result = loadPartialStateFromDisk ( [ `queries` ] , `1` )
@@ -233,6 +239,8 @@ describeWhenLMDB(`worker (queries)`, () => {
233
239
234
240
it ( `should execute static queries` , async ( ) => {
235
241
if ( ! worker ) fail ( `worker not defined` )
242
+
243
+ await worker . single . runQueries ( queryIdsSmall )
236
244
const stateFromWorker = await worker . single . getState ( )
237
245
238
246
const staticQueryResult = await fs . readJson (
@@ -250,6 +258,8 @@ describeWhenLMDB(`worker (queries)`, () => {
250
258
251
259
it ( `should execute page queries` , async ( ) => {
252
260
if ( ! worker ) fail ( `worker not defined` )
261
+
262
+ await worker . single . runQueries ( queryIdsSmall )
253
263
const stateFromWorker = await worker . single . getState ( )
254
264
255
265
const pageQueryResult = await fs . readJson (
@@ -265,6 +275,8 @@ describeWhenLMDB(`worker (queries)`, () => {
265
275
266
276
it ( `should execute page queries with context variables` , async ( ) => {
267
277
if ( ! worker ) fail ( `worker not defined` )
278
+
279
+ await worker . single . runQueries ( queryIdsSmall )
268
280
const stateFromWorker = await worker . single . getState ( )
269
281
270
282
const pageQueryResult = await fs . readJson (
@@ -331,4 +343,131 @@ describeWhenLMDB(`worker (queries)`, () => {
331
343
332
344
spy . mockRestore ( )
333
345
} )
346
+
347
+ it ( `should return actions occurred in worker to replay in the main process` , async ( ) => {
348
+ const result = await worker . single . runQueries ( queryIdsSmall )
349
+
350
+ const expectedActionShapes = {
351
+ QUERY_START : [ `componentPath` , `isPage` , `path` ] ,
352
+ PAGE_QUERY_RUN : [ `componentPath` , `isPage` , `path` , `resultHash` ] ,
353
+ CREATE_COMPONENT_DEPENDENCY : [ `nodeId` , `path` ] ,
354
+ ADD_PENDING_PAGE_DATA_WRITE : [ `path` ] ,
355
+ }
356
+ expect ( result ) . toBeArrayOfSize ( 11 )
357
+
358
+ for ( const action of result ) {
359
+ expect ( action . type ) . toBeOneOf ( Object . keys ( expectedActionShapes ) )
360
+ expect ( action . payload ) . toContainKeys ( expectedActionShapes [ action . type ] )
361
+ }
362
+ // Double-check that important actions are actually present
363
+ expect ( result ) . toContainValue (
364
+ expect . objectContaining ( { type : `QUERY_START` } )
365
+ )
366
+ expect ( result ) . toContainValue (
367
+ expect . objectContaining ( { type : `PAGE_QUERY_RUN` } )
368
+ )
369
+ } )
370
+
371
+ it ( `should replay selected worker actions in runQueriesInWorkersQueue` , async ( ) => {
372
+ const expectedActions = [
373
+ {
374
+ payload : {
375
+ componentPath : `/static-query-component.js` ,
376
+ isPage : false ,
377
+ path : `sq--q1` ,
378
+ } ,
379
+ type : `QUERY_START` ,
380
+ } ,
381
+ {
382
+ payload : {
383
+ nodeId : `ceb8e742-a2ce-5110-a560-94c93d1c71a5` ,
384
+ path : `sq--q1` ,
385
+ } ,
386
+ plugin : `` ,
387
+ type : `CREATE_COMPONENT_DEPENDENCY` ,
388
+ } ,
389
+ {
390
+ payload : {
391
+ componentPath : `/static-query-component.js` ,
392
+ isPage : false ,
393
+ path : `sq--q1` ,
394
+ queryHash : `q1-hash` ,
395
+ resultHash : `Dr5hgCDB+R0S9oRBWeZYj3lB7VI=` ,
396
+ } ,
397
+ type : `PAGE_QUERY_RUN` ,
398
+ } ,
399
+ {
400
+ payload : {
401
+ componentPath : `/foo.js` ,
402
+ isPage : true ,
403
+ path : `/foo` ,
404
+ } ,
405
+ type : `QUERY_START` ,
406
+ } ,
407
+ {
408
+ payload : {
409
+ componentPath : `/bar.js` ,
410
+ isPage : true ,
411
+ path : `/bar` ,
412
+ } ,
413
+ type : `QUERY_START` ,
414
+ } ,
415
+ {
416
+ payload : {
417
+ nodeId : `ceb8e742-a2ce-5110-a560-94c93d1c71a5` ,
418
+ path : `/foo` ,
419
+ } ,
420
+ plugin : `` ,
421
+ type : `CREATE_COMPONENT_DEPENDENCY` ,
422
+ } ,
423
+ {
424
+ payload : {
425
+ nodeId : `ceb8e742-a2ce-5110-a560-94c93d1c71a5` ,
426
+ path : `/bar` ,
427
+ } ,
428
+ plugin : `` ,
429
+ type : `CREATE_COMPONENT_DEPENDENCY` ,
430
+ } ,
431
+ {
432
+ payload : {
433
+ path : `/foo` ,
434
+ } ,
435
+ type : `ADD_PENDING_PAGE_DATA_WRITE` ,
436
+ } ,
437
+ {
438
+ payload : {
439
+ componentPath : `/foo.js` ,
440
+ isPage : true ,
441
+ path : `/foo` ,
442
+ resultHash : `8dW7PoqwZNk/0U8LO6kTj1qBCwU=` ,
443
+ } ,
444
+ type : `PAGE_QUERY_RUN` ,
445
+ } ,
446
+ {
447
+ payload : {
448
+ path : `/bar` ,
449
+ } ,
450
+ type : `ADD_PENDING_PAGE_DATA_WRITE` ,
451
+ } ,
452
+ {
453
+ payload : {
454
+ componentPath : `/bar.js` ,
455
+ isPage : true ,
456
+ path : `/bar` ,
457
+ resultHash : `iKmhf9XgbsfK7qJw0tw95pmGwJM=` ,
458
+ } ,
459
+ type : `PAGE_QUERY_RUN` ,
460
+ } ,
461
+ ]
462
+
463
+ const actualActions : Array < any > = [ ]
464
+ function listenActions ( action ) : void {
465
+ actualActions . push ( action )
466
+ }
467
+ emitter . on ( `*` , listenActions )
468
+ await runQueriesInWorkersQueue ( worker , queryIdsSmall )
469
+ emitter . off ( `*` , listenActions )
470
+
471
+ expect ( actualActions ) . toContainAllValues ( expectedActions )
472
+ } )
334
473
} )
0 commit comments