Sorcery.LiveHelpers behaviour (sorcery v0.4.4)

Functions for combining Sorcery and LiveViews

Summary

Callbacks

Puts a :sorcery key in socket.assigns, with a bunch of stuff used behind the scenes by Sorcery.

Get a list of entities at the given lvar.

Get a list of entities at the given portal/lvar You probably want to use this inside heex

Creates a portal between the LiveView and the PortalServer, using the given arguments.

Types

Link to this type

socket_type()

@type socket_type() :: %Phoenix.LiveView.Socket{
  optional(any()) => any(),
  assigns: map()
}

Callbacks

Link to this callback

initialize_sorcery(socket, body)

@callback initialize_sorcery(
  socket :: socket_type(),
  body :: %{
    optional(:sorcery_module) => module(),
    optional(:args) => map(),
    optional(:store_adapter) => module()
  }
) :: socket_type()

Puts a :sorcery key in socket.assigns, with a bunch of stuff used behind the scenes by Sorcery.

This is mandatory, before you can spawn any portals

You must pass in the sorcery_module. If you used the generator, it'll just be Src.

Examples

iex> body = %{sorcery_module: Src}
iex> socket = initialize_sorcery(socket, body)
Link to this callback

portal_view(sorcery_config, lvar)

@callback portal_view(sorcery_config :: map(), lvar :: binary()) :: list()

Get a list of entities at the given lvar.

If this lvar only exists in ONE portal, you can leave out the portal_name, just know that it might be imperceptibly slower since the function needs to iterate over all portals until it finds one with that lvar..

<% players = portal_view(@sorcery, "?all_players") %>
Link to this callback

portal_view(sorcery_config, portal_name, lvar)

@callback portal_view(sorcery_config :: map(), portal_name :: atom(), lvar :: binary()) ::
  list()

Get a list of entities at the given portal/lvar You probably want to use this inside heex

<% players = portal_view(@sorcery, :my_portal, "?all_players") %>
Link to this callback

spawn_portal(socket, body)

@callback spawn_portal(
  socket :: socket_type(),
  body :: %{
    portal_server: module(),
    portal_name: atom(),
    query_module: module(),
    query_args: map()
  }
) :: socket_type()

Creates a portal between the LiveView and the PortalServer, using the given arguments.

Takes the socket, returns it unchanged.

Examples

iex> body = %{portal_server: Postgres, portal_name: :my_portal, query_module: MyQuery, query_args: %{player_id: 1}}
iex> socket = spawn_portal(socket, body)