Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ export class Channel extends DurableObject<Env> {
const writer = writable.getWriter();
this.connections.add(writer);

// Send missed events (Last-Event-ID replay)
if (lastEventId) {
const idx = this.history.findIndex((e) => e.id === lastEventId);
const missed = idx >= 0 ? this.history.slice(idx + 1) : this.history;
// Send history: replay missed events on reconnect, or full history on fresh connect
const toReplay = lastEventId
? (() => {
const idx = this.history.findIndex((e) => e.id === lastEventId);
return idx >= 0 ? this.history.slice(idx + 1) : this.history;
})()
: this.history.slice(); // fresh connection → send full history
if (toReplay.length > 0) {
(async () => {
for (const ev of missed) {
for (const ev of toReplay) {
await writer.write(this.encoder.encode(this.format(ev)));
}
})();
Expand Down