Merge pull request #460 from vector-im/bwindels/ts-eventkey-direction

Convert EventKey & Direction to typescript + some ts tidbits
This commit is contained in:
Bruno Windels 2021-09-06 13:11:54 +02:00 committed by GitHub
commit 9592d286c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 38 additions and 34 deletions

7
doc/TS-MIGRATION.md Normal file
View file

@ -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

View file

@ -15,34 +15,29 @@ limitations under the License.
*/ */
export class Direction { export class Direction {
constructor(isForward) { constructor(public readonly isForward: boolean) {
this._isForward = isForward;
} }
get isForward() { get isBackward(): boolean {
return this._isForward;
}
get isBackward() {
return !this.isForward; return !this.isForward;
} }
asApiString() { asApiString(): string {
return this.isForward ? "f" : "b"; return this.isForward ? "f" : "b";
} }
reverse() { reverse(): Direction {
return this.isForward ? Direction.Backward : Direction.Forward return this.isForward ? Direction.Backward : Direction.Forward
} }
static get Forward() { static get Forward(): Direction {
return _forward; return _forward;
} }
static get Backward() { static get Backward(): Direction {
return _backward; return _backward;
} }
} }
const _forward = Object.freeze(new Direction(true)); const _forward = new Direction(true);
const _backward = Object.freeze(new Direction(false)); const _backward = new Direction(false);

View file

@ -15,20 +15,22 @@ limitations under the License.
*/ */
import {KeyLimits} from "../../storage/common"; import {KeyLimits} from "../../storage/common";
import {Direction} from "./Direction";
// key for events in the timelineEvents store // key for events in the timelineEvents store
export class EventKey { export class EventKey {
constructor(fragmentId, eventIndex) { constructor(
this.fragmentId = fragmentId; public fragmentId: number,
this.eventIndex = eventIndex; public eventIndex: number
) {
} }
nextFragmentKey() { nextFragmentKey(): EventKey {
// could take MIN_EVENT_INDEX here if it can't be paged back // could take MIN_EVENT_INDEX here if it can't be paged back
return new EventKey(this.fragmentId + 1, KeyLimits.middleStorageKey); return new EventKey(this.fragmentId + 1, KeyLimits.middleStorageKey);
} }
nextKeyForDirection(direction) { nextKeyForDirection(direction: Direction): EventKey {
if (direction.isForward) { if (direction.isForward) {
return this.nextKey(); return this.nextKey();
} else { } else {
@ -36,35 +38,35 @@ export class EventKey {
} }
} }
previousKey() { previousKey(): EventKey {
return new EventKey(this.fragmentId, this.eventIndex - 1); return new EventKey(this.fragmentId, this.eventIndex - 1);
} }
nextKey() { nextKey(): EventKey {
return new EventKey(this.fragmentId, this.eventIndex + 1); return new EventKey(this.fragmentId, this.eventIndex + 1);
} }
static get maxKey() { static get maxKey(): EventKey {
return new EventKey(KeyLimits.maxStorageKey, KeyLimits.maxStorageKey); return new EventKey(KeyLimits.maxStorageKey, KeyLimits.maxStorageKey);
} }
static get minKey() { static get minKey(): EventKey {
return new EventKey(KeyLimits.minStorageKey, KeyLimits.minStorageKey); return new EventKey(KeyLimits.minStorageKey, KeyLimits.minStorageKey);
} }
static get defaultLiveKey() { static get defaultLiveKey(): EventKey {
return EventKey.defaultFragmentKey(KeyLimits.minStorageKey); return EventKey.defaultFragmentKey(KeyLimits.minStorageKey);
} }
static defaultFragmentKey(fragmentId) { static defaultFragmentKey(fragmentId: number): EventKey {
return new EventKey(fragmentId, KeyLimits.middleStorageKey); return new EventKey(fragmentId, KeyLimits.middleStorageKey);
} }
toString() { toString(): string {
return `[${this.fragmentId}/${this.eventIndex}]`; return `[${this.fragmentId}/${this.eventIndex}]`;
} }
equals(other) { equals(other: EventKey): boolean {
return this.fragmentId === other?.fragmentId && this.eventIndex === other?.eventIndex; return this.fragmentId === other?.fragmentId && this.eventIndex === other?.eventIndex;
} }
} }

View file

@ -17,7 +17,7 @@ limitations under the License.
import {SortedArray, AsyncMappedList, ConcatList, ObservableArray} from "../../../observable/index.js"; import {SortedArray, AsyncMappedList, ConcatList, ObservableArray} from "../../../observable/index.js";
import {Disposables} from "../../../utils/Disposables.js"; import {Disposables} from "../../../utils/Disposables.js";
import {Direction} from "./Direction.js"; import {Direction} from "./Direction";
import {TimelineReader} from "./persistence/TimelineReader.js"; import {TimelineReader} from "./persistence/TimelineReader.js";
import {PendingEventEntry} from "./entries/PendingEventEntry.js"; import {PendingEventEntry} from "./entries/PendingEventEntry.js";
import {RoomMember} from "../members/RoomMember.js"; import {RoomMember} from "../members/RoomMember.js";

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
//entries can be sorted, first by fragment, then by entry index. //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; export const PENDING_FRAGMENT_ID = Number.MAX_SAFE_INTEGER;
interface FragmentIdComparer { interface FragmentIdComparer {

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import {BaseEntry} from "./BaseEntry"; import {BaseEntry} from "./BaseEntry";
import {Direction} from "../Direction.js"; import {Direction} from "../Direction";
import {isValidFragmentId} from "../common.js"; import {isValidFragmentId} from "../common.js";
import {KeyLimits} from "../../../storage/common"; import {KeyLimits} from "../../../storage/common";

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {EventKey} from "../EventKey.js"; import {EventKey} from "../EventKey";
import {EventEntry} from "../entries/EventEntry.js"; import {EventEntry} from "../entries/EventEntry.js";
import {createEventEntry, directionalAppend} from "./common.js"; import {createEventEntry, directionalAppend} from "./common.js";
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../members/RoomMember.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../members/RoomMember.js";

View file

@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {EventKey} from "../EventKey.js"; import {EventKey} from "../EventKey";
import {EventEntry} from "../entries/EventEntry.js"; import {EventEntry} from "../entries/EventEntry.js";
import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js"; import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js";
import {createEventEntry} from "./common.js"; import {createEventEntry} from "./common.js";

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import {directionalConcat, directionalAppend} from "./common.js"; import {directionalConcat, directionalAppend} from "./common.js";
import {Direction} from "../Direction.js"; import {Direction} from "../Direction";
import {EventEntry} from "../entries/EventEntry.js"; import {EventEntry} from "../entries/EventEntry.js";
import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js"; import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js";

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {EventKey} from "../../../room/timeline/EventKey.js"; import {EventKey} from "../../../room/timeline/EventKey";
import { StorageError } from "../../common"; import { StorageError } from "../../common";
import { encodeUint32 } from "../utils"; import { encodeUint32 } from "../utils";
import {KeyLimits} from "../../common"; import {KeyLimits} from "../../common";

View file

@ -2,7 +2,7 @@
"compilerOptions": { "compilerOptions": {
"strictNullChecks": true, "strictNullChecks": true,
"noEmit": true, "noEmit": true,
"target": "es6" "target": "ES2020"
}, },
"include": ["src/**/*"], "include": ["src/**/*"],
} }