commit 9e1b920730618d727514ad9a8f4ce0624c21b5eb
parent 4318ee172109d1f79bd29d4002500d53f18f0c9f
Author: MTRNord <mtrnord1@gmail.com>
Date: Thu, 13 Jun 2024 10:59:03 +0200
optimize generator for our usecase
Diffstat:
2 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/src/bot.ts b/src/bot.ts
@@ -113,18 +113,9 @@ async function handleMessages(indexer: Indexer, client: MatrixClient, roomId: st
}
async function handleSync(client: MatrixClient, indexer: Indexer) {
- const sync = client.sync();
- for await (const chunk of sync) {
- // parse new events from the sync response
- const joinedRooms = chunk.rooms.join as Record<string, any>;
-
- // Loop over the joined rooms object (we need the key which is the room id and the events within)
- for (const [roomId, room] of Object.entries(joinedRooms)) {
- // Loop over the events in the room
- for (const event of room.timeline.events) {
- void handleMessages(indexer, client, roomId, event)
- }
- }
+ const events = client.sync();
+ for await (const [roomId, event] of events) {
+ void handleMessages(indexer, client, roomId, event)
}
}
diff --git a/src/tiny-sdk.ts b/src/tiny-sdk.ts
@@ -74,7 +74,7 @@ export class MatrixClient {
await fs.writeFile("./storage/bot.json", JSON.stringify({ syncToken }));
}
- async * sync(): AsyncGenerator<Record<string, any>, void, void> {
+ async * sync(): AsyncGenerator<[string, Record<string, any>], void, void> {
if (!this.olmMachine || !this.token) {
throw new Error("Client not started");
}
@@ -189,7 +189,16 @@ export class MatrixClient {
}));
}));
- yield syncResp;
+ // parse new events from the sync response
+ const joinedRooms = syncResp.rooms.join as Record<string, any>;
+
+ // Loop over the joined rooms object (we need the key which is the room id and the events within)
+ for (const [roomId, room] of Object.entries(joinedRooms)) {
+ // Loop over the events in the room
+ for (const event of room.timeline.events) {
+ yield [roomId, event];
+ }
+ }
} while (this.syncRunning);
}