mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-24 12:04:57 +01:00
Restrict type for iterateCursor
This commit is contained in:
parent
46c306b487
commit
2b44878332
@ -18,19 +18,19 @@ import {iterateCursor, reqAsPromise} from "./utils";
|
|||||||
|
|
||||||
type Reducer<A,B> = (acc: B, val: A) => B
|
type Reducer<A,B> = (acc: B, val: A) => B
|
||||||
|
|
||||||
interface QueryTargetWrapper<T> {
|
interface QueryTargetInterface<T> {
|
||||||
openCursor: (range?: IDBKeyRange | null , direction?: IDBCursorDirection) => IDBRequest<IDBCursorWithValue>
|
openCursor: (range?: IDBKeyRange | null , direction?: IDBCursorDirection) => IDBRequest<IDBCursorWithValue | null>
|
||||||
openKeyCursor: (range: IDBKeyRange, direction: IDBCursorDirection) => IDBRequest<IDBCursor>
|
openKeyCursor: (range: IDBKeyRange, direction: IDBCursorDirection) => IDBRequest<IDBCursor | null>
|
||||||
supports: (method: string) => boolean
|
supports: (method: string) => boolean
|
||||||
keyPath: string
|
keyPath: string
|
||||||
get: (key: IDBValidKey) => IDBRequest<T>
|
get: (key: IDBValidKey) => IDBRequest<T | null>
|
||||||
getKey: (key: IDBValidKey) => IDBRequest<IDBValidKey>
|
getKey: (key: IDBValidKey) => IDBRequest<IDBValidKey | undefined>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class QueryTarget<T> {
|
export class QueryTarget<T> {
|
||||||
private _target: QueryTargetWrapper<T>
|
private _target: QueryTargetInterface<T>
|
||||||
|
|
||||||
constructor(target: QueryTargetWrapper<T>) {
|
constructor(target: QueryTargetInterface<T>) {
|
||||||
this._target = target;
|
this._target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ export class QueryTarget<T> {
|
|||||||
return this._target.supports(methodName);
|
return this._target.supports(methodName);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key: IDBValidKey): Promise<T> {
|
get(key: IDBValidKey): Promise<T | null> {
|
||||||
return reqAsPromise(this._target.get(key));
|
return reqAsPromise(this._target.get(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ export class QueryTarget<T> {
|
|||||||
if (this._target.supports("getKey")) {
|
if (this._target.supports("getKey")) {
|
||||||
return reqAsPromise(this._target.getKey(key));
|
return reqAsPromise(this._target.getKey(key));
|
||||||
} else {
|
} else {
|
||||||
return reqAsPromise<T>(this._target.get(key)).then(value => {
|
return reqAsPromise(this._target.get(key)).then(value => {
|
||||||
if (value) {
|
if (value) {
|
||||||
return value[this._target.keyPath];
|
return value[this._target.keyPath];
|
||||||
}
|
}
|
||||||
@ -119,7 +119,7 @@ export class QueryTarget<T> {
|
|||||||
async findMaxKey(range: IDBKeyRange): Promise<IDBValidKey | undefined> {
|
async findMaxKey(range: IDBKeyRange): Promise<IDBValidKey | undefined> {
|
||||||
const cursor = this._target.openKeyCursor(range, "prev");
|
const cursor = this._target.openKeyCursor(range, "prev");
|
||||||
let maxKey;
|
let maxKey;
|
||||||
await iterateCursor(cursor, (value, key) => {
|
await iterateCursor(cursor, (_, key) => {
|
||||||
maxKey = key;
|
maxKey = key;
|
||||||
return {done: true};
|
return {done: true};
|
||||||
});
|
});
|
||||||
|
@ -128,7 +128,7 @@ type CursorIterator<T, I extends IDBCursor> = I extends IDBCursorWithValue ?
|
|||||||
(value: T, key: IDBValidKey, cursor: IDBCursorWithValue) => { done: boolean, jumpTo?: IDBValidKey } :
|
(value: T, key: IDBValidKey, cursor: IDBCursorWithValue) => { done: boolean, jumpTo?: IDBValidKey } :
|
||||||
(value: undefined, key: IDBValidKey, cursor: IDBCursor) => { done: boolean, jumpTo?: IDBValidKey }
|
(value: undefined, key: IDBValidKey, cursor: IDBCursor) => { done: boolean, jumpTo?: IDBValidKey }
|
||||||
|
|
||||||
export function iterateCursor<T, I extends IDBCursor = IDBCursorWithValue>(cursorRequest: IDBRequest<I>, processValue: CursorIterator<T, I>): Promise<boolean> {
|
export function iterateCursor<T, I extends IDBCursor = IDBCursorWithValue>(cursorRequest: IDBRequest<I | null>, processValue: CursorIterator<T, I>): Promise<boolean> {
|
||||||
// TODO: does cursor already have a value here??
|
// TODO: does cursor already have a value here??
|
||||||
return new Promise<boolean>((resolve, reject) => {
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
cursorRequest.onerror = () => {
|
cursorRequest.onerror = () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user