Skip to content

Commit 93d65bb

Browse files
committed
feat: use Gatsby cache for preserving data across APIs
1 parent b4ffd32 commit 93d65bb

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

source/node.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ type NormalisedEntity<E extends FullEntity = FullEntity> = E extends any
4444

4545
/** manage nodes based on data returned from Notion API */
4646
export 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

source/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,5 @@ export async function sync(
119119
}
120120

121121
// update nodes
122-
manager.update([...databases, ...pages]);
122+
await manager.update([...databases, ...pages]);
123123
}

spec/node.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* -------------------------------------------------------------------------
1414
*/
1515

16+
import { caching } from 'cache-manager';
17+
1618
import { computeEntityMap, normaliseParent, NodeManager } from '#node';
1719

1820
import type { FullDatabase, FullPage } from '#types';
@@ -189,14 +191,15 @@ describe('fn:normaliseParent', () => {
189191

190192
describe('cl:NodeManager', () => {
191193
describe('fn:update', () => {
192-
it('always keep gatsby synced', () => {
194+
it('always keep gatsby synced', async () => {
193195
const createNode = jest.fn();
194196
const deleteNode = jest.fn();
195197
const createContentDigest = jest.fn(() => 'digest');
196198
const createNodeId = jest.fn((id) => id);
197199

198200
const manager = new NodeManager({
199201
actions: { createNode, deleteNode },
202+
cache: caching({ store: 'memory', ttl: 0 }),
200203
createContentDigest,
201204
createNodeId,
202205
reporter: { info: jest.fn() },
@@ -216,7 +219,7 @@ describe('cl:NodeManager', () => {
216219
});
217220

218221
// first call
219-
manager.update([originalDatabase, ...originalDatabase.pages]);
222+
await manager.update([originalDatabase, ...originalDatabase.pages]);
220223

221224
expect(createNode).toBeCalledTimes(3);
222225
expect(deleteNode).toBeCalledTimes(0);
@@ -237,7 +240,7 @@ describe('cl:NodeManager', () => {
237240
}),
238241
],
239242
});
240-
manager.update([updatedDatabase, ...updatedDatabase.pages]);
243+
await manager.update([updatedDatabase, ...updatedDatabase.pages]);
241244
expect(createNode).toBeCalledTimes(1);
242245
expect(deleteNode).toBeCalledTimes(1);
243246
});

0 commit comments

Comments
 (0)