1
- import { RootDatabase , open } from "lmdb-store"
1
+ import { RootDatabase , open , ArrayLikeIterable } from "lmdb-store"
2
2
// import { performance } from "perf_hooks"
3
3
import { ActionsUnion , IGatsbyNode } from "../../redux/types"
4
4
import { updateNodes } from "./updates/nodes"
@@ -27,6 +27,8 @@ const lmdbDatastore = {
27
27
getNodesByType,
28
28
}
29
29
30
+ const preSyncDeletedNodeIdsCache = new Set ( )
31
+
30
32
function getDefaultDbPath ( ) : string {
31
33
const dbFileName =
32
34
process . env . NODE_ENV === `test`
@@ -122,10 +124,8 @@ function iterateNodes(): GatsbyIterable<IGatsbyNode> {
122
124
return new GatsbyIterable (
123
125
nodesDb
124
126
. getKeys ( { snapshot : false } )
125
- . map (
126
- nodeId => ( typeof nodeId === `string` ? getNode ( nodeId ) : undefined ) !
127
- )
128
- . filter ( Boolean )
127
+ . map ( nodeId => ( typeof nodeId === `string` ? getNode ( nodeId ) : undefined ) )
128
+ . filter ( Boolean ) as ArrayLikeIterable < IGatsbyNode >
129
129
)
130
130
}
131
131
@@ -134,13 +134,16 @@ function iterateNodesByType(type: string): GatsbyIterable<IGatsbyNode> {
134
134
return new GatsbyIterable (
135
135
nodesByType
136
136
. getValues ( type )
137
- . map ( nodeId => getNode ( nodeId ) ! )
138
- . filter ( Boolean )
137
+ . map ( nodeId => getNode ( nodeId ) )
138
+ . filter ( Boolean ) as ArrayLikeIterable < IGatsbyNode >
139
139
)
140
140
}
141
141
142
142
function getNode ( id : string ) : IGatsbyNode | undefined {
143
- if ( ! id ) return undefined
143
+ if ( ! id || preSyncDeletedNodeIdsCache . has ( id ) ) {
144
+ return undefined
145
+ }
146
+
144
147
const { nodes } = getDatabases ( )
145
148
return nodes . get ( id )
146
149
}
@@ -151,9 +154,11 @@ function getTypes(): Array<string> {
151
154
152
155
function countNodes ( typeName ?: string ) : number {
153
156
if ( ! typeName ) {
154
- const stats = getDatabases ( ) . nodes . getStats ( )
155
- // @ts -ignore
156
- return Number ( stats . entryCount || 0 ) // FIXME: add -1 when restoring shared structures key
157
+ const stats = getDatabases ( ) . nodes . getStats ( ) as { entryCount : number }
158
+ return Math . max (
159
+ Number ( stats . entryCount ) - preSyncDeletedNodeIdsCache . size ,
160
+ 0
161
+ ) // FIXME: add -1 when restoring shared structures key
157
162
}
158
163
159
164
const { nodesByType } = getDatabases ( )
@@ -192,14 +197,30 @@ function updateDataStore(action: ActionsUnion): void {
192
197
break
193
198
}
194
199
case `CREATE_NODE` :
200
+ case `DELETE_NODE` :
195
201
case `ADD_FIELD_TO_NODE` :
196
- case `ADD_CHILD_NODE_TO_PARENT_NODE` :
197
- case `DELETE_NODE` : {
202
+ case `ADD_CHILD_NODE_TO_PARENT_NODE` : {
198
203
const dbs = getDatabases ( )
199
- lastOperationPromise = Promise . all ( [
204
+ const operationPromise = Promise . all ( [
200
205
updateNodes ( dbs . nodes , action ) ,
201
206
updateNodesByType ( dbs . nodesByType , action ) ,
202
207
] )
208
+ lastOperationPromise = operationPromise
209
+
210
+ // if create is used in the same transaction as delete we should remove it from cache
211
+ if ( action . type === `CREATE_NODE` ) {
212
+ preSyncDeletedNodeIdsCache . delete ( action . payload . id )
213
+ }
214
+
215
+ if ( action . type === `DELETE_NODE` && action . payload ?. id ) {
216
+ preSyncDeletedNodeIdsCache . add ( action . payload . id )
217
+ operationPromise . then ( ( ) => {
218
+ // only clear if no other operations have been done in the meantime
219
+ if ( lastOperationPromise === operationPromise ) {
220
+ preSyncDeletedNodeIdsCache . clear ( )
221
+ }
222
+ } )
223
+ }
203
224
}
204
225
}
205
226
}
0 commit comments