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
This commit is contained in:
Bruno Windels 2019-02-27 19:27:45 +01:00
parent c0bd8fed82
commit 5cafb92fee
3 changed files with 18 additions and 7 deletions

View File

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

View File

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

View File

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