From 3213a0baa00d104454f2bca6581ac3d5b9c74445 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 12 Aug 2021 10:23:55 -0700 Subject: [PATCH] Add type annotations to OlmSessionStore --- .../storage/idb/stores/OlmSessionStore.ts | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/matrix/storage/idb/stores/OlmSessionStore.ts b/src/matrix/storage/idb/stores/OlmSessionStore.ts index d81cc048..e5a8b5ca 100644 --- a/src/matrix/storage/idb/stores/OlmSessionStore.ts +++ b/src/matrix/storage/idb/stores/OlmSessionStore.ts @@ -13,26 +13,37 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +import {Store} from "../Store" -function encodeKey(senderKey, sessionId) { +function encodeKey(senderKey: string, sessionId: string): string { return `${senderKey}|${sessionId}`; } -function decodeKey(key) { +function decodeKey(key: string): { senderKey: string, sessionId: string } { const [senderKey, sessionId] = key.split("|"); return {senderKey, sessionId}; } +interface OlmSession { + session: string + sessionId: string + senderKey: string + lastUsed: number + key: string +} + export class OlmSessionStore { - constructor(store) { + private _store: Store + + constructor(store: Store) { this._store = store; } - async getSessionIds(senderKey) { - const sessionIds = []; + async getSessionIds(senderKey: string): Promise { + const sessionIds: string[] = []; const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, "")); await this._store.iterateKeys(range, key => { - const decodedKey = decodeKey(key); + const decodedKey = decodeKey(key as string); // prevent running into the next room if (decodedKey.senderKey === senderKey) { sessionIds.push(decodedKey.sessionId); @@ -43,23 +54,23 @@ export class OlmSessionStore { return sessionIds; } - getAll(senderKey) { + getAll(senderKey: string): Promise { const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, "")); return this._store.selectWhile(range, session => { return session.senderKey === senderKey; }); } - get(senderKey, sessionId) { + get(senderKey: string, sessionId: string): Promise { return this._store.get(encodeKey(senderKey, sessionId)); } - set(session) { + set(session: OlmSession): Promise { session.key = encodeKey(session.senderKey, session.sessionId); return this._store.put(session); } - remove(senderKey, sessionId) { + remove(senderKey: string, sessionId: string): Promise { return this._store.delete(encodeKey(senderKey, sessionId)); } }