fix: call post hydration

Signed-off-by: Jacob Schlecht <dadadah@echoha.us>
This commit is contained in:
Jacob Schlecht
2026-08-08 17:50:58 -06:00
parent de54aee976
commit 39306e5b46
11 changed files with 49 additions and 32 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ export const botHydration: Hydrate<APIBot, HydratedBot> = {
privacy_policy_url: (bot) => ["privacyPolicyUrl", bot.privacy_policy_url!],
flags: (bot) => ["flags", bot.flags!],
},
initialHydration: () => ({}),
postHydration: () => ({}),
};
/**
+1 -1
View File
@@ -83,7 +83,7 @@ export const channelHydration: Hydrate<Merge<APIChannel>, HydratedChannel> = {
: undefined,
],
},
initialHydration: () => ({
postHydration: () => ({
typingIds: new ReactiveSet(),
recipientIds: new ReactiveSet(),
}),
+1 -1
View File
@@ -21,7 +21,7 @@ export const channelUnreadHydration: Hydrate<
new ReactiveSet(unread.mentions!),
],
},
initialHydration: () => ({
postHydration: () => ({
messageMentionIds: new ReactiveSet(),
}),
};
+1 -1
View File
@@ -27,5 +27,5 @@ export const channelWebhookHydration: Hydrate<
channel_id: (webhook) => ["channelId", webhook.channel_id],
token: (webhook) => ["token", webhook.token!],
},
initialHydration: () => ({}),
postHydration: () => ({}),
};
+1 -1
View File
@@ -20,5 +20,5 @@ export const emojiHydration: Hydrate<APIEmoji, HydratedEmoji> = {
animated: (emoji) => ["animated", emoji.animated || false],
nsfw: (emoji) => ["nsfw", emoji.nsfw || false],
},
initialHydration: () => ({}),
postHydration: () => ({}),
};
+39 -22
View File
@@ -29,7 +29,7 @@ export type KeyMapping<Input, Output> = Record<keyof Input, keyof Output>;
*/
export type Hydrate<Input, Output> = {
functions: MappingFns<Input, Output, keyof Input, keyof Output>;
initialHydration: () => Partial<Output>;
postHydration: () => Partial<Output>;
};
/**
@@ -41,30 +41,46 @@ export type Hydrate<Input, Output> = {
function hydrateInternal<Input extends object, Output>(
hydration: Hydrate<Input, Output>,
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<T extends keyof Hydrators>(
): ExtractOutput<Hydrators[T]> {
return hydrateInternal(
hydrators[type] as never,
initial ? { ...hydrators[type].initialHydration(), ...input } : input,
input,
initial ?? false,
context,
) as ExtractOutput<Hydrators[T]>;
}
+1 -1
View File
@@ -77,7 +77,7 @@ export const messageHydration: Hydrate<
pinned: (message) => ["pinned", message.pinned!],
flags: (message) => ["flags", message.flags!],
},
initialHydration: () => ({
postHydration: () => ({
reactions: new ReactiveMap(),
}),
};
+1 -1
View File
@@ -70,7 +70,7 @@ export const serverHydration: Hydrate<APIServer, HydratedServer> = {
discoverable: (server) => ["discoverable", server.discoverable || false],
nsfw: (server) => ["nsfw", server.nsfw || false],
},
initialHydration: () => ({
postHydration: () => ({
channelIds: new ReactiveSet(),
roles: new ReactiveMap(),
}),
+1 -1
View File
@@ -31,7 +31,7 @@ export const serverMemberHydration: Hydrate<
roles: (member) => ["roles", member.roles],
timeout: (member) => ["timeout", new Date(member.timeout!)],
},
initialHydration: () => ({
postHydration: () => ({
roles: [],
}),
};
+1 -1
View File
@@ -12,5 +12,5 @@ export const sessionHydration: Hydrate<APISession, HydratedSession> = {
_id: (server) => ["id", server._id],
name: (server) => ["name", server.name],
},
initialHydration: () => ({}),
postHydration: () => ({}),
};
+1 -1
View File
@@ -50,7 +50,7 @@ export const userHydration: Hydrate<APIUser, HydratedUser> = {
status: (user) => ["status", user.status!],
bot: (user) => ["bot", user.bot!],
},
initialHydration: () => ({
postHydration: () => ({
relationship: "None",
}),
};