Skip to content

Commit 7ceceea

Browse files
committed
Debounce writing updated parent node
1 parent 880951e commit 7ceceea

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/gatsby/src/datastore/lmdb/lmdb-datastore.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { IDataStore, ILmdbDatabases, IQueryResult } from "../types"
77
import { emitter, replaceReducer } from "../../redux"
88
import { GatsbyIterable } from "../common/iterable"
99
import { doRunQuery } from "./query/run-query"
10+
import _ from "lodash"
1011
import {
1112
IRunFilterArg,
1213
runFastFiltersAndSort,
@@ -219,6 +220,8 @@ async function runQuery(args: IRunFilterArg): Promise<IQueryResult> {
219220

220221
let lastOperationPromise: Promise<any> = Promise.resolve()
221222

223+
const debounceFunctionsPerNode = new Map()
224+
222225
function updateDataStore(action: ActionsUnion): void {
223226
switch (action.type) {
224227
case `DELETE_CACHE`: {
@@ -237,10 +240,26 @@ function updateDataStore(action: ActionsUnion): void {
237240
clearIndexes()
238241
break
239242
}
243+
case `ADD_CHILD_NODE_TO_PARENT_NODE`: {
244+
let fn
245+
const dbs = getDatabases()
246+
if (!debounceFunctionsPerNode.has(action.payload.id)) {
247+
fn = _.debounce(_action => {
248+
updateNodes(dbs.nodes, _action)
249+
updateNodesByType(dbs.nodesByType, _action)
250+
}, 1000)
251+
debounceFunctionsPerNode.set(action.payload.id, fn)
252+
} else {
253+
fn = debounceFunctionsPerNode.get(action.payload.id)
254+
}
255+
256+
// Call the debounce function.
257+
fn(action)
258+
break
259+
}
240260
case `CREATE_NODE`:
241261
case `DELETE_NODE`:
242262
case `ADD_FIELD_TO_NODE`:
243-
case `ADD_CHILD_NODE_TO_PARENT_NODE`:
244263
case `MATERIALIZE_PAGE_MODE`: {
245264
const dbs = getDatabases()
246265
const operationPromise = Promise.all([

0 commit comments

Comments
 (0)