1
- import { RootDatabase , open } from "lmdb-store"
1
+ import { RootDatabase , open , ArrayLikeIterable } from "lmdb-store"
2
2
// import { performance } from "perf_hooks"
3
- import { ActionsUnion , IGatsbyNode } from "../../redux/types"
3
+ import { ActionsUnion , IDeleteNodeAction , IGatsbyNode } from "../../redux/types"
4
4
import { updateNodes } from "./updates/nodes"
5
5
import { updateNodesByType } from "./updates/nodes-by-type"
6
6
import { IDataStore , ILmdbDatabases , IQueryResult } from "../types"
@@ -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,22 @@ 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 => {
138
+ if ( preSyncDeletedNodeIdsCache . has ( nodeId ) ) {
139
+ return undefined
140
+ }
141
+
142
+ return getNode ( nodeId )
143
+ } )
144
+ . filter ( Boolean ) as ArrayLikeIterable < IGatsbyNode >
139
145
)
140
146
}
141
147
142
148
function getNode ( id : string ) : IGatsbyNode | undefined {
143
- if ( ! id ) return undefined
149
+ if ( ! id || preSyncDeletedNodeIdsCache . has ( id ) ) {
150
+ return undefined
151
+ }
152
+
144
153
const { nodes } = getDatabases ( )
145
154
return nodes . get ( id )
146
155
}
@@ -151,9 +160,11 @@ function getTypes(): Array<string> {
151
160
152
161
function countNodes ( typeName ?: string ) : number {
153
162
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
163
+ const stats = getDatabases ( ) . nodes . getStats ( ) as { entryCount : number }
164
+ return Math . max (
165
+ Number ( stats . entryCount ) - preSyncDeletedNodeIdsCache . size ,
166
+ 0
167
+ ) // FIXME: add -1 when restoring shared structures key
157
168
}
158
169
159
170
const { nodesByType } = getDatabases ( )
@@ -192,15 +203,29 @@ function updateDataStore(action: ActionsUnion): void {
192
203
break
193
204
}
194
205
case `CREATE_NODE` :
206
+ case `DELETE_NODE` :
195
207
case `ADD_FIELD_TO_NODE` :
196
208
case `ADD_CHILD_NODE_TO_PARENT_NODE` :
197
- case `DELETE_NODE` :
198
209
case `MATERIALIZE_PAGE_MODE` : {
199
210
const dbs = getDatabases ( )
200
211
lastOperationPromise = Promise . all ( [
201
212
updateNodes ( dbs . nodes , action ) ,
202
213
updateNodesByType ( dbs . nodesByType , action ) ,
203
214
] )
215
+
216
+ // if create is used in the same transaction as delete we should remove it from cache
217
+ if ( action . type === `CREATE_NODE` ) {
218
+ preSyncDeletedNodeIdsCache . delete ( action . payload . id )
219
+ }
220
+
221
+ if ( action . type === `DELETE_NODE` ) {
222
+ preSyncDeletedNodeIdsCache . add (
223
+ ( ( action as IDeleteNodeAction ) . payload as IGatsbyNode ) . id
224
+ )
225
+ lastOperationPromise . then ( ( ) => {
226
+ preSyncDeletedNodeIdsCache . clear ( )
227
+ } )
228
+ }
204
229
}
205
230
}
206
231
}
0 commit comments