From 5579c018d19cd1057e694b1995b420e3d309303e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 9 Aug 2021 13:44:07 -0700 Subject: [PATCH] Migrate common.js to TypeScript Add initial stab at annotating common Add missing return types and semicolons --- src/matrix/room/timeline/EventKey.js | 2 +- .../timeline/entries/FragmentBoundaryEntry.js | 2 +- src/matrix/storage/{common.js => common.ts} | 17 ++++++++++------- src/matrix/storage/idb/Storage.js | 2 +- src/matrix/storage/idb/Transaction.js | 2 +- src/matrix/storage/idb/error.js | 2 +- src/matrix/storage/idb/export.js | 2 +- .../storage/idb/stores/PendingEventStore.js | 2 +- .../storage/idb/stores/TimelineEventStore.js | 4 ++-- .../storage/idb/stores/TimelineFragmentStore.js | 4 ++-- src/matrix/storage/idb/utils.js | 2 +- src/matrix/storage/memory/Storage.js | 2 +- 12 files changed, 23 insertions(+), 20 deletions(-) rename src/matrix/storage/{common.js => common.ts} (79%) diff --git a/src/matrix/room/timeline/EventKey.js b/src/matrix/room/timeline/EventKey.js index b4dce376..af01a7e5 100644 --- a/src/matrix/room/timeline/EventKey.js +++ b/src/matrix/room/timeline/EventKey.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {KeyLimits} from "../../storage/common.js"; +import {KeyLimits} from "../../storage/common"; // key for events in the timelineEvents store export class EventKey { diff --git a/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js b/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js index 3d4bd67b..0697fbdd 100644 --- a/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js +++ b/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js @@ -17,7 +17,7 @@ limitations under the License. import {BaseEntry} from "./BaseEntry"; import {Direction} from "../Direction.js"; import {isValidFragmentId} from "../common.js"; -import {KeyLimits} from "../../../storage/common.js"; +import {KeyLimits} from "../../../storage/common"; export class FragmentBoundaryEntry extends BaseEntry { constructor(fragment, isFragmentStart, fragmentIdComparer) { diff --git a/src/matrix/storage/common.js b/src/matrix/storage/common.ts similarity index 79% rename from src/matrix/storage/common.js rename to src/matrix/storage/common.ts index 4d10ef65..926ccbbf 100644 --- a/src/matrix/storage/common.js +++ b/src/matrix/storage/common.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -export const STORE_NAMES = Object.freeze([ +export const STORE_NAMES: Readonly = Object.freeze([ "session", "roomState", "roomSummary", @@ -35,13 +35,16 @@ export const STORE_NAMES = Object.freeze([ "accountData", ]); -export const STORE_MAP = Object.freeze(STORE_NAMES.reduce((nameMap, name) => { +export const STORE_MAP: Readonly<{ [name : string]: string }> = Object.freeze(STORE_NAMES.reduce((nameMap, name) => { nameMap[name] = name; return nameMap; }, {})); export class StorageError extends Error { - constructor(message, cause) { + errcode?: string; + cause?: Error; + + constructor(message: string, cause?: Error) { super(message); if (cause) { this.errcode = cause.name; @@ -49,23 +52,23 @@ export class StorageError extends Error { this.cause = cause; } - get name() { + get name(): string { return "StorageError"; } } export const KeyLimits = { - get minStorageKey() { + get minStorageKey(): number { // for indexeddb, we use unsigned 32 bit integers as keys return 0; }, - get middleStorageKey() { + get middleStorageKey(): number { // for indexeddb, we use unsigned 32 bit integers as keys return 0x7FFFFFFF; }, - get maxStorageKey() { + get maxStorageKey(): number { // for indexeddb, we use unsigned 32 bit integers as keys return 0xFFFFFFFF; } diff --git a/src/matrix/storage/idb/Storage.js b/src/matrix/storage/idb/Storage.js index 1f96e347..0a357dda 100644 --- a/src/matrix/storage/idb/Storage.js +++ b/src/matrix/storage/idb/Storage.js @@ -15,7 +15,7 @@ limitations under the License. */ import {Transaction} from "./Transaction.js"; -import { STORE_NAMES, StorageError } from "../common.js"; +import { STORE_NAMES, StorageError } from "../common"; import { reqAsPromise } from "./utils.js"; const WEBKITEARLYCLOSETXNBUG_BOGUS_KEY = "782rh281re38-boguskey"; diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index bdcc45e3..ae0f75be 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -15,7 +15,7 @@ limitations under the License. */ import {txnAsPromise} from "./utils.js"; -import {StorageError} from "../common.js"; +import {StorageError} from "../common"; import {Store} from "./Store.js"; import {SessionStore} from "./stores/SessionStore.js"; import {RoomSummaryStore} from "./stores/RoomSummaryStore.js"; diff --git a/src/matrix/storage/idb/error.js b/src/matrix/storage/idb/error.js index 2ba6289f..0b9de95c 100644 --- a/src/matrix/storage/idb/error.js +++ b/src/matrix/storage/idb/error.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { StorageError } from "../common.js"; +import { StorageError } from "../common"; export class IDBError extends StorageError { constructor(message, source, cause) { diff --git a/src/matrix/storage/idb/export.js b/src/matrix/storage/idb/export.js index 8a7724a3..34dd8706 100644 --- a/src/matrix/storage/idb/export.js +++ b/src/matrix/storage/idb/export.js @@ -15,7 +15,7 @@ limitations under the License. */ import { iterateCursor, txnAsPromise } from "./utils.js"; -import { STORE_NAMES } from "../common.js"; +import { STORE_NAMES } from "../common"; export async function exportSession(db) { const NOT_DONE = {done: false}; diff --git a/src/matrix/storage/idb/stores/PendingEventStore.js b/src/matrix/storage/idb/stores/PendingEventStore.js index 455cad2c..385f3c9a 100644 --- a/src/matrix/storage/idb/stores/PendingEventStore.js +++ b/src/matrix/storage/idb/stores/PendingEventStore.js @@ -15,7 +15,7 @@ limitations under the License. */ import { encodeUint32, decodeUint32 } from "../utils.js"; -import {KeyLimits} from "../../common.js"; +import {KeyLimits} from "../../common"; function encodeKey(roomId, queueIndex) { return `${roomId}|${encodeUint32(queueIndex)}`; diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.js b/src/matrix/storage/idb/stores/TimelineEventStore.js index b4b204a8..664958e6 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.js +++ b/src/matrix/storage/idb/stores/TimelineEventStore.js @@ -15,9 +15,9 @@ limitations under the License. */ import {EventKey} from "../../../room/timeline/EventKey.js"; -import { StorageError } from "../../common.js"; +import { StorageError } from "../../common"; import { encodeUint32 } from "../utils.js"; -import {KeyLimits} from "../../common.js"; +import {KeyLimits} from "../../common"; function encodeKey(roomId, fragmentId, eventIndex) { return `${roomId}|${encodeUint32(fragmentId)}|${encodeUint32(eventIndex)}`; diff --git a/src/matrix/storage/idb/stores/TimelineFragmentStore.js b/src/matrix/storage/idb/stores/TimelineFragmentStore.js index 96ff441e..208b7300 100644 --- a/src/matrix/storage/idb/stores/TimelineFragmentStore.js +++ b/src/matrix/storage/idb/stores/TimelineFragmentStore.js @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { StorageError } from "../../common.js"; -import {KeyLimits} from "../../common.js"; +import { StorageError } from "../../common"; +import {KeyLimits} from "../../common"; import { encodeUint32 } from "../utils.js"; function encodeKey(roomId, fragmentId) { diff --git a/src/matrix/storage/idb/utils.js b/src/matrix/storage/idb/utils.js index fbb82fc5..f90324cd 100644 --- a/src/matrix/storage/idb/utils.js +++ b/src/matrix/storage/idb/utils.js @@ -16,7 +16,7 @@ limitations under the License. */ import { IDBRequestError } from "./error.js"; -import { StorageError } from "../common.js"; +import { StorageError } from "../common"; let needsSyncPromise = false; diff --git a/src/matrix/storage/memory/Storage.js b/src/matrix/storage/memory/Storage.js index c1c0fe3c..a76c16f3 100644 --- a/src/matrix/storage/memory/Storage.js +++ b/src/matrix/storage/memory/Storage.js @@ -15,7 +15,7 @@ limitations under the License. */ import {Transaction} from "./Transaction.js"; -import { STORE_MAP, STORE_NAMES } from "../common.js"; +import { STORE_MAP, STORE_NAMES } from "../common"; export class Storage { constructor(initialStoreValues = {}) {