Migrate TimelineRelationStore.js to TypeScript

This commit is contained in:
Danila Fedorin 2021-08-11 14:07:44 -07:00
parent e3b1d034f0
commit 69953e5277
2 changed files with 19 additions and 9 deletions

View File

@ -21,7 +21,7 @@ import {SessionStore} from "./stores/SessionStore";
import {RoomSummaryStore} from "./stores/RoomSummaryStore"; import {RoomSummaryStore} from "./stores/RoomSummaryStore";
import {InviteStore} from "./stores/InviteStore"; import {InviteStore} from "./stores/InviteStore";
import {TimelineEventStore} from "./stores/TimelineEventStore"; import {TimelineEventStore} from "./stores/TimelineEventStore";
import {TimelineRelationStore} from "./stores/TimelineRelationStore.js"; import {TimelineRelationStore} from "./stores/TimelineRelationStore";
import {RoomStateStore} from "./stores/RoomStateStore.js"; import {RoomStateStore} from "./stores/RoomStateStore.js";
import {RoomMemberStore} from "./stores/RoomMemberStore"; import {RoomMemberStore} from "./stores/RoomMemberStore";
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js"; import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";

View File

@ -14,30 +14,40 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {MIN_UNICODE, MAX_UNICODE} from "./common"; 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}`; 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("|"); const [roomId, targetEventId, relType, sourceEventId] = key.split("|");
return {roomId, targetEventId, relType, sourceEventId}; return {roomId, targetEventId, relType, sourceEventId};
} }
export class TimelineRelationStore { export class TimelineRelationStore {
constructor(store) { private _store: Store<{ key: string }>;
constructor(store: Store<{ key: string }>) {
this._store = store; this._store = store;
} }
add(roomId, targetEventId, relType, sourceEventId) { add(roomId: string, targetEventId: string, relType: string, sourceEventId: string): Promise<IDBValidKey> {
return this._store.add({key: encodeKey(roomId, targetEventId, relType, sourceEventId)}); 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<undefined> {
return this._store.delete(encodeKey(roomId, targetEventId, relType, sourceEventId)); return this._store.delete(encodeKey(roomId, targetEventId, relType, sourceEventId));
} }
removeAllForTarget(roomId, targetId) { removeAllForTarget(roomId: string, targetId: string): Promise<undefined> {
const range = this._store.IDBKeyRange.bound( const range = this._store.IDBKeyRange.bound(
encodeKey(roomId, targetId, MIN_UNICODE, MIN_UNICODE), encodeKey(roomId, targetId, MIN_UNICODE, MIN_UNICODE),
encodeKey(roomId, targetId, MAX_UNICODE, MAX_UNICODE), encodeKey(roomId, targetId, MAX_UNICODE, MAX_UNICODE),
@ -47,7 +57,7 @@ export class TimelineRelationStore {
return this._store.delete(range); return this._store.delete(range);
} }
async getForTargetAndType(roomId, targetId, relType) { async getForTargetAndType(roomId: string, targetId: string, relType: string): Promise<RelationEntry[]> {
// exclude both keys as they are theoretical min and max, // 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 // but we should't have a match for just the room id, or room id with max
const range = this._store.IDBKeyRange.bound( const range = this._store.IDBKeyRange.bound(
@ -60,7 +70,7 @@ export class TimelineRelationStore {
return items.map(i => decodeKey(i.key)); return items.map(i => decodeKey(i.key));
} }
async getAllForTarget(roomId, targetId) { async getAllForTarget(roomId: string, targetId: string): Promise<RelationEntry[]> {
// exclude both keys as they are theoretical min and max, // 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 // but we should't have a match for just the room id, or room id with max
const range = this._store.IDBKeyRange.bound( const range = this._store.IDBKeyRange.bound(