diff --git a/doc/TS-MIGRATION.md b/doc/TS-MIGRATION.md new file mode 100644 index 00000000..7cb5bb34 --- /dev/null +++ b/doc/TS-MIGRATION.md @@ -0,0 +1,7 @@ +# Typescript migration + +## Introduce `abstract` & `override` + + - find all methods and getters that throw or are empty in base classes and turn into abstract method or if all methods are abstract, into an interface. + - change child impls to not call super.method and to add override + - don't allow implicit override in ts config diff --git a/src/matrix/room/timeline/Direction.js b/src/matrix/room/timeline/Direction.ts similarity index 70% rename from src/matrix/room/timeline/Direction.js rename to src/matrix/room/timeline/Direction.ts index c83ec61c..24a8ff41 100644 --- a/src/matrix/room/timeline/Direction.js +++ b/src/matrix/room/timeline/Direction.ts @@ -15,34 +15,29 @@ limitations under the License. */ export class Direction { - constructor(isForward) { - this._isForward = isForward; + constructor(public readonly isForward: boolean) { } - get isForward() { - return this._isForward; - } - - get isBackward() { + get isBackward(): boolean { return !this.isForward; } - asApiString() { + asApiString(): string { return this.isForward ? "f" : "b"; } - reverse() { + reverse(): Direction { return this.isForward ? Direction.Backward : Direction.Forward } - static get Forward() { + static get Forward(): Direction { return _forward; } - static get Backward() { + static get Backward(): Direction { return _backward; } } -const _forward = Object.freeze(new Direction(true)); -const _backward = Object.freeze(new Direction(false)); +const _forward = new Direction(true); +const _backward = new Direction(false); diff --git a/src/matrix/room/timeline/EventKey.js b/src/matrix/room/timeline/EventKey.ts similarity index 75% rename from src/matrix/room/timeline/EventKey.js rename to src/matrix/room/timeline/EventKey.ts index af01a7e5..8775df26 100644 --- a/src/matrix/room/timeline/EventKey.js +++ b/src/matrix/room/timeline/EventKey.ts @@ -15,20 +15,22 @@ limitations under the License. */ import {KeyLimits} from "../../storage/common"; +import {Direction} from "./Direction"; // key for events in the timelineEvents store export class EventKey { - constructor(fragmentId, eventIndex) { - this.fragmentId = fragmentId; - this.eventIndex = eventIndex; + constructor( + public fragmentId: number, + public eventIndex: number + ) { } - nextFragmentKey() { + nextFragmentKey(): EventKey { // could take MIN_EVENT_INDEX here if it can't be paged back return new EventKey(this.fragmentId + 1, KeyLimits.middleStorageKey); } - nextKeyForDirection(direction) { + nextKeyForDirection(direction: Direction): EventKey { if (direction.isForward) { return this.nextKey(); } else { @@ -36,35 +38,35 @@ export class EventKey { } } - previousKey() { + previousKey(): EventKey { return new EventKey(this.fragmentId, this.eventIndex - 1); } - nextKey() { + nextKey(): EventKey { return new EventKey(this.fragmentId, this.eventIndex + 1); } - static get maxKey() { + static get maxKey(): EventKey { return new EventKey(KeyLimits.maxStorageKey, KeyLimits.maxStorageKey); } - static get minKey() { + static get minKey(): EventKey { return new EventKey(KeyLimits.minStorageKey, KeyLimits.minStorageKey); } - static get defaultLiveKey() { + static get defaultLiveKey(): EventKey { return EventKey.defaultFragmentKey(KeyLimits.minStorageKey); } - static defaultFragmentKey(fragmentId) { + static defaultFragmentKey(fragmentId: number): EventKey { return new EventKey(fragmentId, KeyLimits.middleStorageKey); } - toString() { + toString(): string { return `[${this.fragmentId}/${this.eventIndex}]`; } - equals(other) { + equals(other: EventKey): boolean { return this.fragmentId === other?.fragmentId && this.eventIndex === other?.eventIndex; } } diff --git a/src/matrix/room/timeline/Timeline.js b/src/matrix/room/timeline/Timeline.js index fdfaee48..022eda4e 100644 --- a/src/matrix/room/timeline/Timeline.js +++ b/src/matrix/room/timeline/Timeline.js @@ -17,7 +17,7 @@ limitations under the License. import {SortedArray, AsyncMappedList, ConcatList, ObservableArray} from "../../../observable/index.js"; import {Disposables} from "../../../utils/Disposables.js"; -import {Direction} from "./Direction.js"; +import {Direction} from "./Direction"; import {TimelineReader} from "./persistence/TimelineReader.js"; import {PendingEventEntry} from "./entries/PendingEventEntry.js"; import {RoomMember} from "../members/RoomMember.js"; diff --git a/src/matrix/room/timeline/entries/BaseEntry.ts b/src/matrix/room/timeline/entries/BaseEntry.ts index 1ef6a863..23ec268c 100644 --- a/src/matrix/room/timeline/entries/BaseEntry.ts +++ b/src/matrix/room/timeline/entries/BaseEntry.ts @@ -15,7 +15,7 @@ limitations under the License. */ //entries can be sorted, first by fragment, then by entry index. -import {EventKey} from "../EventKey.js"; +import {EventKey} from "../EventKey"; export const PENDING_FRAGMENT_ID = Number.MAX_SAFE_INTEGER; interface FragmentIdComparer { diff --git a/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js b/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js index 0697fbdd..7a8ed5d0 100644 --- a/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js +++ b/src/matrix/room/timeline/entries/FragmentBoundaryEntry.js @@ -15,7 +15,7 @@ limitations under the License. */ import {BaseEntry} from "./BaseEntry"; -import {Direction} from "../Direction.js"; +import {Direction} from "../Direction"; import {isValidFragmentId} from "../common.js"; import {KeyLimits} from "../../../storage/common"; diff --git a/src/matrix/room/timeline/persistence/GapWriter.js b/src/matrix/room/timeline/persistence/GapWriter.js index 2fc56a21..e133d713 100644 --- a/src/matrix/room/timeline/persistence/GapWriter.js +++ b/src/matrix/room/timeline/persistence/GapWriter.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {EventKey} from "../EventKey.js"; +import {EventKey} from "../EventKey"; import {EventEntry} from "../entries/EventEntry.js"; import {createEventEntry, directionalAppend} from "./common.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../members/RoomMember.js"; diff --git a/src/matrix/room/timeline/persistence/SyncWriter.js b/src/matrix/room/timeline/persistence/SyncWriter.js index 54fd5a0b..1ee2ce7d 100644 --- a/src/matrix/room/timeline/persistence/SyncWriter.js +++ b/src/matrix/room/timeline/persistence/SyncWriter.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {EventKey} from "../EventKey.js"; +import {EventKey} from "../EventKey"; import {EventEntry} from "../entries/EventEntry.js"; import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js"; import {createEventEntry} from "./common.js"; diff --git a/src/matrix/room/timeline/persistence/TimelineReader.js b/src/matrix/room/timeline/persistence/TimelineReader.js index 27b88ea9..ffad80ec 100644 --- a/src/matrix/room/timeline/persistence/TimelineReader.js +++ b/src/matrix/room/timeline/persistence/TimelineReader.js @@ -15,7 +15,7 @@ limitations under the License. */ import {directionalConcat, directionalAppend} from "./common.js"; -import {Direction} from "../Direction.js"; +import {Direction} from "../Direction"; import {EventEntry} from "../entries/EventEntry.js"; import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js"; diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.ts b/src/matrix/storage/idb/stores/TimelineEventStore.ts index f659bdbd..49892771 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.ts +++ b/src/matrix/storage/idb/stores/TimelineEventStore.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {EventKey} from "../../../room/timeline/EventKey.js"; +import {EventKey} from "../../../room/timeline/EventKey"; import { StorageError } from "../../common"; import { encodeUint32 } from "../utils"; import {KeyLimits} from "../../common"; diff --git a/tsconfig.json b/tsconfig.json index b7ae64e8..0179e267 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "strictNullChecks": true, "noEmit": true, - "target": "es6" + "target": "ES2020" }, "include": ["src/**/*"], }