From 39306e5b461023cb53d32c391f6004a8bda1bae4 Mon Sep 17 00:00:00 2001 From: Jacob Schlecht Date: Sat, 8 Aug 2026 17:50:58 -0600 Subject: [PATCH] fix: call post hydration Signed-off-by: Jacob Schlecht --- src/hydration/bot.ts | 2 +- src/hydration/channel.ts | 2 +- src/hydration/channelUnread.ts | 2 +- src/hydration/channelWebhook.ts | 2 +- src/hydration/emoji.ts | 2 +- src/hydration/index.ts | 61 +++++++++++++++++++++------------ src/hydration/message.ts | 2 +- src/hydration/server.ts | 2 +- src/hydration/serverMember.ts | 2 +- src/hydration/session.ts | 2 +- src/hydration/user.ts | 2 +- 11 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/hydration/bot.ts b/src/hydration/bot.ts index 556c8481..ea554311 100644 --- a/src/hydration/bot.ts +++ b/src/hydration/bot.ts @@ -31,7 +31,7 @@ export const botHydration: Hydrate = { privacy_policy_url: (bot) => ["privacyPolicyUrl", bot.privacy_policy_url!], flags: (bot) => ["flags", bot.flags!], }, - initialHydration: () => ({}), + postHydration: () => ({}), }; /** diff --git a/src/hydration/channel.ts b/src/hydration/channel.ts index a49d7697..9bc538c2 100644 --- a/src/hydration/channel.ts +++ b/src/hydration/channel.ts @@ -83,7 +83,7 @@ export const channelHydration: Hydrate, HydratedChannel> = { : undefined, ], }, - initialHydration: () => ({ + postHydration: () => ({ typingIds: new ReactiveSet(), recipientIds: new ReactiveSet(), }), diff --git a/src/hydration/channelUnread.ts b/src/hydration/channelUnread.ts index b6c492a6..102c8e3f 100644 --- a/src/hydration/channelUnread.ts +++ b/src/hydration/channelUnread.ts @@ -21,7 +21,7 @@ export const channelUnreadHydration: Hydrate< new ReactiveSet(unread.mentions!), ], }, - initialHydration: () => ({ + postHydration: () => ({ messageMentionIds: new ReactiveSet(), }), }; diff --git a/src/hydration/channelWebhook.ts b/src/hydration/channelWebhook.ts index 9de74e71..1ed6cfd4 100644 --- a/src/hydration/channelWebhook.ts +++ b/src/hydration/channelWebhook.ts @@ -27,5 +27,5 @@ export const channelWebhookHydration: Hydrate< channel_id: (webhook) => ["channelId", webhook.channel_id], token: (webhook) => ["token", webhook.token!], }, - initialHydration: () => ({}), + postHydration: () => ({}), }; diff --git a/src/hydration/emoji.ts b/src/hydration/emoji.ts index 193fef61..4a242d6d 100644 --- a/src/hydration/emoji.ts +++ b/src/hydration/emoji.ts @@ -20,5 +20,5 @@ export const emojiHydration: Hydrate = { animated: (emoji) => ["animated", emoji.animated || false], nsfw: (emoji) => ["nsfw", emoji.nsfw || false], }, - initialHydration: () => ({}), + postHydration: () => ({}), }; diff --git a/src/hydration/index.ts b/src/hydration/index.ts index d4ee84a4..bd3a4de5 100644 --- a/src/hydration/index.ts +++ b/src/hydration/index.ts @@ -29,7 +29,7 @@ export type KeyMapping = Record; */ export type Hydrate = { functions: MappingFns; - initialHydration: () => Partial; + postHydration: () => Partial; }; /** @@ -41,30 +41,46 @@ export type Hydrate = { function hydrateInternal( hydration: Hydrate, input: Input, + initial: boolean, context: unknown, ): Output { - return (Object.keys(input) as (keyof Input)[]).reduce((acc, key) => { - let targetKey: keyof Output | undefined; - let value: Output[keyof Output]; - try { - [targetKey, value] = hydration.functions[key](input, context); - } catch { - if (key === "partial") { - return { - ...acc, - partial: input["partial" as never], - }; + const hydrated = (Object.keys(input) as (keyof Input)[]).reduce( + (acc, key) => { + let targetKey: keyof Output | undefined; + let value: Output[keyof Output]; + try { + [targetKey, value] = hydration.functions[key](input, context); + } catch { + if (key === "partial") { + return { + ...acc, + partial: input["partial" as never], + }; + } + if (key === "type") return acc; + console.debug(`Skipping key ${String(key)} during hydration!`); + return acc; } - if (key === "type") return acc; - console.debug(`Skipping key ${String(key)} during hydration!`); - return acc; - } - return { - ...acc, - [targetKey]: value, - }; - }, {} as Output); + return { + ...acc, + [targetKey]: value, + }; + }, + {} as Output, + ); + + if (initial) { + const toAppend = hydration.postHydration(); + for (const k in toAppend) { + const val = toAppend[k]; + if (val !== undefined) { + hydrated[k] = val; + } + } + } + + return hydrated; } const hydrators = { @@ -103,7 +119,8 @@ export function hydrate( ): ExtractOutput { return hydrateInternal( hydrators[type] as never, - initial ? { ...hydrators[type].initialHydration(), ...input } : input, + input, + initial ?? false, context, ) as ExtractOutput; } diff --git a/src/hydration/message.ts b/src/hydration/message.ts index 251c8cae..c78932dc 100644 --- a/src/hydration/message.ts +++ b/src/hydration/message.ts @@ -77,7 +77,7 @@ export const messageHydration: Hydrate< pinned: (message) => ["pinned", message.pinned!], flags: (message) => ["flags", message.flags!], }, - initialHydration: () => ({ + postHydration: () => ({ reactions: new ReactiveMap(), }), }; diff --git a/src/hydration/server.ts b/src/hydration/server.ts index 5921bd63..edc44158 100644 --- a/src/hydration/server.ts +++ b/src/hydration/server.ts @@ -70,7 +70,7 @@ export const serverHydration: Hydrate = { discoverable: (server) => ["discoverable", server.discoverable || false], nsfw: (server) => ["nsfw", server.nsfw || false], }, - initialHydration: () => ({ + postHydration: () => ({ channelIds: new ReactiveSet(), roles: new ReactiveMap(), }), diff --git a/src/hydration/serverMember.ts b/src/hydration/serverMember.ts index 0dd7d03d..b690248f 100644 --- a/src/hydration/serverMember.ts +++ b/src/hydration/serverMember.ts @@ -31,7 +31,7 @@ export const serverMemberHydration: Hydrate< roles: (member) => ["roles", member.roles], timeout: (member) => ["timeout", new Date(member.timeout!)], }, - initialHydration: () => ({ + postHydration: () => ({ roles: [], }), }; diff --git a/src/hydration/session.ts b/src/hydration/session.ts index 57cd18d1..c7d0f380 100644 --- a/src/hydration/session.ts +++ b/src/hydration/session.ts @@ -12,5 +12,5 @@ export const sessionHydration: Hydrate = { _id: (server) => ["id", server._id], name: (server) => ["name", server.name], }, - initialHydration: () => ({}), + postHydration: () => ({}), }; diff --git a/src/hydration/user.ts b/src/hydration/user.ts index 8591cd1e..7a63aef1 100644 --- a/src/hydration/user.ts +++ b/src/hydration/user.ts @@ -50,7 +50,7 @@ export const userHydration: Hydrate = { status: (user) => ["status", user.status!], bot: (user) => ["bot", user.bot!], }, - initialHydration: () => ({ + postHydration: () => ({ relationship: "None", }), };