Wrap room.writeSync and co in an async log wrap to preserve context

This commit is contained in:
Kegan Dougal 2021-12-15 14:11:51 +00:00
parent 21f510e754
commit 131d255180

View file

@ -411,58 +411,60 @@ export class Sync3 {
// session.prepareSync // E2EE decrypts room keys // session.prepareSync // E2EE decrypts room keys
// this.session.writeSync() // write account data, device lists, etc. // this.session.writeSync() // write account data, device lists, etc.
await Promise.all(updates.map(async (roomResponse) => { await Promise.all(updates.map(async (roomResponse) => {
// get or create a room await log.wrap("room", async () => {
let room = this.session.rooms.get(roomResponse.room_id); // get or create a room
if (!room) { let room = this.session.rooms.get(roomResponse.room_id);
room = this.session.createRoom(roomResponse.room_id); if (!room) {
} else { room = this.session.createRoom(roomResponse.room_id);
await room.load(null, syncTxn, log); } else {
} await room.load(null, syncTxn, log);
const invite = { }
isDirectMessage: false, const invite = {
inviter: { userId: null }, isDirectMessage: false,
}; inviter: { userId: null },
// inject a fake m.room.name event if there isn't a real m.room.name event there already };
roomResponse.required_state = roomResponse.required_state || []; // inject a fake m.room.name event if there isn't a real m.room.name event there already
if (roomResponse.name) { roomResponse.required_state = roomResponse.required_state || [];
roomResponse.required_state.push({ if (roomResponse.name) {
event_id: "$name" + roomResponse.room_id, roomResponse.required_state.push({
content: { event_id: "$name" + roomResponse.room_id,
name: roomResponse.name, content: {
}, name: roomResponse.name,
type: "m.room.name", },
state_key: "", type: "m.room.name",
sender: "@noone", state_key: "",
room_id: roomResponse.room_id, sender: "@noone",
}) room_id: roomResponse.room_id,
} })
}
const roomv2Response = { const roomv2Response = {
timeline: { timeline: {
events: roomResponse.timeline || [], events: roomResponse.timeline || [],
}, },
state: { state: {
events: roomResponse.required_state || [], events: roomResponse.required_state || [],
}, },
account_data: null, account_data: null,
summary: null, summary: null,
unread_notifications: { unread_notifications: {
notification_count: roomResponse.notification_count, notification_count: roomResponse.notification_count,
highlight_count: roomResponse.highlight_count, highlight_count: roomResponse.highlight_count,
}, },
} }
// newKeys = [] (null for now) // newKeys = [] (null for now)
const preparation = await room.prepareSync( const preparation = await room.prepareSync(
roomv2Response, "join", invite, null, syncTxn, log, roomv2Response, "join", invite, null, syncTxn, log,
); );
await room.afterPrepareSync(preparation, log); await room.afterPrepareSync(preparation, log);
const changes = await room.writeSync( const changes = await room.writeSync(
roomv2Response, isFirstSync, preparation, syncTxn, log roomv2Response, isFirstSync, preparation, syncTxn, log
) )
rooms.push({ rooms.push({
room: room, room: room,
changes: changes, changes: changes,
}); });
})
})) }))
} catch (err) { } catch (err) {
// avoid corrupting state by only // avoid corrupting state by only