Sea v0.5.0 Sea.Signal behaviour View Source

Defines signal (aka. event) with payload that will get emitted to defined observers.

Link to this section Summary

Functions

Emits passed signal struct to observers defined in the struct module

Adds observer module(s) that signal will be emitted to

Callbacks

Build the signal struct from arbitrary input (or return it if already a signal struct)

Emit the signal from arbitrary input (converted to signal struct if necessary)

Link to this section Functions

Emits passed signal struct to observers defined in the struct module.

Link to this macro emit_to(observer_mod_or_mods) View Source (macro)

Adds observer module(s) that signal will be emitted to.

It may be invoked multiple times, but specific observer may be added only once.

Example

Emit to single observer:

defmodule MainApp.Accounts.UserRegisteredSignal do
  use Sea.Signal

  emit_to AnalyticsApp.Observer
  emit_to CustomerExperienceApp.Observer
end

Emit to multiple observers specified as an array:

defmodule MainApp.Accounts.UserRegisteredSignal do
  use Sea.Signal

  emit_to [
    MainApp.Mailing.UserRegisteredObserver,
    MainApp.Sales.UserRegisteredObserver
  ]
end

Emit to multiple observers specified with the multi-alias syntax:

defmodule MainApp.Accounts.UserRegisteredSignal do
  use Sea.Signal

  emit_to CustomerExperienceApp.{Mailing, Sales}
end

Examples above present multiple approaches to organizing observers. Please refer to Organizing observers guide for complete explanation and examples on how to approach this problem.

Link to this section Callbacks

Build the signal struct from arbitrary input (or return it if already a signal struct).

Sea provides the default implementation that will simply return the signal struct if it’s provided as argument. Specific signal module may define further variants of build capable of taking any input and converting it to signal payload.

Emit the signal from arbitrary input (converted to signal struct if necessary).

Sea provides its implementation of emit that will call build with whatever is passed to it in order to normalize the input into the signal struct and then it’ll call Sea.Signal.emit/1 with that in order to actually call defined observers.