2019-02-07 01:20:27 +01:00
|
|
|
import {iterateCursor} from "./utils.js";
|
2019-01-09 11:06:09 +01:00
|
|
|
|
2019-02-05 00:21:50 +01:00
|
|
|
export default class QueryTarget {
|
2019-02-07 01:20:27 +01:00
|
|
|
constructor(target) {
|
|
|
|
this._target = target;
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:09 +01:00
|
|
|
reduce(range, reducer, initialValue) {
|
|
|
|
return this._reduce(range, reducer, initialValue, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
reduceReverse(range, reducer, initialValue) {
|
|
|
|
return this._reduce(range, reducer, initialValue, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectLimit(range, amount) {
|
|
|
|
return this._selectLimit(range, amount, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectLimitReverse(range, amount) {
|
|
|
|
return this._selectLimit(range, amount, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectWhile(range, predicate) {
|
|
|
|
return this._selectWhile(range, predicate, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectWhileReverse(range, predicate) {
|
|
|
|
return this._selectWhile(range, predicate, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectAll(range) {
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
const results = [];
|
|
|
|
return iterateCursor(cursor, (value) => {
|
|
|
|
results.push(value);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
selectFirst(range) {
|
|
|
|
return this._find(range, () => true, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectLast(range) {
|
|
|
|
return this._find(range, () => true, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
find(range, predicate) {
|
|
|
|
return this._find(range, predicate, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
findReverse(range, predicate) {
|
|
|
|
return this._find(range, predicate, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
_reduce(range, reducer, initialValue, direction) {
|
|
|
|
let reducedValue = initialValue;
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
return iterateCursor(cursor, (value) => {
|
|
|
|
reducedValue = reducer(reducedValue, value);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_selectLimit(range, amount, direction) {
|
|
|
|
return this._selectWhile(range, (results) => {
|
|
|
|
return results.length === amount;
|
|
|
|
}, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
_selectWhile(range, predicate, direction) {
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
const results = [];
|
|
|
|
return iterateCursor(cursor, (value) => {
|
|
|
|
results.push(value);
|
|
|
|
return predicate(results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _find(range, predicate, direction) {
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
let result;
|
|
|
|
const found = await iterateCursor(cursor, (value) => {
|
2019-02-07 01:20:27 +01:00
|
|
|
const found = predicate(value);
|
|
|
|
if (found) {
|
2019-01-09 11:06:09 +01:00
|
|
|
result = value;
|
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
return found;
|
2019-01-09 11:06:09 +01:00
|
|
|
});
|
2019-02-07 01:20:27 +01:00
|
|
|
if (found) {
|
|
|
|
return result;
|
2019-01-09 11:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|