From 5cafb92fee993353eeaf083bb6056c1d34fd3c4e Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 27 Feb 2019 19:27:45 +0100 Subject: [PATCH] emit after txn has been committed to keep txn open for as short as possible, and avoid any errors caused as part of emit aborting txn --- doc/TODO.md | 2 +- src/matrix/room/room.js | 6 +++++- src/matrix/sync.js | 17 ++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/doc/TODO.md b/doc/TODO.md index 0a6b3420..3d098995 100644 --- a/doc/TODO.md +++ b/doc/TODO.md @@ -21,7 +21,7 @@ - DONE: build a very basic interface with - DONE: a start/stop sync button - DONE: a room list sorted alphabetically - - do some preprocessing on sync response which can then be used by persister, summary, timeline + - DONE: do some preprocessing on sync response which can then be used by persister, summary, timeline - support timeline - clicking on a room list, you see messages (userId -> body) - send messages diff --git a/src/matrix/room/room.js b/src/matrix/room/room.js index 8ec39974..5b5923b6 100644 --- a/src/matrix/room/room.js +++ b/src/matrix/room/room.js @@ -12,9 +12,13 @@ export default class Room extends EventEmitter { this._emitCollectionChange = emitCollectionChange; } - async applySync(roomResponse, membership, txn) { + persistSync(roomResponse, membership, txn) { const changed = this._summary.applySync(roomResponse, membership, txn); this._persister.persistSync(roomResponse, txn); + return changed; + } + + emitSync(changed) { if (changed) { this.emit("change"); (this._emitCollectionChange)(this); diff --git a/src/matrix/sync.js b/src/matrix/sync.js index 7f44d0a5..aae1fa0a 100644 --- a/src/matrix/sync.js +++ b/src/matrix/sync.js @@ -76,10 +76,11 @@ export default class Sync extends EventEmitter { storeNames.roomTimeline, storeNames.roomState, ]); - try { - this._session.applySync(syncToken, response.account_data, syncTxn); - // to_device - // presence + const roomChanges = []; + try { + this._session.applySync(syncToken, response.account_data, syncTxn); + // to_device + // presence if (response.rooms) { parseRooms(response.rooms, (roomId, roomResponse, membership) => { let room = this._session.rooms.get(roomId); @@ -87,7 +88,8 @@ export default class Sync extends EventEmitter { room = this._session.createRoom(roomId); } console.log(` * applying sync response to room ${roomId} ...`); - room.applySync(roomResponse, membership, syncTxn); + const changes = room.persistSync(roomResponse, membership, syncTxn); + roomChanges.push({room, changes}); }); } } catch(err) { @@ -104,6 +106,11 @@ export default class Sync extends EventEmitter { } catch (err) { throw new StorageError("unable to commit sync tranaction", err); } + // emit room related events after txn has been closed + for(let {room, changes} of roomChanges) { + room.emitSync(changes); + } + return syncToken; }