@@ -44,12 +44,11 @@ type NormalisedEntity<E extends FullEntity = FullEntity> = E extends any
4444
4545/** manage nodes based on data returned from Notion API */
4646export class NodeManager {
47-
48- private entityMap : Map < string , NormalisedEntity > = new Map ( ) ;
4947 private createNode : NodePluginArgs [ 'actions' ] [ 'createNode' ] ;
5048 private deleteNode : NodePluginArgs [ 'actions' ] [ 'deleteNode' ] ;
5149 private createNodeId : NodePluginArgs [ 'createNodeId' ] ;
5250 private createContentDigest : NodePluginArgs [ 'createContentDigest' ] ;
51+ private cache : NodePluginArgs [ 'cache' ] ;
5352 private reporter : NodePluginArgs [ 'reporter' ] ;
5453
5554 /**
@@ -60,12 +59,14 @@ export class NodeManager {
6059 /* eslint-disable @typescript-eslint/unbound-method */
6160 const {
6261 actions : { createNode, deleteNode } ,
62+ cache,
6363 createContentDigest,
6464 createNodeId,
6565 reporter,
6666 } = args ;
6767 /* eslint-enable */
6868
69+ this . cache = cache ;
6970 this . createNode = createNode ;
7071 this . deleteNode = deleteNode ;
7172 this . createNodeId = createNodeId ;
@@ -77,31 +78,56 @@ export class NodeManager {
7778 * update nodes available on gatsby
7879 * @param entities all entities collected from notion, including database and page
7980 */
80- public update ( entities : FullEntity [ ] ) : void {
81+ public async update ( entities : FullEntity [ ] ) : Promise < void > {
8182 // 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 ) ;
8687
8788 // for the usage of createNode
8889 // 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 {
89102 for ( const entity of added ) {
90103 this . createNode ( this . nodifyEntity ( entity ) ) ;
91104 }
105+
92106 this . reporter . info ( `[${ name } ] added ${ added . length } nodes` ) ;
107+ }
93108
109+ /**
110+ * update existing nodes
111+ * @param updated updated nodes
112+ */
113+ private updateNodes ( updated : NormalisedEntity [ ] ) : void {
94114 for ( const entity of updated ) {
95115 this . createNode ( this . nodifyEntity ( entity ) ) ;
96116 }
117+
97118 this . reporter . info ( `[${ name } ] updated ${ updated . length } nodes` ) ;
119+ }
98120
121+ /**
122+ * remove old nodes
123+ * @param removed nodes to be removed
124+ */
125+ private removeNodes ( removed : NormalisedEntity [ ] ) : void {
99126 for ( const entity of removed ) {
100127 this . deleteNode ( this . nodifyEntity ( entity ) ) ;
101128 }
102- this . reporter . info ( `[${ name } ] removed ${ removed . length } nodes` ) ;
103129
104- this . entityMap = newEntityMap ;
130+ this . reporter . info ( `[ ${ name } ] removed ${ removed . length } nodes` ) ;
105131 }
106132
107133 /**
@@ -165,15 +191,17 @@ export class NodeManager {
165191
166192 /**
167193 * 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
169196 * @returns a list of new entities
170197 */
171198 private findNewEntities (
172- entityMap : Map < string , NormalisedEntity > ,
199+ oldMap : Map < string , NormalisedEntity > ,
200+ newMap : Map < string , NormalisedEntity > ,
173201 ) : NormalisedEntity [ ] {
174202 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 ) ;
177205 if ( ! oldEntity ) {
178206 added . push ( newEntity ) ;
179207 }
@@ -184,16 +212,18 @@ export class NodeManager {
184212
185213 /**
186214 * 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
188217 * @returns a list of removed entities
189218 */
190219 private findRemovedEntities (
191- entityMap : Map < string , NormalisedEntity > ,
220+ oldMap : Map < string , NormalisedEntity > ,
221+ newMap : Map < string , NormalisedEntity > ,
192222 ) : NormalisedEntity [ ] {
193223 const removed : NormalisedEntity [ ] = [ ] ;
194224
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 ) ) {
197227 removed . push ( oldEntity ) ;
198228 }
199229 }
@@ -203,16 +233,18 @@ export class NodeManager {
203233
204234 /**
205235 * 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
207238 * @returns a list of updated entities
208239 */
209240 private findUpdatedEntities (
210- entityMap : Map < string , NormalisedEntity > ,
241+ oldMap : Map < string , NormalisedEntity > ,
242+ newMap : Map < string , NormalisedEntity > ,
211243 ) : NormalisedEntity [ ] {
212244 const updated : NormalisedEntity [ ] = [ ] ;
213245
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 ) ;
216248 if (
217249 oldEntity &&
218250 oldEntity . last_edited_time !== newEntity . last_edited_time
0 commit comments