From 50d9c1294b281266771c40486be0a63ff451dab5 Mon Sep 17 00:00:00 2001 From: Declan Chidlow Date: Sat, 11 Jul 2026 21:47:41 +0800 Subject: [PATCH] Hopefully recover from disconnect --- packages/bot/src/stoat/collections/ChannelCollection.ts | 5 ++++- packages/bot/src/stoat/collections/MessageCollection.ts | 5 ++++- packages/bot/src/stoat/collections/ServerCollection.ts | 5 ++++- packages/bot/src/stoat/collections/ServerMemberCollection.ts | 5 ++++- packages/bot/src/stoat/collections/UserCollection.ts | 5 ++++- packages/bot/src/stoat/ws.ts | 2 +- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/bot/src/stoat/collections/ChannelCollection.ts b/packages/bot/src/stoat/collections/ChannelCollection.ts index 345c220..3ef54bc 100644 --- a/packages/bot/src/stoat/collections/ChannelCollection.ts +++ b/packages/bot/src/stoat/collections/ChannelCollection.ts @@ -26,7 +26,10 @@ export class ChannelCollection extends BaseCollection { getOrCreate(id: string, data: any, isNew: boolean = false): Channel { const existing = this.get(id); - if (existing) return existing; + if (existing) { + this.updateUnderlyingObject(id, data); + return existing; + } this.underlying.set(id, data); diff --git a/packages/bot/src/stoat/collections/MessageCollection.ts b/packages/bot/src/stoat/collections/MessageCollection.ts index 89ed4ba..36ad420 100644 --- a/packages/bot/src/stoat/collections/MessageCollection.ts +++ b/packages/bot/src/stoat/collections/MessageCollection.ts @@ -6,7 +6,10 @@ const MAX_MESSAGES = 50_000; export class MessageCollection extends BaseCollection { getOrCreate(id: string, data: any, _isNew?: boolean): Message { const existing = this.get(id); - if (existing) return existing; + if (existing) { + this.updateUnderlyingObject(id, data); + return existing; + } // Evict oldest entries when over capacity if (this.objects.size >= MAX_MESSAGES) { diff --git a/packages/bot/src/stoat/collections/ServerCollection.ts b/packages/bot/src/stoat/collections/ServerCollection.ts index e1059e8..da9f771 100644 --- a/packages/bot/src/stoat/collections/ServerCollection.ts +++ b/packages/bot/src/stoat/collections/ServerCollection.ts @@ -24,7 +24,10 @@ export class ServerCollection extends BaseCollection { getOrCreate(id: string, data: any, isNew: boolean = false): Server { const existing = this.get(id); - if (existing) return existing; + if (existing) { + this.updateUnderlyingObject(id, data); + return existing; + } this.underlying.set(id, data); diff --git a/packages/bot/src/stoat/collections/ServerMemberCollection.ts b/packages/bot/src/stoat/collections/ServerMemberCollection.ts index 1a58c26..832b9f8 100644 --- a/packages/bot/src/stoat/collections/ServerMemberCollection.ts +++ b/packages/bot/src/stoat/collections/ServerMemberCollection.ts @@ -35,7 +35,10 @@ export class ServerMemberCollection extends BaseCollection { getOrCreate(id: { server: string; user: string }, data: any): ServerMember { const key = id.server + id.user; const existing = this.get(key); - if (existing) return existing; + if (existing) { + this.updateUnderlyingObject(key, data); + return existing; + } this.underlying.set(key, { ...data }); diff --git a/packages/bot/src/stoat/collections/UserCollection.ts b/packages/bot/src/stoat/collections/UserCollection.ts index 846a5b8..04cdf6a 100644 --- a/packages/bot/src/stoat/collections/UserCollection.ts +++ b/packages/bot/src/stoat/collections/UserCollection.ts @@ -25,7 +25,10 @@ export class UserCollection extends BaseCollection { getOrCreate(id: string, data: any): User { const existing = this.get(id); - if (existing) return existing; + if (existing) { + this.updateUnderlyingObject(id, data); + return existing; + } this.underlying.set(id, { ...data }); diff --git a/packages/bot/src/stoat/ws.ts b/packages/bot/src/stoat/ws.ts index 61d862d..5d5e9bc 100644 --- a/packages/bot/src/stoat/ws.ts +++ b/packages/bot/src/stoat/ws.ts @@ -59,6 +59,7 @@ export class EventClient extends EventEmitter { } connect(uri: string, token: string, readyFields?: string[]) { + this.closed = true; this.disconnect(); this.closed = false; this.setState(ConnectionState.Connecting); @@ -192,7 +193,6 @@ export class EventClient extends EventEmitter { } disconnect() { - this.closed = true; this.cleanup(); const sock = this.socket; this.socket = undefined; -- 2.51.2