Noizu.Discord.Pagination (Noizu.Discord v0.1.0)

Copy Markdown View Source

Lazy snowflake-cursor walker behind every *_stream helper.

Discord list endpoints page by before/after snowflake ids rather than opaque cursors. stream/2 calls fetch.(cursor) (cursor is nil for the first page, then the next snowflake id), emits each item, and stops when a page is empty or shorter than :limit.

A failed page fetch raises the %Noizu.Discord.Error{} (an enumerable cannot return an error mid-stream); rescue at the enumeration site if desired.

Options

  • :direction — :after (default; next cursor = largest id on the page) or :before (next cursor = smallest id).
  • :limit — page size requested; a shorter page ends the stream.
  • :id_fun — extracts the snowflake from an item (default & &1["id"]).
  • :start — initial cursor (default nil).

Summary

Functions

Snowflake ordering helper: compares ids numerically.

Functions

snowflake_compare(a, b)

@spec snowflake_compare(String.t(), String.t()) :: :lt | :eq | :gt

Snowflake ordering helper: compares ids numerically.

stream(fetch, opts \\ [])

@spec stream(
  (String.t() | nil -> {:ok, list()} | {:error, Noizu.Discord.Error.t()}),
  keyword()
) ::
  Enumerable.t()