Skip to content

Commit

Permalink
Resolves #1863
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Jan 7, 2024
1 parent 49af9aa commit 9ce97b6
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/live-query/cache/apply-optimistic-ops.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cmp } from '../../functions/cmp';
import { deepClone } from '../../functions/utils';
import { deepClone, isArray } from '../../functions/utils';
import { RangeSet, rangesOverlap } from '../../helpers/rangeset';
import { CacheEntry } from '../../public/types/cache';
import {
Expand All @@ -20,6 +20,8 @@ export function applyOptimisticOps(
): any[] {
if (!ops || ops.length === 0) return result;
const index = req.query.index;
const { multiEntry } = index;
const queryRange = req.query.range;
const primaryKey = table.schema.primaryKey;
const extractPrimKey = primaryKey.extractKey;
const extractIndex = index.extractKey;
Expand All @@ -29,13 +31,16 @@ export function applyOptimisticOps(
let modifedResult = result;
const includedValues =
op.type === 'add' || op.type === 'put'
? op.values.filter((v) =>
isWithinRange(extractIndex(v), req.query.range)
).map(v => {
v = deepClone(v);// v might come from user so we can't just freeze it.
if (immutable) Object.freeze(v);
return v;
})
? op.values.filter((v) => {
const key = extractIndex(v);
return multiEntry && isArray(key) // multiEntry index work like plain index unless key is array
? key.some((k) => isWithinRange(k, queryRange)) // multiEntry and array key
: isWithinRange(key, queryRange); // multiEntry but not array key
}).map(v => {
v = deepClone(v);// v might come from user so we can't just freeze it.
if (immutable) Object.freeze(v);
return v;
})
: [];
switch (op.type) {
case 'add':
Expand Down

0 comments on commit 9ce97b6

Please sign in to comment.