Polls

A Poll instance holds the reactive state of a poll.

Initialization

Polls of loaded messages are added automatically by channel.watch(), client.queryChannelsAndHydrate() and client.queryThreadsAndHydrate(). Call registerSubscriptions() once, so polls follow votes and updates.

client.polls.registerSubscriptions();

// A poll that isn't loaded yet: fetches it
const poll = await client.polls.getPoll(pollId);

Reading

// A loaded poll, `undefined` if it isn't loaded
const pollInstance = client.polls.fromState(pollId);

const { name, options, vote_counts_by_option, ownVotesByOptionId, is_closed } =
  pollInstance?.state.getLatestValue() ?? {};

Subscribe to render changes:

const unsubscribe = pollInstance?.state.subscribeWithSelector(
  (state) => ({ vote_counts_by_option: state.vote_counts_by_option }),
  ({ vote_counts_by_option }) => renderVotes(vote_counts_by_option),
);