@@ -10,7 +10,6 @@ const deletionCounter: { [key: string]: number } = {}
10
10
* invalidateNode - Recursive function that returns an array of node ids that are invalidated
11
11
* @param gatsbyApi - Gatsby Helpers
12
12
* @param pluginOptions - Plugin Options Object
13
- * @param nodeMap - Map Object of all nodes that haven't been deleted
14
13
* @param id - The root node to invalidate
15
14
*
16
15
* Note: This function is designed to receive a single top-level node on the first pass
@@ -19,24 +18,26 @@ const deletionCounter: { [key: string]: number } = {}
19
18
function invalidateNode (
20
19
gatsbyApi : SourceNodesArgs ,
21
20
pluginOptions : IShopifyPluginOptions ,
22
- nodeMap : IShopifyNodeMap ,
23
- id : string
24
- ) : Array < string > {
21
+ id : string ,
22
+ invalidatedNodeIds = new Set < string > ( )
23
+ ) : Set < string > {
25
24
const { typePrefix } = pluginOptions
26
25
27
- const node = nodeMap [ id ]
28
- let invalidatedNodeIds : Array < string > = [ ]
26
+ const node = gatsbyApi . getNode ( id )
29
27
30
28
if ( node ) {
31
- invalidatedNodeIds . push ( node . id )
29
+ invalidatedNodeIds . add ( node . id )
32
30
const type = node . internal . type . replace ( `${ typePrefix } Shopify` , `` )
33
31
const { coupledNodeFields } = shopifyTypes [ type ]
34
32
35
33
if ( coupledNodeFields ) {
36
34
for ( const field of coupledNodeFields ) {
37
35
for ( const coupledNodeId of node [ field ] as Array < string > ) {
38
- invalidatedNodeIds = invalidatedNodeIds . concat (
39
- invalidateNode ( gatsbyApi , pluginOptions , nodeMap , coupledNodeId )
36
+ invalidateNode (
37
+ gatsbyApi ,
38
+ pluginOptions ,
39
+ coupledNodeId ,
40
+ invalidatedNodeIds
40
41
)
41
42
}
42
43
}
@@ -73,39 +74,41 @@ export async function updateCache(
73
74
pluginOptions : IShopifyPluginOptions ,
74
75
lastBuildTime : Date
75
76
) : Promise < void > {
76
- const { typePrefix } = pluginOptions
77
-
78
- const nodeMap : IShopifyNodeMap = Object . keys ( shopifyTypes )
79
- . map ( type => gatsbyApi . getNodesByType ( `${ typePrefix } Shopify${ type } ` ) )
80
- . reduce ( ( acc , value ) => acc . concat ( value ) , [ ] )
81
- . reduce ( ( acc , value ) => {
82
- return { ...acc , [ value . id ] : value }
83
- } , { } )
84
-
85
77
const { fetchDestroyEventsSince } = eventsApi ( pluginOptions )
86
78
const destroyEvents = await fetchDestroyEventsSince ( lastBuildTime )
87
79
88
- const invalidatedNodeIds = destroyEvents . reduce < Array < string > > (
89
- ( acc , value ) => {
90
- const shopifyId = `gid://shopify/${ value . subject_type } /${ value . subject_id } `
91
- const nodeId = createNodeId ( shopifyId , gatsbyApi , pluginOptions )
92
- return acc . concat (
93
- invalidateNode ( gatsbyApi , pluginOptions , nodeMap , nodeId )
94
- )
95
- } ,
96
- [ ]
97
- )
98
-
99
- for ( const node of Object . values ( nodeMap ) ) {
100
- if ( invalidatedNodeIds . includes ( node . id ) ) {
101
- gatsbyApi . actions . deleteNode ( node )
102
- reportNodeDeletion ( gatsbyApi , node )
103
- } else {
104
- gatsbyApi . actions . touchNode ( node )
80
+ const invalidatedNodeIds = new Set < string > ( )
81
+ for ( const value of destroyEvents ) {
82
+ const shopifyId = `gid://shopify/${ value . subject_type } /${ value . subject_id } `
83
+ const nodeId = createNodeId ( shopifyId , gatsbyApi , pluginOptions )
84
+ invalidateNode ( gatsbyApi , pluginOptions , nodeId , invalidatedNodeIds )
85
+ }
86
+
87
+ // don't block event loop for too long
88
+ await new Promise ( resolve => setImmediate ( resolve ) )
89
+
90
+ for ( const shopifyType of Object . keys ( shopifyTypes ) ) {
91
+ {
92
+ // closure so we can let Node GC `nodes` (if needed) before next iteration
93
+ const nodes = gatsbyApi . getNodesByType (
94
+ `${ pluginOptions . typePrefix } Shopify${ shopifyType } `
95
+ ) as Array < IShopifyNode >
96
+
97
+ for ( const node of nodes ) {
98
+ if ( invalidatedNodeIds . has ( node . id ) ) {
99
+ gatsbyApi . actions . deleteNode ( node )
100
+ reportNodeDeletion ( gatsbyApi , node )
101
+ } else {
102
+ gatsbyApi . actions . touchNode ( node )
103
+ }
104
+ }
105
105
}
106
+
107
+ // don't block event loop for too long
108
+ await new Promise ( resolve => setImmediate ( resolve ) )
106
109
}
107
110
108
- if ( invalidatedNodeIds . length > 0 ) {
111
+ if ( invalidatedNodeIds . size > 0 ) {
109
112
reportDeletionSummary ( gatsbyApi )
110
113
}
111
114
}
0 commit comments