Skip to content

feat: resolve gateway structure relations through the cache with _add - #102

Merged
RedStar071 merged 2 commits into
feat/messagesfrom
feat/manager-add
Sep 25, 2026
Merged

RedStar071 merged 2 commits into
feat/messagesfrom
feat/manager-add

Conversation

@RedStar071

Copy link
Copy Markdown
Member

Stacked on #101.

🔗 Linked issue

Related to #54.

🧭 Context

Structures built their relations straight from the payload: message.author was new User(data.author), member.user was new User(data.user), and so on. discord.js does it differently. Its managers go through CachedManager#_add, which patches the cached entry, and message.author is the entry of client.users. So a user's latest known data shows up on every message they sent. Qjuh's @discordjs/next keeps the same _add, made asynchronous for a pluggable cache.

📚 Description

Every manager now has discord.js's _add and resolve:

  • _add(data, cache = true, { key }) shallowly merges the payload into the cached entry, like discord.js's _patch, so fields a partial payload lacks keep their cached value. It writes the result unless cache is false, then builds the structure. It's asynchronous, since the cache can be Redis. Every REST path that used to write the cache by hand now goes through it.
  • hydrate(data) builds a structure and resolves its relations from the cache. get, fetch, _add and the gateway events all use it, so a messageCreate message has the cached author and member.
  • resolve(structureOrKey) works like DataManager#resolve.
  • resolveData(data) returns the cached structure for a payload, or builds one from it. The relations use it.

The resolved relations are message.author, message.member, member.user, emoji.author, sticker.user, invite.inviter and invite.targetUser. Managers pass them to the constructor. A structure built by hand (new Message(data)) falls back to the copy in its payload, so structures still don't need a client.

Two choices worth a look:

  • Webhook authors are never written to client.users, following discord.js (_add(data.author, !data.webhook_id)). A webhook's author only holds for its own message.
  • A patch that carries the relation drops the resolved one, e.g. message.edit() returns a new author. The structure then reads the fresh payload instead of a user resolved earlier.

MessageMentions, DM recipients and Typing still read their payload. They are synchronous getters over embedded lists, and resolving them would make every access asynchronous.

no-underscore-dangle now allows _add in plugin-gateway. I kept the name on purpose for discord.js parity. It is @internal, so typedoc leaves it out.

Key changes

Area Change
CachedManager _add, hydrate, resolve, resolveData, abstract keyOf; storeRaw takes a key
Managers Private store helpers go through _add; members, messages, emojis, stickers and invites add their users
Structures Message, GuildMember, GuildEmoji, Sticker, BaseInvite take resolved relations
Gateway events Built with hydrate instead of createStructure

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Workflow / CI change

Pre-flight Checklist

  • I have read the Contributing Guidelines (if applicable)
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset (or pnpm changeset add --empty if no release is needed). See .changeset/README.md.

🤖 AI disclosure: Wolfstar Agent Kit modified this description. My AI open-source policy.

@coldtea-pr-lens

coldtea-pr-lens Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

◈ PR Lens

PR Lens paused analysis for this repository — the fair-use limit was reached. It resumes automatically at 2026-09-25T18:00:00.000Z.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one minor observation inline.

Reviewed changes

Reviewed the full diff porting discord.js's CachedManager#_add/DataManager#resolve pattern to plugin-gateway.

  • CachedManager grows _add, hydrate, resolve, resolveData, and an abstract keyOf: _add shallow-merges an API payload into the cached entry (like _patch) and writes it unless cache: false; hydrate builds a structure and resolves its relations from other entity caches; keyOf replaces the old Args-tuple-based storeRaw key derivation so relation resolution can derive a cache key from raw data alone.
  • Every manager's private store helper now goes through _add, and managers with relations (GuildEmojiManager, GuildStickerManager, GuildMemberManager, GuildInviteManager, MessageManager) override _add/hydrate to also write/resolve their nested user relation (uploader, member's user, inviter/target user, author/member).
  • Structures (Message, GuildMember, GuildEmoji, Sticker, BaseInvite) take an optional relations constructor param and reset the resolved field in [kPatch] when the raw payload repatches that relation — a deliberate, documented tradeoff (a patch carrying the relation "drops" the earlier resolved one).
  • Gateway dispatch handlers (util/dispatch.ts) build structures with hydrate instead of createStructure, relying on applyGatewayDispatch (unmodified, in @wolfstar/plugin-cache) having already written the dispatch's cache entries earlier in GatewayClient.handleDispatch.
  • Webhook message authors are deliberately excluded from client.users via cache && !data.webhook_id in MessageManager._add, and hydrate bypasses cache resolution for webhook authors entirely — covered by a new test.
  • New tests/cached-manager.test.ts covers merge-on-partial-payload, cache: false, no-cache-configured fallback, resolve, message author/member relation resolution, and webhook non-caching.

Verified locally: tsc --noEmit is clean, the full plugin-gateway suite passes (138/138, including the 8 new tests), and oxlint reports 0 issues. Traced applyGatewayDispatch's dispatch-time cache writes (MessageCreate, GuildMemberAdd/Update, etc.) to confirm hydrate's relation lookups see fresh data, and confirmed every CachedManager subclass implements the new abstract keyOf with call sites that can't hit GuildMemberManager.keyOf's userless-member throw.

ℹ️ Nitpicks

  • GuildEmojiManager/GuildStickerManager's _add writes the uploader to client.users, but the GuildEmojisUpdate/GuildStickersUpdate gateway path only calls hydrate (no _add), so an emoji/sticker uploader is cached to client.users on REST calls but not on that gateway event. Minor and likely matches how sparse that payload's user field already is in practice — not blocking.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Claude Sonnet | 𝕏

@pkg-pr-new

pkg-pr-new Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-api@102

@wolfstar/plugin-cache

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-cache@102

@wolfstar/plugin-gateway

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-gateway@102

@wolfstar/plugin-i18next

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-i18next@102

@wolfstar/plugin-logger

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-logger@102

@wolfstar/plugin-subcommands-advanced

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-subcommands-advanced@102

commit: 88b5ae2

@pullfrog

pullfrog Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an ANTHROPIC_API_KEY repo secret — Pullfrog routes around an exhausted subscription automatically when one is present.

Add repo secret → · Model settings → · Setup docs → · Ask in Discord →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using Claude Sonnet | 𝕏

@pullfrog

pullfrog Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an ANTHROPIC_API_KEY repo secret — Pullfrog routes around an exhausted subscription automatically when one is present.

Add repo secret → · Model settings → · Setup docs → · Ask in Discord →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using Claude Sonnet | 𝕏

@RedStar071
RedStar071 merged commit ba2423d into main Sep 25, 2026
18 of 25 checks passed
@RedStar071
RedStar071 deleted the feat/manager-add branch September 25, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant