@@ -44,12 +44,11 @@ type NormalisedEntity<E extends FullEntity = FullEntity> = E extends any
44
44
45
45
/** manage nodes based on data returned from Notion API */
46
46
export class NodeManager {
47
-
48
- private entityMap : Map < string , NormalisedEntity > = new Map ( ) ;
49
47
private createNode : NodePluginArgs [ 'actions' ] [ 'createNode' ] ;
50
48
private deleteNode : NodePluginArgs [ 'actions' ] [ 'deleteNode' ] ;
51
49
private createNodeId : NodePluginArgs [ 'createNodeId' ] ;
52
50
private createContentDigest : NodePluginArgs [ 'createContentDigest' ] ;
51
+ private cache : NodePluginArgs [ 'cache' ] ;
53
52
private reporter : NodePluginArgs [ 'reporter' ] ;
54
53
55
54
/**
@@ -60,12 +59,14 @@ export class NodeManager {
60
59
/* eslint-disable @typescript-eslint/unbound-method */
61
60
const {
62
61
actions : { createNode, deleteNode } ,
62
+ cache,
63
63
createContentDigest,
64
64
createNodeId,
65
65
reporter,
66
66
} = args ;
67
67
/* eslint-enable */
68
68
69
+ this . cache = cache ;
69
70
this . createNode = createNode ;
70
71
this . deleteNode = deleteNode ;
71
72
this . createNodeId = createNodeId ;
@@ -77,31 +78,56 @@ export class NodeManager {
77
78
* update nodes available on gatsby
78
79
* @param entities all entities collected from notion, including database and page
79
80
*/
80
- public update ( entities : FullEntity [ ] ) : void {
81
+ public async update ( entities : FullEntity [ ] ) : Promise < void > {
81
82
// get entries with relationship build-in
82
- const newEntityMap = computeEntityMap ( entities ) ;
83
- const added = this . findNewEntities ( newEntityMap ) ;
84
- const updated = this . findUpdatedEntities ( newEntityMap ) ;
85
- const removed = this . findRemovedEntities ( newEntityMap ) ;
83
+ const oldMap = new Map < string , NormalisedEntity > (
84
+ ( await this . cache . get ( 'entityMap' ) ) ?? [ ] ,
85
+ ) ;
86
+ const newMap = computeEntityMap ( entities ) ;
86
87
87
88
// for the usage of createNode
88
89
// see https://www.gatsbyjs.com/docs/reference/config-files/actions/#createNode
90
+ this . addNodes ( this . findNewEntities ( oldMap , newMap ) ) ;
91
+ this . updateNodes ( this . findUpdatedEntities ( oldMap , newMap ) ) ;
92
+ this . removeNodes ( this . findRemovedEntities ( oldMap , newMap ) ) ;
93
+
94
+ await this . cache . set ( 'entityMap' , [ ...newMap . entries ( ) ] ) ;
95
+ }
96
+
97
+ /**
98
+ * add new nodes
99
+ * @param added new nodes to be added
100
+ */
101
+ private addNodes ( added : NormalisedEntity [ ] ) : void {
89
102
for ( const entity of added ) {
90
103
this . createNode ( this . nodifyEntity ( entity ) ) ;
91
104
}
105
+
92
106
this . reporter . info ( `[${ name } ] added ${ added . length } nodes` ) ;
107
+ }
93
108
109
+ /**
110
+ * update existing nodes
111
+ * @param updated updated nodes
112
+ */
113
+ private updateNodes ( updated : NormalisedEntity [ ] ) : void {
94
114
for ( const entity of updated ) {
95
115
this . createNode ( this . nodifyEntity ( entity ) ) ;
96
116
}
117
+
97
118
this . reporter . info ( `[${ name } ] updated ${ updated . length } nodes` ) ;
119
+ }
98
120
121
+ /**
122
+ * remove old nodes
123
+ * @param removed nodes to be removed
124
+ */
125
+ private removeNodes ( removed : NormalisedEntity [ ] ) : void {
99
126
for ( const entity of removed ) {
100
127
this . deleteNode ( this . nodifyEntity ( entity ) ) ;
101
128
}
102
- this . reporter . info ( `[${ name } ] removed ${ removed . length } nodes` ) ;
103
129
104
- this . entityMap = newEntityMap ;
130
+ this . reporter . info ( `[ ${ name } ] removed ${ removed . length } nodes` ) ;
105
131
}
106
132
107
133
/**
@@ -165,15 +191,17 @@ export class NodeManager {
165
191
166
192
/**
167
193
* find new entities
168
- * @param entityMap the new entity map computed from up-to-date data from Notion
194
+ * @param oldMap the old entity map generated from earlier data
195
+ * @param newMap the new entity map computed from up-to-date data from Notion
169
196
* @returns a list of new entities
170
197
*/
171
198
private findNewEntities (
172
- entityMap : Map < string , NormalisedEntity > ,
199
+ oldMap : Map < string , NormalisedEntity > ,
200
+ newMap : Map < string , NormalisedEntity > ,
173
201
) : NormalisedEntity [ ] {
174
202
const added : NormalisedEntity [ ] = [ ] ;
175
- for ( const [ id , newEntity ] of entityMap . entries ( ) ) {
176
- const oldEntity = this . entityMap . get ( id ) ;
203
+ for ( const [ id , newEntity ] of newMap . entries ( ) ) {
204
+ const oldEntity = oldMap . get ( id ) ;
177
205
if ( ! oldEntity ) {
178
206
added . push ( newEntity ) ;
179
207
}
@@ -184,16 +212,18 @@ export class NodeManager {
184
212
185
213
/**
186
214
* find removed entities
187
- * @param entityMap the new entity map computed from up-to-date data from Notion
215
+ * @param oldMap the old entity map generated from earlier data
216
+ * @param newMap the new entity map computed from up-to-date data from Notion
188
217
* @returns a list of removed entities
189
218
*/
190
219
private findRemovedEntities (
191
- entityMap : Map < string , NormalisedEntity > ,
220
+ oldMap : Map < string , NormalisedEntity > ,
221
+ newMap : Map < string , NormalisedEntity > ,
192
222
) : NormalisedEntity [ ] {
193
223
const removed : NormalisedEntity [ ] = [ ] ;
194
224
195
- for ( const [ id , oldEntity ] of this . entityMap . entries ( ) ) {
196
- if ( ! entityMap . has ( id ) ) {
225
+ for ( const [ id , oldEntity ] of oldMap . entries ( ) ) {
226
+ if ( ! newMap . has ( id ) ) {
197
227
removed . push ( oldEntity ) ;
198
228
}
199
229
}
@@ -203,16 +233,18 @@ export class NodeManager {
203
233
204
234
/**
205
235
* find updated entities
206
- * @param entityMap the new entity map computed from up-to-date data from Notion
236
+ * @param oldMap the old entity map generated from earlier data
237
+ * @param newMap the new entity map computed from up-to-date data from Notion
207
238
* @returns a list of updated entities
208
239
*/
209
240
private findUpdatedEntities (
210
- entityMap : Map < string , NormalisedEntity > ,
241
+ oldMap : Map < string , NormalisedEntity > ,
242
+ newMap : Map < string , NormalisedEntity > ,
211
243
) : NormalisedEntity [ ] {
212
244
const updated : NormalisedEntity [ ] = [ ] ;
213
245
214
- for ( const [ id , newEntity ] of entityMap . entries ( ) ) {
215
- const oldEntity = this . entityMap . get ( id ) ;
246
+ for ( const [ id , newEntity ] of newMap . entries ( ) ) {
247
+ const oldEntity = oldMap . get ( id ) ;
216
248
if (
217
249
oldEntity &&
218
250
oldEntity . last_edited_time !== newEntity . last_edited_time
0 commit comments