Add type annotations to TimelineRelationStore
This commit is contained in:
parent
ddf09af05b
commit
8e93487ebe
1 changed files with 18 additions and 8 deletions
|
@ -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(
|
||||||
|
|
Reference in a new issue