1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

refactor: sliding sync: swap to lists-as-keys (#3086)

* refactor: sliding sync: swap to lists-as-keys

Update the request/response API shape to match the latest
MSC3575 version, which converts `lists` from being an array
of list objects to being a map of list objects.

* Linting

* prettier

* add extra setListRanges test

* Default to right type
This commit is contained in:
kegsay
2023-01-23 15:26:25 +00:00
committed by GitHub
parent 02aa3edda4
commit 6cf6a0c522
3 changed files with 266 additions and 236 deletions

View File

@@ -70,7 +70,7 @@ export interface MSC3575List extends MSC3575RoomSubscription {
*/
export interface MSC3575SlidingSyncRequest {
// json body params
lists?: MSC3575List[];
lists?: Record<string, MSC3575List>;
unsubscribe_rooms?: string[];
room_subscriptions?: Record<string, MSC3575RoomSubscription>;
extensions?: object;
@@ -137,7 +137,7 @@ type Operation = DeleteOperation | InsertOperation | InvalidateOperation | SyncO
export interface MSC3575SlidingSyncResponse {
pos: string;
txn_id?: string;
lists: ListResponse[];
lists: Record<string, ListResponse>;
rooms: Record<string, MSC3575RoomData>;
extensions: Record<string, object>;
}
@@ -332,11 +332,7 @@ export type SlidingSyncEventHandlerMap = {
resp: MSC3575SlidingSyncResponse | null,
err?: Error,
) => void;
[SlidingSyncEvent.List]: (
listIndex: number,
joinedCount: number,
roomIndexToRoomId: Record<number, string>,
) => void;
[SlidingSyncEvent.List]: (listKey: string, joinedCount: number, roomIndexToRoomId: Record<number, string>) => void;
};
/**
@@ -346,7 +342,7 @@ export type SlidingSyncEventHandlerMap = {
* To hook this up with the JS SDK, you need to use SlidingSyncSdk.
*/
export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSyncEventHandlerMap> {
private lists: SlidingList[];
private lists: Map<string, SlidingList>;
private listModifiedCount = 0;
private terminated = false;
// flag set when resend() is called because we cannot rely on detecting AbortError in JS SDK :(
@@ -380,13 +376,16 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
*/
public constructor(
private readonly proxyBaseUrl: string,
lists: MSC3575List[],
lists: Map<string, MSC3575List>,
private roomSubscriptionInfo: MSC3575RoomSubscription,
private readonly client: MatrixClient,
private readonly timeoutMS: number,
) {
super();
this.lists = lists.map((l) => new SlidingList(l));
this.lists = new Map<string, SlidingList>();
lists.forEach((list, key) => {
this.lists.set(key, new SlidingList(list));
});
}
/**
@@ -423,70 +422,70 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
}
/**
* Get the length of the sliding lists.
* @returns The number of lists in the sync request
*/
public listLength(): number {
return this.lists.length;
}
/**
* Get the room data for a list.
* @param index - The list index
* Get the room index data for a list.
* @param key - The list key
* @returns The list data which contains the rooms in this list
*/
public getListData(index: number): { joinedCount: number; roomIndexToRoomId: Record<number, string> } | null {
if (!this.lists[index]) {
public getListData(key: string): { joinedCount: number; roomIndexToRoomId: Record<number, string> } | null {
const data = this.lists.get(key);
if (!data) {
return null;
}
return {
joinedCount: this.lists[index].joinedCount,
roomIndexToRoomId: Object.assign({}, this.lists[index].roomIndexToRoomId),
joinedCount: data.joinedCount,
roomIndexToRoomId: Object.assign({}, data.roomIndexToRoomId),
};
}
/**
* Get the full list parameters for a list index. This function is provided for callers to use
* Get the full request list parameters for a list index. This function is provided for callers to use
* in conjunction with setList to update fields on an existing list.
* @param index - The list index to get the list for.
* @returns A copy of the list or undefined.
* @param key - The list key to get the params for.
* @returns A copy of the list params or undefined.
*/
public getList(index: number): MSC3575List | null {
if (!this.lists[index]) {
public getListParams(key: string): MSC3575List | null {
const params = this.lists.get(key);
if (!params) {
return null;
}
return this.lists[index].getList(true);
return params.getList(true);
}
/**
* Set new ranges for an existing list. Calling this function when _only_ the ranges have changed
* is more efficient than calling setList(index,list) as this function won't resend sticky params,
* whereas setList always will.
* @param index - The list index to modify
* @param key - The list key to modify
* @param ranges - The new ranges to apply.
* @returns A promise which resolves to the transaction ID when it has been received down sync
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
* immediately after sending, in which case the action will be applied in the subsequent request)
*/
public setListRanges(index: number, ranges: number[][]): Promise<string> {
this.lists[index].updateListRange(ranges);
public setListRanges(key: string, ranges: number[][]): Promise<string> {
const list = this.lists.get(key);
if (!list) {
return Promise.reject(new Error("no list with key " + key));
}
list.updateListRange(ranges);
return this.resend();
}
/**
* Add or replace a list. Calling this function will interrupt the /sync request to resend new
* lists.
* @param index - The index to modify
* @param key - The key to modify
* @param list - The new list parameters.
* @returns A promise which resolves to the transaction ID when it has been received down sync
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
* immediately after sending, in which case the action will be applied in the subsequent request)
*/
public setList(index: number, list: MSC3575List): Promise<string> {
if (this.lists[index]) {
this.lists[index].replaceList(list);
public setList(key: string, list: MSC3575List): Promise<string> {
const existingList = this.lists.get(key);
if (existingList) {
existingList.replaceList(list);
this.lists.set(key, existingList);
} else {
this.lists[index] = new SlidingList(list);
this.lists.set(key, new SlidingList(list));
}
this.listModifiedCount += 1;
return this.resend();
@@ -592,32 +591,44 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
this.emit(SlidingSyncEvent.Lifecycle, state, resp, err);
}
private shiftRight(listIndex: number, hi: number, low: number): void {
private shiftRight(listKey: string, hi: number, low: number): void {
const list = this.lists.get(listKey);
if (!list) {
return;
}
// l h
// 0,1,2,3,4 <- before
// 0,1,2,2,3 <- after, hi is deleted and low is duplicated
for (let i = hi; i > low; i--) {
if (this.lists[listIndex].isIndexInRange(i)) {
this.lists[listIndex].roomIndexToRoomId[i] = this.lists[listIndex].roomIndexToRoomId[i - 1];
if (list.isIndexInRange(i)) {
list.roomIndexToRoomId[i] = list.roomIndexToRoomId[i - 1];
}
}
}
private shiftLeft(listIndex: number, hi: number, low: number): void {
private shiftLeft(listKey: string, hi: number, low: number): void {
const list = this.lists.get(listKey);
if (!list) {
return;
}
// l h
// 0,1,2,3,4 <- before
// 0,1,3,4,4 <- after, low is deleted and hi is duplicated
for (let i = low; i < hi; i++) {
if (this.lists[listIndex].isIndexInRange(i)) {
this.lists[listIndex].roomIndexToRoomId[i] = this.lists[listIndex].roomIndexToRoomId[i + 1];
if (list.isIndexInRange(i)) {
list.roomIndexToRoomId[i] = list.roomIndexToRoomId[i + 1];
}
}
}
private removeEntry(listIndex: number, index: number): void {
private removeEntry(listKey: string, index: number): void {
const list = this.lists.get(listKey);
if (!list) {
return;
}
// work out the max index
let max = -1;
for (const n in this.lists[listIndex].roomIndexToRoomId) {
for (const n in list.roomIndexToRoomId) {
if (Number(n) > max) {
max = Number(n);
}
@@ -626,14 +637,18 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
return;
}
// Everything higher than the gap needs to be shifted left.
this.shiftLeft(listIndex, max, index);
delete this.lists[listIndex].roomIndexToRoomId[max];
this.shiftLeft(listKey, max, index);
delete list.roomIndexToRoomId[max];
}
private addEntry(listIndex: number, index: number): void {
private addEntry(listKey: string, index: number): void {
const list = this.lists.get(listKey);
if (!list) {
return;
}
// work out the max index
let max = -1;
for (const n in this.lists[listIndex].roomIndexToRoomId) {
for (const n in list.roomIndexToRoomId) {
if (Number(n) > max) {
max = Number(n);
}
@@ -642,30 +657,37 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
return;
}
// Everything higher than the gap needs to be shifted right, +1 so we don't delete the highest element
this.shiftRight(listIndex, max + 1, index);
this.shiftRight(listKey, max + 1, index);
}
private processListOps(list: ListResponse, listIndex: number): void {
private processListOps(list: ListResponse, listKey: string): void {
let gapIndex = -1;
const listData = this.lists.get(listKey);
if (!listData) {
return;
}
list.ops.forEach((op: Operation) => {
if (!listData) {
return;
}
switch (op.op) {
case "DELETE": {
logger.debug("DELETE", listIndex, op.index, ";");
delete this.lists[listIndex].roomIndexToRoomId[op.index];
logger.debug("DELETE", listKey, op.index, ";");
delete listData.roomIndexToRoomId[op.index];
if (gapIndex !== -1) {
// we already have a DELETE operation to process, so process it.
this.removeEntry(listIndex, gapIndex);
this.removeEntry(listKey, gapIndex);
}
gapIndex = op.index;
break;
}
case "INSERT": {
logger.debug("INSERT", listIndex, op.index, op.room_id, ";");
if (this.lists[listIndex].roomIndexToRoomId[op.index]) {
logger.debug("INSERT", listKey, op.index, op.room_id, ";");
if (listData.roomIndexToRoomId[op.index]) {
// something is in this space, shift items out of the way
if (gapIndex < 0) {
// we haven't been told where to shift from, so make way for a new room entry.
this.addEntry(listIndex, op.index);
this.addEntry(listKey, op.index);
} else if (gapIndex > op.index) {
// the gap is further down the list, shift every element to the right
// starting at the gap so we can just shift each element in turn:
@@ -674,11 +696,11 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
// [A,B,B,C] i=2
// [A,A,B,C] i=1
// Terminate. We'll assign into op.index next.
this.shiftRight(listIndex, gapIndex, op.index);
this.shiftRight(listKey, gapIndex, op.index);
} else if (gapIndex < op.index) {
// the gap is further up the list, shift every element to the left
// starting at the gap so we can just shift each element in turn
this.shiftLeft(listIndex, op.index, gapIndex);
this.shiftLeft(listKey, op.index, gapIndex);
}
}
// forget the gap, we don't need it anymore. This is outside the check for
@@ -686,15 +708,15 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
// forget the gap, not conditionally based on the presence of a room in the INSERT
// position. Without this, DELETE 0; INSERT 0; would do the wrong thing.
gapIndex = -1;
this.lists[listIndex].roomIndexToRoomId[op.index] = op.room_id;
listData.roomIndexToRoomId[op.index] = op.room_id;
break;
}
case "INVALIDATE": {
const startIndex = op.range[0];
for (let i = startIndex; i <= op.range[1]; i++) {
delete this.lists[listIndex].roomIndexToRoomId[i];
delete listData.roomIndexToRoomId[i];
}
logger.debug("INVALIDATE", listIndex, op.range[0], op.range[1], ";");
logger.debug("INVALIDATE", listKey, op.range[0], op.range[1], ";");
break;
}
case "SYNC": {
@@ -704,9 +726,9 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
if (!roomId) {
break; // we are at the end of list
}
this.lists[listIndex].roomIndexToRoomId[i] = roomId;
listData.roomIndexToRoomId[i] = roomId;
}
logger.debug("SYNC", listIndex, op.range[0], op.range[1], (op.room_ids || []).join(" "), ";");
logger.debug("SYNC", listKey, op.range[0], op.range[1], (op.room_ids || []).join(" "), ";");
break;
}
}
@@ -714,7 +736,7 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
if (gapIndex !== -1) {
// we already have a DELETE operation to process, so process it
// Everything higher than the gap needs to be shifted left.
this.removeEntry(listIndex, gapIndex);
this.removeEntry(listKey, gapIndex);
}
}
@@ -814,10 +836,12 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
let resp: MSC3575SlidingSyncResponse | undefined;
try {
const listModifiedCount = this.listModifiedCount;
const reqLists: Record<string, MSC3575List> = {};
this.lists.forEach((l: SlidingList, key: string) => {
reqLists[key] = l.getList(false);
});
const reqBody: MSC3575SlidingSyncRequest = {
lists: this.lists.map((l) => {
return l.getList(false);
}),
lists: reqLists,
pos: currentPos,
timeout: this.timeoutMS,
clientTimeout: this.timeoutMS + BUFFER_PERIOD_MS,
@@ -866,11 +890,15 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
l.setModified(false);
});
// set default empty values so we don't need to null check
resp.lists = resp.lists || [];
resp.lists = resp.lists || {};
resp.rooms = resp.rooms || {};
resp.extensions = resp.extensions || {};
resp.lists.forEach((val, i) => {
this.lists[i].joinedCount = val.count;
Object.keys(resp.lists).forEach((key: string) => {
const list = this.lists.get(key);
if (!list || !resp) {
return;
}
list.joinedCount = resp.lists[key].count;
});
this.invokeLifecycleListeners(SlidingSyncState.RequestFinished, resp);
} catch (err) {
@@ -899,25 +927,24 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
this.invokeRoomDataListeners(roomId, resp!.rooms[roomId]);
});
const listIndexesWithUpdates: Set<number> = new Set();
const listKeysWithUpdates: Set<string> = new Set();
if (!doNotUpdateList) {
resp.lists.forEach((list, listIndex) => {
for (const [key, list] of Object.entries(resp.lists)) {
list.ops = list.ops || [];
if (list.ops.length > 0) {
listIndexesWithUpdates.add(listIndex);
listKeysWithUpdates.add(key);
}
this.processListOps(list, listIndex);
});
this.processListOps(list, key);
}
}
this.invokeLifecycleListeners(SlidingSyncState.Complete, resp);
this.onPostExtensionsResponse(resp.extensions);
listIndexesWithUpdates.forEach((i) => {
this.emit(
SlidingSyncEvent.List,
i,
this.lists[i].joinedCount,
Object.assign({}, this.lists[i].roomIndexToRoomId),
);
listKeysWithUpdates.forEach((listKey: string) => {
const list = this.lists.get(listKey);
if (!list) {
return;
}
this.emit(SlidingSyncEvent.List, listKey, list.joinedCount, Object.assign({}, list.roomIndexToRoomId));
});
this.resolveTransactionDefers(resp.txn_id);