Runs instaql on a query and a store
(hash, applyOptimistic = true)
| 1372 | |
| 1373 | /** Runs instaql on a query and a store */ |
| 1374 | dataForQuery(hash, applyOptimistic = true) { |
| 1375 | const errorMessage = this._errorMessage; |
| 1376 | if (errorMessage) { |
| 1377 | return { error: errorMessage }; |
| 1378 | } |
| 1379 | if (!this.querySubs) return; |
| 1380 | if (!this.kv.currentValue.pendingMutations) return; |
| 1381 | const querySubVersion = this.querySubs.version(); |
| 1382 | const querySubs = this.querySubs.currentValue; |
| 1383 | const pendingMutationsVersion = this.kv.version(); |
| 1384 | const pendingMutations = this._pendingMutations(); |
| 1385 | |
| 1386 | const { q, result } = querySubs[hash] || {}; |
| 1387 | if (!result) return; |
| 1388 | |
| 1389 | const cached = this._dataForQueryCache[hash]; |
| 1390 | if ( |
| 1391 | cached && |
| 1392 | querySubVersion === cached.querySubVersion && |
| 1393 | pendingMutationsVersion === cached.pendingMutationsVersion |
| 1394 | ) { |
| 1395 | return cached; |
| 1396 | } |
| 1397 | |
| 1398 | let store = result.store; |
| 1399 | let attrsStore = result.attrsStore; |
| 1400 | const { pageInfo, aggregate, processedTxId } = result; |
| 1401 | const mutations = this._rewriteMutationsSorted( |
| 1402 | attrsStore, |
| 1403 | pendingMutations, |
| 1404 | ); |
| 1405 | if (applyOptimistic) { |
| 1406 | const optimisticResult = this._applyOptimisticUpdates( |
| 1407 | store, |
| 1408 | attrsStore, |
| 1409 | mutations, |
| 1410 | processedTxId, |
| 1411 | ); |
| 1412 | |
| 1413 | store = optimisticResult.store; |
| 1414 | attrsStore = optimisticResult.attrsStore; |
| 1415 | } |
| 1416 | const resp = instaql( |
| 1417 | { store: store, attrsStore: attrsStore, pageInfo, aggregate }, |
| 1418 | q, |
| 1419 | ); |
| 1420 | |
| 1421 | return { data: resp, querySubVersion, pendingMutationsVersion }; |
| 1422 | } |
| 1423 | |
| 1424 | _applyOptimisticUpdates(store, attrsStore, mutations, processedTxId) { |
| 1425 | for (const [_, mut] of mutations) { |
no test coverage detected