merge initial & incremental sync

This commit is contained in:
Bruno Windels 2019-02-04 22:26:45 +00:00
parent b15136a9e8
commit f0e5757692
3 changed files with 14 additions and 33 deletions

View File

@ -1,10 +1,20 @@
import {parseRooms} from "./common";
import {RequestAbortError} from "../network";
import {HomeServerError} from "../error";
import {RequestAbortError} from "./network.js";
import {HomeServerError, StorageError} from "./error.js";
const INCREMENTAL_TIMEOUT = 30;
export class IncrementalSync {
function parseRooms(responseSections, roomMapper) {
return ["join", "invite", "leave"].map(membership => {
const membershipSection = responseSections[membership];
const results = Object.entries(membershipSection).map(([roomId, roomResponse]) => {
const room = roomMapper(roomId, membership);
return room.processInitialSync(roomResponse);
});
return results;
}).reduce((allResults, sectionResults) => allResults.concat(sectionResults), []);
}
export class Sync {
constructor(network, session, storage) {
this._network = network;
this._session = session;

View File

@ -1,10 +0,0 @@
export function parseRooms(responseSections, roomMapper) {
return ["join", "invite", "leave"].map(membership => {
const membershipSection = responseSections[membership];
const results = Object.entries(membershipSection).map(([roomId, roomResponse]) => {
const room = roomMapper(roomId, membership);
return room.processInitialSync(roomResponse);
});
return results;
}).reduce((allResults, sectionResults) => allResults.concat(sectionResults), []);
}

View File

@ -1,19 +0,0 @@
import {parseRooms} from "./common";
// TODO make abortable
export async function initialSync(network, session) {
const response = await network.sync().response();
const rooms = await createRooms(response.rooms, session);
const sessionData = {syncToken: response.next_batch};
const accountData = response.account_data;
await session.applySync(rooms, response.next_batch, response.account_data);
}
function createRooms(responseSections, session) {
const roomPromises = parseRooms(responseSections, (roomId, roomResponse, membership) => {
const room = await session.createRoom(roomId);
await room.initialSync(roomResponse, membership);
return room;
});
return Promise.all(roomPromises);
}