From 69953e5277594122c7cd02b33423d77101379a41 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Aug 2021 14:07:44 -0700 Subject: [PATCH] Migrate TimelineRelationStore.js to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- ...ationStore.js => TimelineRelationStore.ts} | 26 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) rename src/matrix/storage/idb/stores/{TimelineRelationStore.js => TimelineRelationStore.ts} (72%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index a047a7a2..71513730 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -21,7 +21,7 @@ import {SessionStore} from "./stores/SessionStore"; import {RoomSummaryStore} from "./stores/RoomSummaryStore"; import {InviteStore} from "./stores/InviteStore"; import {TimelineEventStore} from "./stores/TimelineEventStore"; -import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; +import {TimelineRelationStore} from "./stores/TimelineRelationStore"; import {RoomStateStore} from "./stores/RoomStateStore.js"; import {RoomMemberStore} from "./stores/RoomMemberStore"; import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; diff --git a/src/matrix/storage/idb/stores/TimelineRelationStore.js b/src/matrix/storage/idb/stores/TimelineRelationStore.ts similarity index 72% rename from src/matrix/storage/idb/stores/TimelineRelationStore.js rename to src/matrix/storage/idb/stores/TimelineRelationStore.ts index 5e802b65..0905eb0c 100644 --- a/src/matrix/storage/idb/stores/TimelineRelationStore.js +++ b/src/matrix/storage/idb/stores/TimelineRelationStore.ts @@ -14,30 +14,40 @@ See the License for the specific language governing permissions and limitations under the License. */ import {MIN_UNICODE, MAX_UNICODE} from "./common"; +import {Store} from "../Store"; -function encodeKey(roomId, targetEventId, relType, sourceEventId) { +function encodeKey(roomId: string, targetEventId: string, relType: string, sourceEventId: string): string { return `${roomId}|${targetEventId}|${relType}|${sourceEventId}`; } -function decodeKey(key) { +interface RelationEntry { + roomId: string; + targetEventId: string; + sourceEventId: string; + relType: string; +} + +function decodeKey(key: string): RelationEntry { const [roomId, targetEventId, relType, sourceEventId] = key.split("|"); return {roomId, targetEventId, relType, sourceEventId}; } export class TimelineRelationStore { - constructor(store) { + private _store: Store<{ key: string }>; + + constructor(store: Store<{ key: string }>) { this._store = store; } - add(roomId, targetEventId, relType, sourceEventId) { + add(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise { return this._store.add({key: encodeKey(roomId, targetEventId, relType, sourceEventId)}); } - remove(roomId, targetEventId, relType, sourceEventId) { + remove(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise { return this._store.delete(encodeKey(roomId, targetEventId, relType, sourceEventId)); } - removeAllForTarget(roomId, targetId) { + removeAllForTarget(roomId: string, targetId: string): Promise { const range = this._store.IDBKeyRange.bound( encodeKey(roomId, targetId, MIN_UNICODE, MIN_UNICODE), encodeKey(roomId, targetId, MAX_UNICODE, MAX_UNICODE), @@ -47,7 +57,7 @@ export class TimelineRelationStore { return this._store.delete(range); } - async getForTargetAndType(roomId, targetId, relType) { + async getForTargetAndType(roomId: string, targetId: string, relType: string): Promise { // exclude both keys as they are theoretical min and max, // but we should't have a match for just the room id, or room id with max const range = this._store.IDBKeyRange.bound( @@ -60,7 +70,7 @@ export class TimelineRelationStore { return items.map(i => decodeKey(i.key)); } - async getAllForTarget(roomId, targetId) { + async getAllForTarget(roomId: string, targetId: string): Promise { // exclude both keys as they are theoretical min and max, // but we should't have a match for just the room id, or room id with max const range = this._store.IDBKeyRange.bound(