diamondburned / arikawa

A Golang library and framework for the Discord API.
ISC License
469 stars 55 forks source link

v4: Automatically emit Gateway commands for State wrappers #400

Open diamondburned opened 1 year ago

diamondburned commented 1 year ago

Background

Currently, state.X methods will check the cabinet for any gateway-received data, and if none is seen, it'll fall back to checking the API and storing the API response into the cabinet.

A major issue with this approach is cache stateless: the stored item is effectively never renewed because Discord never knew to send updates over the (stateless) gateway. Ideally, the state function should let Discord know of this.

Idea

State methods should send a command over to the gateway whenever it can to notify that the returned item should be kept updated. For example, State.Member could do the following:

member = cabinet.Member()
if member:
    return member

reply = session.Send(RequestGuildMembersCommand)
member = await (session.WaitFor(reply) or api.Member())
return member

Specifically for RequestGuildMembersCommand, some debouncing will be required: within duration d (e.g. 250ms), collect all interested member IDs for a guild, assign it a nonce, then try to wait for either a reply for that nonce or for the given context to expire. The state cache will automatically pick up unhandled events and load them into the state cache.