Topical (topical v0.3.1)

This module provides the high level interface for interacting with topics. Primarily for subscribing (and unsubscribing), but also for sending requests.

After subscribing, a client will initially receive a {:reset, ref, value} message, and then subsequent {:updates, ref, updates} messages when the value of the topic changes, where updates is a list with each item being one of:

  • {:set, path, value}: the value has been set at the path.
  • {:unset, path, key}: the key has been unset from the object at the path.
  • {:insert, path, index, values}: the values have been inserted into the array at the path.
  • {:delete, path, index, count}: count values have been deleted from the array at the path, from the position index.

A client can interact directly with a topic by executing actions (which returns a result), or by notifying (without waiting for a result). These are analogous to GenServer.call/3 and GenServer.cast/2. Be aware that a topic is blocked while processing a request.

Link to this section Summary

Functions

Captures the state of the topic (in the specified registry) without subscribing.

Returns a specification to start a Topical registry under a supervisor.

Subscribes to the specified topic (in the specified registry).

Unsubscribes from a topic.

Link to this section Functions

Link to this function

capture(registry, topic, context \\ nil, params \\ %{})

Captures the state of the topic (in the specified registry) without subscribing.

options

Options

  • params - Optional map of params to pass to the topic (default: %{}).

example

Example

Topical.capture(MyApp.Topical, ["lists", "foo"])
# => {:ok, %{items: %{}, order: []}}
Link to this function

child_spec(options)

Returns a specification to start a Topical registry under a supervisor.

Link to this function

execute(registry, topic, action, args \\ {}, context \\ nil, params \\ %{})

Executes an action in a topic.

options

Options

  • params - Optional map of params to pass to the topic (default: %{}).

example

Example

Topical.execute(MyApp.Topical, ["lists", "foo"], "add_item", {"Test", false})
#=> {:ok, "item123"}
Link to this function

notify(registry, topic, action, args \\ {}, context \\ nil, params \\ %{})

Send a notification to a registry.

This is similar to execute/4, except no result is waited for.

options

Options

  • params - Optional map of params to pass to the topic (default: %{}).

example

Example

Topical.notify(MyApp.Topical, ["lists", "foo"], "update_done", {"item123", true})
#=> :ok
Link to this function

subscribe(registry, topic, pid, context \\ nil, params \\ %{})

Subscribes to the specified topic (in the specified registry).

Returns {:ok, ref, server}, where ref is a reference to the subscription and server is the topic server PID (used for unsubscribing).

The pid will be sent messages, as described above.

options

Options

  • params - Optional map of params to pass to the topic (default: %{}). These are merged with route params and passed to connect/2 and init/1. Topics with different param values are separate instances and do not share state.

example

Example

Topical.subscribe(MyApp.Topical, ["lists", "foo"], self())
#=> {:ok, #Reference<0.4021726225.4145020932.239110>, #PID<0.123.0>}
Link to this function

unsubscribe(server, ref)

Unsubscribes from a topic.

Takes the server PID and ref returned from subscribe/5.

example

Example

{:ok, ref, server} = Topical.subscribe(MyApp.Topical, ["lists", "foo"], self())
Topical.unsubscribe(server, ref)