From fad6d63c147e952acc74fb6c1ce2038c100f9237 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 15:35:28 -0700 Subject: [PATCH] Migrate TimelineFragmentStore.js to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- ...gmentStore.js => TimelineFragmentStore.ts} | 40 +++++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) rename src/matrix/storage/idb/stores/{TimelineFragmentStore.js => TimelineFragmentStore.ts} (68%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index 23037195..43f26cd2 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -24,7 +24,7 @@ import {TimelineEventStore} from "./stores/TimelineEventStore"; import {TimelineRelationStore} from "./stores/TimelineRelationStore"; import {RoomStateStore} from "./stores/RoomStateStore"; import {RoomMemberStore} from "./stores/RoomMemberStore"; -import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; +import {TimelineFragmentStore} from "./stores/TimelineFragmentStore"; import {PendingEventStore} from "./stores/PendingEventStore.js"; import {UserIdentityStore} from "./stores/UserIdentityStore.js"; import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js"; diff --git a/src/matrix/storage/idb/stores/TimelineFragmentStore.js b/src/matrix/storage/idb/stores/TimelineFragmentStore.ts similarity index 68% rename from src/matrix/storage/idb/stores/TimelineFragmentStore.js rename to src/matrix/storage/idb/stores/TimelineFragmentStore.ts index 07a8ff42..cb89211a 100644 --- a/src/matrix/storage/idb/stores/TimelineFragmentStore.js +++ b/src/matrix/storage/idb/stores/TimelineFragmentStore.ts @@ -17,17 +17,31 @@ limitations under the License. import { StorageError } from "../../common"; import {KeyLimits} from "../../common"; import { encodeUint32 } from "../utils"; +import {Store} from "../Store"; -function encodeKey(roomId, fragmentId) { +interface Fragment { + roomId: string; + id: number; + previousId: number | null; + nextId: number | null; + previousToken: string | null; + nextToken: string | null; +} + +type FragmentEntry = Fragment & { key: string } + +function encodeKey(roomId: string, fragmentId: number): string { return `${roomId}|${encodeUint32(fragmentId)}`; } export class TimelineFragmentStore { - constructor(store) { + private _store: Store; + + constructor(store: Store) { this._store = store; } - _allRange(roomId) { + _allRange(roomId: string): IDBKeyRange { try { return this._store.IDBKeyRange.bound( encodeKey(roomId, KeyLimits.minStorageKey), @@ -38,13 +52,13 @@ export class TimelineFragmentStore { } } - all(roomId) { + all(roomId: string): Promise { return this._store.selectAll(this._allRange(roomId)); } /** Returns the fragment without a nextToken and without nextId, if any, with the largest id if there are multiple (which should not happen) */ - liveFragment(roomId) { + liveFragment(roomId: string): Promise { // why do we need this? // Ok, take the case where you've got a /context fragment and a /sync fragment // They are not connected. So, upon loading the persister, which one do we take? We can't sort them ... @@ -60,20 +74,20 @@ export class TimelineFragmentStore { // should generate an id an return it? // depends if we want to do anything smart with fragment ids, // like give them meaning depending on range. not for now probably ... - add(fragment) { - fragment.key = encodeKey(fragment.roomId, fragment.id); - this._store.add(fragment); + add(fragment: Fragment): Promise { + (fragment as any).key = encodeKey(fragment.roomId, fragment.id); + return this._store.add(fragment as FragmentEntry); } - update(fragment) { - this._store.put(fragment); + update(fragment: FragmentEntry): Promise { + return this._store.put(fragment); } - get(roomId, fragmentId) { + get(roomId: string, fragmentId: number): Promise { return this._store.get(encodeKey(roomId, fragmentId)); } - removeAllForRoom(roomId) { - this._store.delete(this._allRange(roomId)); + removeAllForRoom(roomId: string): Promise { + return this._store.delete(this._allRange(roomId)); } }