first round of fixes after running the app again in the browser!

This commit is contained in:
Bruno Windels 2019-06-02 00:49:47 +02:00
parent 210a00d541
commit a1e527ccbc
10 changed files with 32 additions and 31 deletions

View file

@ -1,4 +1,4 @@
import SimpleTile from "./SimpleTile";
import SimpleTile from "./SimpleTile.js";
export default class GapTile extends SimpleTile {
constructor(options, timeline) {

View file

@ -3,7 +3,7 @@ import SimpleTile from "./SimpleTile.js";
export default class RoomNameTile extends SimpleTile {
get shape() {
return "annoucement";
return "announcement";
}
get label() {

View file

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

View file

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

View file

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

View file

@ -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,
};
}

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
import BaseObservableList from "./BaseObservableList.js";
import sortedIndex from "../../utils/sortedIndex";
import sortedIndex from "../../utils/sortedIndex.js";
/*