From a1e527ccbc0aa24a179a17451136c07cc675fd22 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 2 Jun 2019 00:49:47 +0200 Subject: [PATCH] first round of fixes after running the app again in the browser! --- .../session/room/timeline/tiles/GapTile.js | 2 +- .../room/timeline/tiles/RoomNameTile.js | 2 +- src/matrix/room/room.js | 4 +-- src/matrix/room/timeline/Direction.js | 5 ++-- .../room/timeline/persistence/SyncWriter.js | 13 ++++----- .../room/timeline/persistence/common.js | 3 +- src/matrix/storage/idb/create.js | 2 +- src/matrix/sync.js | 28 +++++++++---------- src/observable/list/SortedArray.js | 2 +- src/observable/list/SortedMapList.js | 2 +- 10 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/domain/session/room/timeline/tiles/GapTile.js b/src/domain/session/room/timeline/tiles/GapTile.js index 240c071c..55bc7429 100644 --- a/src/domain/session/room/timeline/tiles/GapTile.js +++ b/src/domain/session/room/timeline/tiles/GapTile.js @@ -1,4 +1,4 @@ -import SimpleTile from "./SimpleTile"; +import SimpleTile from "./SimpleTile.js"; export default class GapTile extends SimpleTile { constructor(options, timeline) { diff --git a/src/domain/session/room/timeline/tiles/RoomNameTile.js b/src/domain/session/room/timeline/tiles/RoomNameTile.js index 756d8d97..f415efe6 100644 --- a/src/domain/session/room/timeline/tiles/RoomNameTile.js +++ b/src/domain/session/room/timeline/tiles/RoomNameTile.js @@ -3,7 +3,7 @@ import SimpleTile from "./SimpleTile.js"; export default class RoomNameTile extends SimpleTile { get shape() { - return "annoucement"; + return "announcement"; } get label() { diff --git a/src/matrix/room/room.js b/src/matrix/room/room.js index 09b3427c..7c17b3b1 100644 --- a/src/matrix/room/room.js +++ b/src/matrix/room/room.js @@ -17,9 +17,9 @@ export default class Room extends EventEmitter { this._timeline = null; } - persistSync(roomResponse, membership, txn) { + async persistSync(roomResponse, membership, txn) { const summaryChanged = this._summary.applySync(roomResponse, membership, txn); - const newTimelineEntries = this._syncWriter.writeSync(roomResponse, txn); + const newTimelineEntries = await this._syncWriter.writeSync(roomResponse, txn); return {summaryChanged, newTimelineEntries}; } diff --git a/src/matrix/room/timeline/Direction.js b/src/matrix/room/timeline/Direction.js index d885ed63..6e527e88 100644 --- a/src/matrix/room/timeline/Direction.js +++ b/src/matrix/room/timeline/Direction.js @@ -1,6 +1,4 @@ -const _forward = Object.freeze(new Direction(true)); -const _backward = Object.freeze(new Direction(false)); export default class Direction { constructor(isForward) { @@ -27,3 +25,6 @@ export default class Direction { return _backward; } } + +const _forward = Object.freeze(new Direction(true)); +const _backward = Object.freeze(new Direction(false)); diff --git a/src/matrix/room/timeline/persistence/SyncWriter.js b/src/matrix/room/timeline/persistence/SyncWriter.js index ebec1bae..faf0d6e6 100644 --- a/src/matrix/room/timeline/persistence/SyncWriter.js +++ b/src/matrix/room/timeline/persistence/SyncWriter.js @@ -13,7 +13,7 @@ export default class SyncWriter { async load(txn) { const liveFragment = await txn.timelineFragments.liveFragment(this._roomId); if (liveFragment) { - const [lastEvent] = await txn.roomTimeline.lastEvents(this._roomId, liveFragment.id, 1); + const [lastEvent] = await txn.timelineEvents.lastEvents(this._roomId, liveFragment.id, 1); // sorting and identifying (e.g. sort key and pk to insert) are a bit intertwined here // we could split it up into a SortKey (only with compare) and // a EventKey (no compare or fragment index) with nextkey methods and getters/setters for eventIndex/fragmentId @@ -69,6 +69,7 @@ export default class SyncWriter { async writeSync(roomResponse, txn) { const entries = []; + const timeline = roomResponse.timeline; if (!this._lastLiveKey) { // means we haven't synced this room yet (just joined or did initial sync) @@ -86,22 +87,20 @@ export default class SyncWriter { entries.push(FragmentBoundaryEntry.start(newFragment, this._fragmentIdComparer)); } let currentKey = this._lastLiveKey; - const timeline = roomResponse.timeline; if (timeline.events) { for(const event of timeline.events) { currentKey = currentKey.nextKey(); - const entry = createEventEntry(currentKey, event); - txn.roomTimeline.insert(entry); + const entry = createEventEntry(currentKey, this._roomId, event); + txn.timelineEvents.insert(entry); entries.push(new EventEntry(entry, this._fragmentIdComparer)); } } // right thing to do? if the txn fails, not sure we'll continue anyways ... - // only advance the key once the transaction has - // succeeded + // only advance the key once the transaction has succeeded txn.complete().then(() => { console.log("txn complete, setting key"); this._lastLiveKey = currentKey; - }); + }) // persist state const state = roomResponse.state; diff --git a/src/matrix/room/timeline/persistence/common.js b/src/matrix/room/timeline/persistence/common.js index 5bec1a82..93d96f94 100644 --- a/src/matrix/room/timeline/persistence/common.js +++ b/src/matrix/room/timeline/persistence/common.js @@ -1,7 +1,8 @@ -export function createEventEntry(key, event) { +export function createEventEntry(key, roomId, event) { return { fragmentId: key.fragmentId, eventIndex: key.eventIndex, + roomId, event: event, }; } diff --git a/src/matrix/storage/idb/create.js b/src/matrix/storage/idb/create.js index be5618f0..6ee6af24 100644 --- a/src/matrix/storage/idb/create.js +++ b/src/matrix/storage/idb/create.js @@ -13,7 +13,7 @@ function createStores(db) { // need index to find live fragment? prooobably ok without for now db.createObjectStore("timelineFragments", {keyPath: ["roomId", "id"]}); - const timelineEvents = db.createObjectStore("timelineEvents", {keyPath: ["event.room_id", "fragmentId", "eventIndex"]}); + const timelineEvents = db.createObjectStore("timelineEvents", {keyPath: ["roomId", "fragmentId", "eventIndex"]}); timelineEvents.createIndex("byEventId", [ "event.room_id", "event.event_id" diff --git a/src/matrix/sync.js b/src/matrix/sync.js index b12dd73e..11c6ad4c 100644 --- a/src/matrix/sync.js +++ b/src/matrix/sync.js @@ -9,19 +9,18 @@ const INCREMENTAL_TIMEOUT = 30000; const SYNC_EVENT_LIMIT = 10; function parseRooms(roomsSection, roomCallback) { - if (!roomsSection) { - return; - } - const allMemberships = ["join", "invite", "leave"]; - for(const membership of allMemberships) { - const membershipSection = roomsSection[membership]; - if (membershipSection) { - const rooms = Object.entries(membershipSection) - for (const [roomId, roomResponse] of rooms) { - roomCallback(roomId, roomResponse, membership); + if (roomsSection) { + const allMemberships = ["join", "invite", "leave"]; + for(const membership of allMemberships) { + const membershipSection = roomsSection[membership]; + if (membershipSection) { + return Object.entries(membershipSection).map(([roomId, roomResponse]) => { + return roomCallback(roomId, roomResponse, membership); + }); } } } + return []; } export default class Sync extends EventEmitter { @@ -73,9 +72,9 @@ export default class Sync extends EventEmitter { const syncTxn = await this._storage.readWriteTxn([ storeNames.session, storeNames.roomSummary, + storeNames.roomState, storeNames.timelineEvents, storeNames.timelineFragments, - storeNames.roomState, ]); const roomChanges = []; try { @@ -83,18 +82,19 @@ export default class Sync extends EventEmitter { // to_device // presence if (response.rooms) { - parseRooms(response.rooms, (roomId, roomResponse, membership) => { + const promises = parseRooms(response.rooms, async (roomId, roomResponse, membership) => { let room = this._session.rooms.get(roomId); if (!room) { room = this._session.createRoom(roomId); } console.log(` * applying sync response to room ${roomId} ...`); - const changes = room.persistSync(roomResponse, membership, syncTxn); + const changes = await room.persistSync(roomResponse, membership, syncTxn); roomChanges.push({room, changes}); }); + await Promise.all(promises); } } catch(err) { - console.warn("aborting syncTxn because of error"); + console.warn("aborting syncTxn because of error", err.stack); // avoid corrupting state by only // storing the sync up till the point // the exception occurred diff --git a/src/observable/list/SortedArray.js b/src/observable/list/SortedArray.js index b18b7250..8c90ce15 100644 --- a/src/observable/list/SortedArray.js +++ b/src/observable/list/SortedArray.js @@ -1,5 +1,5 @@ import BaseObservableList from "./BaseObservableList.js"; -import sortedIndex from "../../utils/sortedIndex"; +import sortedIndex from "../../utils/sortedIndex.js"; export default class SortedArray extends BaseObservableList { constructor(comparator) { diff --git a/src/observable/list/SortedMapList.js b/src/observable/list/SortedMapList.js index a3479be0..ebd7e86a 100644 --- a/src/observable/list/SortedMapList.js +++ b/src/observable/list/SortedMapList.js @@ -1,5 +1,5 @@ import BaseObservableList from "./BaseObservableList.js"; -import sortedIndex from "../../utils/sortedIndex"; +import sortedIndex from "../../utils/sortedIndex.js"; /*