From cab334ed7310d85eb6f7178f3d7fb0de44a092b5 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Thu, 1 Jul 2021 22:47:50 +0100
Subject: [PATCH] Convert Sync and SyncAccumulator to Typescript
---
src/client.ts | 9 +-
src/crypto/index.ts | 13 +-
src/models/event.ts | 2 +-
src/models/room-member.ts | 1 +
src/store/index.ts | 10 +-
src/store/indexeddb.ts | 3 +-
src/store/memory.ts | 4 +-
src/store/stub.ts | 4 +-
...ync-accumulator.js => sync-accumulator.ts} | 294 ++-
src/sync.js | 1710 ----------------
src/sync.ts | 1745 +++++++++++++++++
src/utils.ts | 6 +-
12 files changed, 1981 insertions(+), 1820 deletions(-)
rename src/{sync-accumulator.js => sync-accumulator.ts} (76%)
delete mode 100644 src/sync.js
create mode 100644 src/sync.ts
diff --git a/src/client.ts b/src/client.ts
index c967a17b7..820f08e02 100644
--- a/src/client.ts
+++ b/src/client.ts
@@ -298,6 +298,11 @@ export interface IMatrixClientCreateOpts extends ICreateClientOpts {
usingExternalCrypto?: boolean;
}
+export enum PendingEventOrdering {
+ Chronological = "chronological",
+ Detached = "detached",
+}
+
export interface IStartClientOpts {
/**
* The event limit= to apply to initial sync. Default: 8.
@@ -320,7 +325,7 @@ export interface IStartClientOpts {
* pending messages will appear in a separate list, accessbile via {@link module:models/room#getPendingEvents}.
* Default: "chronological".
*/
- pendingEventOrdering?: "chronological" | "detached";
+ pendingEventOrdering?: PendingEventOrdering;
/**
* The number of milliseconds to wait on /sync. Default: 30000 (30 seconds).
@@ -454,7 +459,7 @@ export class MatrixClient extends EventEmitter {
protected fallbackICEServerAllowed = false;
protected roomList: RoomList;
protected syncApi: SyncApi;
- protected pushRules: any; // TODO: Types
+ public pushRules: any; // TODO: Types
protected syncLeftRoomsPromise: Promise;
protected syncedLeftRooms = false;
protected clientOpts: IStoredClientOpts;
diff --git a/src/crypto/index.ts b/src/crypto/index.ts
index 070f8f5d9..10d471deb 100644
--- a/src/crypto/index.ts
+++ b/src/crypto/index.ts
@@ -57,6 +57,7 @@ import type { EncryptionAlgorithm, DecryptionAlgorithm } from "./algorithms/base
import type { RoomList } from "./RoomList";
import { IRecoveryKey, IEncryptedEventInfo } from "./api";
import { IKeyBackupInfo } from "./keybackup";
+import { ISyncStateData } from "../sync";
const DeviceVerification = DeviceInfo.DeviceVerification;
@@ -148,12 +149,6 @@ interface IUserOlmSession {
}[];
}
-interface ISyncData {
- oldSyncToken?: string;
- nextSyncToken: string;
- catchingUp?: boolean;
-}
-
interface ISyncDeviceLists {
changed: string[];
left: string[];
@@ -2780,7 +2775,7 @@ export class Crypto extends EventEmitter {
* @param {Object} syncDeviceLists device_lists field from /sync, or response from
* /keys/changes
*/
- public async handleDeviceListChanges(syncData: ISyncData, syncDeviceLists: ISyncDeviceLists): Promise {
+ public async handleDeviceListChanges(syncData: ISyncStateData, syncDeviceLists: ISyncDeviceLists): Promise {
// Initial syncs don't have device change lists. We'll either get the complete list
// of changes for the interval or will have invalidated everything in willProcessSync
if (!syncData.oldSyncToken) return;
@@ -2870,7 +2865,7 @@ export class Crypto extends EventEmitter {
*
* @param {Object} syncData the data from the 'MatrixClient.sync' event
*/
- public async onSyncWillProcess(syncData: ISyncData): Promise {
+ public async onSyncWillProcess(syncData: ISyncStateData): Promise {
if (!syncData.oldSyncToken) {
// If there is no old sync token, we start all our tracking from
// scratch, so mark everything as untracked. onCryptoEvent will
@@ -2894,7 +2889,7 @@ export class Crypto extends EventEmitter {
*
* @param {Object} syncData the data from the 'MatrixClient.sync' event
*/
- public async onSyncCompleted(syncData: ISyncData): Promise {
+ public async onSyncCompleted(syncData: ISyncStateData): Promise {
this.deviceList.setSyncToken(syncData.nextSyncToken);
this.deviceList.saveIfDirty();
diff --git a/src/models/event.ts b/src/models/event.ts
index 73648dca9..6b29bb6b0 100644
--- a/src/models/event.ts
+++ b/src/models/event.ts
@@ -74,7 +74,7 @@ export interface IContent {
type StrippedState = Required>;
-interface IUnsigned {
+export interface IUnsigned {
age?: number;
prev_sender?: string;
prev_content?: IContent;
diff --git a/src/models/room-member.ts b/src/models/room-member.ts
index 589fddd95..e7a98257b 100644
--- a/src/models/room-member.ts
+++ b/src/models/room-member.ts
@@ -29,6 +29,7 @@ import { RoomState } from "./room-state";
export class RoomMember extends EventEmitter {
private _isOutOfBand = false;
private _modified: number;
+ public _requestedProfileInfo: boolean; // used by sync.ts
// XXX these should be read-only
public typing = false;
diff --git a/src/store/index.ts b/src/store/index.ts
index a49f0ad23..80cdc8a1a 100644
--- a/src/store/index.ts
+++ b/src/store/index.ts
@@ -21,6 +21,14 @@ import { User } from "../models/user";
import { MatrixEvent } from "../models/event";
import { Filter } from "../filter";
import { RoomSummary } from "../models/room-summary";
+import { IMinimalEvent, IGroups, IRooms } from "../sync-accumulator";
+
+export interface ISavedSync {
+ nextBatch: string;
+ roomsData: IRooms;
+ groupsData: IGroups;
+ accountData: IMinimalEvent[];
+}
/**
* Construct a stub store. This does no-ops on most store methods.
@@ -199,7 +207,7 @@ export interface IStore {
* client state to where it was at the last save, or null if there
* is no saved sync data.
*/
- getSavedSync(): Promise