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: build a very basic interface with
- DONE: a start/stop sync button - DONE: a start/stop sync button
- DONE: a room list sorted alphabetically - 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 - support timeline
- clicking on a room list, you see messages (userId -> body) - clicking on a room list, you see messages (userId -> body)
- send messages - send messages

View file

@ -12,9 +12,13 @@ export default class Room extends EventEmitter {
this._emitCollectionChange = emitCollectionChange; this._emitCollectionChange = emitCollectionChange;
} }
async applySync(roomResponse, membership, txn) { persistSync(roomResponse, membership, txn) {
const changed = this._summary.applySync(roomResponse, membership, txn); const changed = this._summary.applySync(roomResponse, membership, txn);
this._persister.persistSync(roomResponse, txn); this._persister.persistSync(roomResponse, txn);
return changed;
}
emitSync(changed) {
if (changed) { if (changed) {
this.emit("change"); this.emit("change");
(this._emitCollectionChange)(this); (this._emitCollectionChange)(this);

View file

@ -76,10 +76,11 @@ export default class Sync extends EventEmitter {
storeNames.roomTimeline, storeNames.roomTimeline,
storeNames.roomState, storeNames.roomState,
]); ]);
try { const roomChanges = [];
this._session.applySync(syncToken, response.account_data, syncTxn); try {
// to_device this._session.applySync(syncToken, response.account_data, syncTxn);
// presence // to_device
// presence
if (response.rooms) { if (response.rooms) {
parseRooms(response.rooms, (roomId, roomResponse, membership) => { parseRooms(response.rooms, (roomId, roomResponse, membership) => {
let room = this._session.rooms.get(roomId); let room = this._session.rooms.get(roomId);
@ -87,7 +88,8 @@ export default class Sync extends EventEmitter {
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} ...`);
room.applySync(roomResponse, membership, syncTxn); const changes = room.persistSync(roomResponse, membership, syncTxn);
roomChanges.push({room, changes});
}); });
} }
} catch(err) { } catch(err) {
@ -104,6 +106,11 @@ export default class Sync extends EventEmitter {
} catch (err) { } catch (err) {
throw new StorageError("unable to commit sync tranaction", 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; return syncToken;
} }