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 { export default class GapTile extends SimpleTile {
constructor(options, timeline) { constructor(options, timeline) {

View File

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

View File

@ -17,9 +17,9 @@ export default class Room extends EventEmitter {
this._timeline = null; this._timeline = null;
} }
persistSync(roomResponse, membership, txn) { async persistSync(roomResponse, membership, txn) {
const summaryChanged = this._summary.applySync(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}; 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 { export default class Direction {
constructor(isForward) { constructor(isForward) {
@ -27,3 +25,6 @@ export default class Direction {
return _backward; 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) { async load(txn) {
const liveFragment = await txn.timelineFragments.liveFragment(this._roomId); const liveFragment = await txn.timelineFragments.liveFragment(this._roomId);
if (liveFragment) { 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 // 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 // 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 // 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) { async writeSync(roomResponse, txn) {
const entries = []; const entries = [];
const timeline = roomResponse.timeline;
if (!this._lastLiveKey) { if (!this._lastLiveKey) {
// means we haven't synced this room yet (just joined or did initial sync) // 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)); entries.push(FragmentBoundaryEntry.start(newFragment, this._fragmentIdComparer));
} }
let currentKey = this._lastLiveKey; let currentKey = this._lastLiveKey;
const timeline = roomResponse.timeline;
if (timeline.events) { if (timeline.events) {
for(const event of timeline.events) { for(const event of timeline.events) {
currentKey = currentKey.nextKey(); currentKey = currentKey.nextKey();
const entry = createEventEntry(currentKey, event); const entry = createEventEntry(currentKey, this._roomId, event);
txn.roomTimeline.insert(entry); txn.timelineEvents.insert(entry);
entries.push(new EventEntry(entry, this._fragmentIdComparer)); entries.push(new EventEntry(entry, this._fragmentIdComparer));
} }
} }
// right thing to do? if the txn fails, not sure we'll continue anyways ... // right thing to do? if the txn fails, not sure we'll continue anyways ...
// only advance the key once the transaction has // only advance the key once the transaction has succeeded
// succeeded
txn.complete().then(() => { txn.complete().then(() => {
console.log("txn complete, setting key"); console.log("txn complete, setting key");
this._lastLiveKey = currentKey; this._lastLiveKey = currentKey;
}); })
// persist state // persist state
const state = roomResponse.state; const state = roomResponse.state;

View File

@ -1,7 +1,8 @@
export function createEventEntry(key, event) { export function createEventEntry(key, roomId, event) {
return { return {
fragmentId: key.fragmentId, fragmentId: key.fragmentId,
eventIndex: key.eventIndex, eventIndex: key.eventIndex,
roomId,
event: event, event: event,
}; };
} }

View File

@ -13,7 +13,7 @@ function createStores(db) {
// need index to find live fragment? prooobably ok without for now // need index to find live fragment? prooobably ok without for now
db.createObjectStore("timelineFragments", {keyPath: ["roomId", "id"]}); 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", [ timelineEvents.createIndex("byEventId", [
"event.room_id", "event.room_id",
"event.event_id" "event.event_id"

View File

@ -9,19 +9,18 @@ const INCREMENTAL_TIMEOUT = 30000;
const SYNC_EVENT_LIMIT = 10; const SYNC_EVENT_LIMIT = 10;
function parseRooms(roomsSection, roomCallback) { function parseRooms(roomsSection, roomCallback) {
if (!roomsSection) { if (roomsSection) {
return;
}
const allMemberships = ["join", "invite", "leave"]; const allMemberships = ["join", "invite", "leave"];
for(const membership of allMemberships) { for(const membership of allMemberships) {
const membershipSection = roomsSection[membership]; const membershipSection = roomsSection[membership];
if (membershipSection) { if (membershipSection) {
const rooms = Object.entries(membershipSection) return Object.entries(membershipSection).map(([roomId, roomResponse]) => {
for (const [roomId, roomResponse] of rooms) { return roomCallback(roomId, roomResponse, membership);
roomCallback(roomId, roomResponse, membership); });
} }
} }
} }
return [];
} }
export default class Sync extends EventEmitter { export default class Sync extends EventEmitter {
@ -73,9 +72,9 @@ export default class Sync extends EventEmitter {
const syncTxn = await this._storage.readWriteTxn([ const syncTxn = await this._storage.readWriteTxn([
storeNames.session, storeNames.session,
storeNames.roomSummary, storeNames.roomSummary,
storeNames.roomState,
storeNames.timelineEvents, storeNames.timelineEvents,
storeNames.timelineFragments, storeNames.timelineFragments,
storeNames.roomState,
]); ]);
const roomChanges = []; const roomChanges = [];
try { try {
@ -83,18 +82,19 @@ export default class Sync extends EventEmitter {
// to_device // to_device
// presence // presence
if (response.rooms) { 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); let room = this._session.rooms.get(roomId);
if (!room) { if (!room) {
room = this._session.createRoom(roomId); room = this._session.createRoom(roomId);
} }
console.log(` * applying sync response to room ${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}); roomChanges.push({room, changes});
}); });
await Promise.all(promises);
} }
} catch(err) { } catch(err) {
console.warn("aborting syncTxn because of error"); console.warn("aborting syncTxn because of error", err.stack);
// avoid corrupting state by only // avoid corrupting state by only
// storing the sync up till the point // storing the sync up till the point
// the exception occurred // the exception occurred

View File

@ -1,5 +1,5 @@
import BaseObservableList from "./BaseObservableList.js"; import BaseObservableList from "./BaseObservableList.js";
import sortedIndex from "../../utils/sortedIndex"; import sortedIndex from "../../utils/sortedIndex.js";
export default class SortedArray extends BaseObservableList { export default class SortedArray extends BaseObservableList {
constructor(comparator) { constructor(comparator) {

View File

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