What is Flux?
Flux is how Discord manages its state. Its dispatcher sends Flux actions, such asMESSAGE_CREATE when a message arrives. Flux stores, such as UserStore, handle those actions and hold the resulting state.
The flux namespace reads stores, listens for actions, and sends actions. Each listener function returns a function that removes the listener. In a plugin, every listener is also removed when the plugin stops, so stop doesn’t have to remove them.
Reading a store
getStore(name) finds a store by its name without the Store suffix. It’s lazy: the search runs the first time you read from the store, so it’s safe at the top of a file.
string
required
The store name, such as
'User' or 'SelectedChannel'.boolean
default:"true"
Pass
false to search right away and get the real store instead of a proxy.boolean
default:"true"
Appends
Store to the name before matching. Pass false to match the full name.User, Guild, GuildMember, Channel, SelectedChannel, Typing, Relationship, UserProfile and GuildSettings. Any other name returns the base FluxStore type, which has getName, addChangeListener, removeChangeListener and emitChange. To type another store, see Typing your own stores and actions.
Reading a store in a component
useStore(stores, getter, deps?) runs getter and re-renders the component when any of the stores changes.
Watching a store
onStoreChange(store, listener) runs listener after each change to a store.
Listening for actions
onAction(type, listener) runs listener for each action of one type, after Discord’s stores have handled it. The action’s payload is typed for the known action types.
Intercepting actions
interceptAction(type, interceptor) runs interceptor before Discord handles each action of one type. The interceptor can change the action in place. Return true to drop the action, so no store sees it.
Sending an action
dispatch(action) sends an action through Discord’s dispatcher. It returns a promise that settles once Discord’s stores have handled the action. For a known type, the payload is type-checked.
Typing your own stores and actions
FluxStores and FluxActions are interfaces, so you can add your own entries with a declare module block. getStore, onAction, interceptAction and dispatch then use your types.