Drafter.Event.Manager (drafter v0.3.2)

Copy Markdown View Source

Fans events out from the terminal driver to the processes that want them.

One manager exists per session. Producers cast events to it; it delivers each one to every subscriber whose filter accepts it, as the message {:tui_event, event}. The event shapes are the ones listed in Drafter.Event.

Drafter.Event.Manager.subscribe(self(), &Drafter.Event.key_event?/1)
receive do
  {:tui_event, {:key, key}} -> key
end

A subscriber is monitored and dropped when it exits, so unsubscribing on shutdown is not required. Subscribing the same pid twice replaces its filter.

The :app_pid given at start receives every deliverable event regardless of the subscriber list.

{:bracketed_paste, _} is withheld unless Drafter.Clipboard.paste_enabled?/0 returns true; every other event is always deliverable.

Functions without an explicit manager argument resolve it through Drafter.Session.Context under the :event_manager key, so they address the caller's own session.

Summary

Functions

Returns a specification to start this module under a supervisor.

Always returns :ok immediately. Retained for callers in the runtime loop.

Deliver event to this session's subscribers.

Deliver each of events in order to this session's subscribers.

Start a manager.

Subscribe subscriber_pid to this session's manager.

Subscribe subscriber_pid to manager, for a manager that is not registered under this module's name.

Returns once every event cast before this call has been dispatched.

As sync/0, for a manager that is not globally registered.

Stop delivering events to subscriber_pid, defaulting to the calling process.

Types

event_filter()

@type event_filter() :: (Drafter.Event.t() -> boolean()) | :all

subscriber()

@type subscriber() :: pid()

subscription()

@type subscription() :: {subscriber(), event_filter()}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

drain_queue()

@spec drain_queue() :: :ok

Always returns :ok immediately. Retained for callers in the runtime loop.

send_event(event)

@spec send_event(Drafter.Event.t()) :: :ok

Deliver event to this session's subscribers.

Asynchronous; use sync/0 to wait for delivery.

send_events(events)

@spec send_events([Drafter.Event.t()]) :: :ok

Deliver each of events in order to this session's subscribers.

Asynchronous; use sync/0 to wait for delivery.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start a manager.

Options:

  • :name — registered name, default Drafter.Event.Manager. Pass nil to start it unregistered, which is what a session other than the local terminal does.
  • :app_pid — a process that receives every deliverable event without subscribing.

subscribe(subscriber_pid \\ self(), filter \\ :all)

@spec subscribe(pid(), event_filter()) :: :ok

Subscribe subscriber_pid to this session's manager.

Defaults to the calling process and to the :all filter. See subscribe_to/3 for the filter contract.

subscribe_to(manager, subscriber_pid, filter \\ :all)

@spec subscribe_to(pid() | atom(), pid(), event_filter()) :: :ok

Subscribe subscriber_pid to manager, for a manager that is not registered under this module's name.

filter is :all (the default) or a one-argument function returning true for the events to deliver. A filter that raises is treated as no match. The subscriber is monitored; a second subscription for the same pid replaces its filter.

sync()

@spec sync() :: :ok

Returns once every event cast before this call has been dispatched.

A synchronous round trip through the manager, so it orders after earlier casts from the same caller. Call it before observing a subscriber's state to be sure the events sent to it have been delivered.

sync(manager)

@spec sync(pid() | atom()) :: :ok

As sync/0, for a manager that is not globally registered.

unsubscribe(subscriber_pid \\ self())

@spec unsubscribe(pid()) :: :ok

Stop delivering events to subscriber_pid, defaulting to the calling process.

Unnecessary when the subscriber is exiting; the monitor removes it.