forked from mystiq/hydrogen-web
Migrate PendingEventStore.js to TypeScript
This commit is contained in:
parent
fad6d63c14
commit
b46ae152d6
2 changed files with 33 additions and 16 deletions
|
@ -25,7 +25,7 @@ import {TimelineRelationStore} from "./stores/TimelineRelationStore";
|
||||||
import {RoomStateStore} from "./stores/RoomStateStore";
|
import {RoomStateStore} from "./stores/RoomStateStore";
|
||||||
import {RoomMemberStore} from "./stores/RoomMemberStore";
|
import {RoomMemberStore} from "./stores/RoomMemberStore";
|
||||||
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore";
|
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore";
|
||||||
import {PendingEventStore} from "./stores/PendingEventStore.js";
|
import {PendingEventStore} from "./stores/PendingEventStore";
|
||||||
import {UserIdentityStore} from "./stores/UserIdentityStore.js";
|
import {UserIdentityStore} from "./stores/UserIdentityStore.js";
|
||||||
import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js";
|
import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js";
|
||||||
import {OlmSessionStore} from "./stores/OlmSessionStore.js";
|
import {OlmSessionStore} from "./stores/OlmSessionStore.js";
|
||||||
|
|
|
@ -16,23 +16,40 @@ limitations under the License.
|
||||||
|
|
||||||
import { encodeUint32, decodeUint32 } from "../utils";
|
import { encodeUint32, decodeUint32 } from "../utils";
|
||||||
import {KeyLimits} from "../../common";
|
import {KeyLimits} from "../../common";
|
||||||
|
import {Store} from "../Store";
|
||||||
|
import {Content} from "../../types";
|
||||||
|
|
||||||
function encodeKey(roomId, queueIndex) {
|
interface PendingEntry {
|
||||||
|
roomId: string;
|
||||||
|
queueIndex: number;
|
||||||
|
eventType: string;
|
||||||
|
content: Content;
|
||||||
|
relatexTxnId: string | null;
|
||||||
|
relatedEventId: string | null;
|
||||||
|
txnId?: string;
|
||||||
|
needsEncryption: boolean;
|
||||||
|
needsUpload: boolean;
|
||||||
|
key: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encodeKey(roomId: string, queueIndex: number): string {
|
||||||
return `${roomId}|${encodeUint32(queueIndex)}`;
|
return `${roomId}|${encodeUint32(queueIndex)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function decodeKey(key) {
|
function decodeKey(key: string): { roomId: string, queueIndex: number } {
|
||||||
const [roomId, encodedQueueIndex] = key.split("|");
|
const [roomId, encodedQueueIndex] = key.split("|");
|
||||||
const queueIndex = decodeUint32(encodedQueueIndex);
|
const queueIndex = decodeUint32(encodedQueueIndex);
|
||||||
return {roomId, queueIndex};
|
return {roomId, queueIndex};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PendingEventStore {
|
export class PendingEventStore {
|
||||||
constructor(eventStore) {
|
private _eventStore: Store<PendingEntry>;
|
||||||
|
|
||||||
|
constructor(eventStore: Store<PendingEntry>) {
|
||||||
this._eventStore = eventStore;
|
this._eventStore = eventStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMaxQueueIndex(roomId) {
|
async getMaxQueueIndex(roomId: string): Promise<number | undefined> {
|
||||||
const range = this._eventStore.IDBKeyRange.bound(
|
const range = this._eventStore.IDBKeyRange.bound(
|
||||||
encodeKey(roomId, KeyLimits.minStorageKey),
|
encodeKey(roomId, KeyLimits.minStorageKey),
|
||||||
encodeKey(roomId, KeyLimits.maxStorageKey),
|
encodeKey(roomId, KeyLimits.maxStorageKey),
|
||||||
|
@ -41,38 +58,38 @@ export class PendingEventStore {
|
||||||
);
|
);
|
||||||
const maxKey = await this._eventStore.findMaxKey(range);
|
const maxKey = await this._eventStore.findMaxKey(range);
|
||||||
if (maxKey) {
|
if (maxKey) {
|
||||||
return decodeKey(maxKey).queueIndex;
|
return decodeKey(maxKey as string).queueIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(roomId, queueIndex) {
|
remove(roomId: string, queueIndex: number): Promise<undefined> {
|
||||||
const keyRange = this._eventStore.IDBKeyRange.only(encodeKey(roomId, queueIndex));
|
const keyRange = this._eventStore.IDBKeyRange.only(encodeKey(roomId, queueIndex));
|
||||||
this._eventStore.delete(keyRange);
|
return this._eventStore.delete(keyRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
async exists(roomId, queueIndex) {
|
async exists(roomId: string, queueIndex: number): Promise<boolean> {
|
||||||
const keyRange = this._eventStore.IDBKeyRange.only(encodeKey(roomId, queueIndex));
|
const keyRange = this._eventStore.IDBKeyRange.only(encodeKey(roomId, queueIndex));
|
||||||
const key = await this._eventStore.getKey(keyRange);
|
const key = await this._eventStore.getKey(keyRange);
|
||||||
return !!key;
|
return !!key;
|
||||||
}
|
}
|
||||||
|
|
||||||
add(pendingEvent) {
|
add(pendingEvent: PendingEntry): Promise<IDBValidKey> {
|
||||||
pendingEvent.key = encodeKey(pendingEvent.roomId, pendingEvent.queueIndex);
|
pendingEvent.key = encodeKey(pendingEvent.roomId, pendingEvent.queueIndex);
|
||||||
this._eventStore.add(pendingEvent);
|
return this._eventStore.add(pendingEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(pendingEvent) {
|
update(pendingEvent: PendingEntry): Promise<IDBValidKey> {
|
||||||
this._eventStore.put(pendingEvent);
|
return this._eventStore.put(pendingEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
getAll() {
|
getAll(): Promise<PendingEntry[]> {
|
||||||
return this._eventStore.selectAll();
|
return this._eventStore.selectAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAllForRoom(roomId) {
|
removeAllForRoom(roomId: string): Promise<undefined> {
|
||||||
const minKey = encodeKey(roomId, KeyLimits.minStorageKey);
|
const minKey = encodeKey(roomId, KeyLimits.minStorageKey);
|
||||||
const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey);
|
const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey);
|
||||||
const range = this._eventStore.IDBKeyRange.bound(minKey, maxKey);
|
const range = this._eventStore.IDBKeyRange.bound(minKey, maxKey);
|
||||||
this._eventStore.delete(range);
|
return this._eventStore.delete(range);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue