forked from mystiq/hydrogen-web
add _selectWhile with proper "while" semantics
the existing _selectWhile method was more like _selectUntil, which is what we want for _selectLimit but not for selectWhile(Reverse) The changes we had made before also broke _selectLimit as it would look at the results length before the value got added so you always got 1 value more than requested, breaking sync.
This commit is contained in:
parent
79363ed1d7
commit
fe7cc08287
1 changed files with 19 additions and 8 deletions
|
@ -62,11 +62,11 @@ export class QueryTarget {
|
|||
}
|
||||
|
||||
selectWhile(range, predicate) {
|
||||
return this._selectWhile(range, predicate, "next", false);
|
||||
return this._selectWhile(range, predicate, "next");
|
||||
}
|
||||
|
||||
selectWhileReverse(range, predicate) {
|
||||
return this._selectWhile(range, predicate, "prev", false);
|
||||
return this._selectWhile(range, predicate, "prev");
|
||||
}
|
||||
|
||||
async selectAll(range, direction) {
|
||||
|
@ -151,20 +151,31 @@ export class QueryTarget {
|
|||
}
|
||||
|
||||
_selectLimit(range, amount, direction) {
|
||||
return this._selectWhile(range, (results) => {
|
||||
return this._selectUntil(range, (results) => {
|
||||
return results.length === amount;
|
||||
}, direction, true);
|
||||
}, direction);
|
||||
}
|
||||
|
||||
async _selectWhile(range, predicate, direction, includeFailingPredicateResult) {
|
||||
async _selectUntil(range, predicate, direction) {
|
||||
const cursor = this._openCursor(range, direction);
|
||||
const results = [];
|
||||
await iterateCursor(cursor, (value) => {
|
||||
const passesPredicate = predicate(results, value);
|
||||
if (passesPredicate || includeFailingPredicateResult) {
|
||||
results.push(value);
|
||||
return {done: predicate(results, value)};
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
// allows you to fetch one too much that won't get added when the predicate fails
|
||||
async _selectWhile(range, predicate, direction) {
|
||||
const cursor = this._openCursor(range, direction);
|
||||
const results = [];
|
||||
await iterateCursor(cursor, (value) => {
|
||||
const passesPredicate = predicate(value);
|
||||
if (passesPredicate) {
|
||||
results.push(value);
|
||||
}
|
||||
return {done: passesPredicate};
|
||||
return {done: !passesPredicate};
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue