zotonic_notifier_worker (zotonic_notifier v1.0.0-rc.17)

Simple implementation of an observer/notifier. Relays events to observers of that event. Also implements map and fold operations over the observers.

Summary

Functions

Subscribe once to a notification, detach after receiving the notification.

Convert process state when code is changed

Unsubscribe an owner-pid from an event.

Detach all observers for the owner

Call all observers till one returns something else than undefined. The prototype of the observer is: f(Msg, Context)

Do a fold over all observers, prio 1 observers first. The prototype of the observer is: f(Msg, Acc, ContextArg)

Do a fold over all observers, prio 1 observers last

Return all observers

Return all observers for a particular event

Add an observer to an event

Trap unknown casts

Handling all non call/cast messages

Initiates the server, creates a new observer list

Call all observers, return the list of answers. The prototype of the observer is: f(Msg, ContextArg)

Cast the event to the first observer. The prototype of the observer is: f(Msg, Context) -> void

Cast the event to all observers. The prototype of the observer is: f(Msg, Context) -> void

Notify an observer of an event

Cast the event to all observers. The prototype of the observer is: f(Msg, Context) -> void

Subscribe to an event. Observer is a {M,F} or pid()

Starts the notification server

Start a notifier server for unit testing

Functions

await(Notifier, Event, Msg, Timeout)

-spec await(zotonic_notifier:notifier(), zotonic_notifier:event(), atom() | tuple(), pos_integer()) ->
               {ok, tuple() | atom()} | {ok, {pid(), reference()}, tuple() | atom()} | {error, timeout}.

Subscribe once to a notification, detach after receiving the notification.

await_exact(Notifier, Event, Msg, Timeout)

-spec await_exact(zotonic_notifier:notifier(), zotonic_notifier:event(), term(), pos_integer()) ->
                     {ok, term()} | {ok, {pid(), reference()}, term()} | {error, timeout}.

code_change(OldVsn, State, Extra)

-spec code_change(term(),
                  #state{name :: atom(), observers :: ets:tab(), monitors :: map(), pid2event :: map()},
                  term()) ->
                     {ok,
                      #state{name :: atom(),
                             observers :: ets:tab(),
                             monitors :: map(),
                             pid2event :: map()}}.

Convert process state when code is changed

detach(Notifier, Event, OwnerPid)

Unsubscribe an owner-pid from an event.

detach_all(Notifier, OwnerPid)

-spec detach_all(zotonic_notifier:notifier(), pid()) -> ok.

Detach all observers for the owner

first(Notifier, Event, Msg, ContextArg)

Call all observers till one returns something else than undefined. The prototype of the observer is: f(Msg, Context)

foldl(Notifier, Event, Msg, Acc0, ContextArg)

Do a fold over all observers, prio 1 observers first. The prototype of the observer is: f(Msg, Acc, ContextArg)

foldr(Notifier, Event, Msg, Acc0, ContextArg)

Do a fold over all observers, prio 1 observers last

get_observers(Notifier)

-spec get_observers(zotonic_notifier:notifier()) -> list().

Return all observers

get_observers(Notifier, Event)

-spec get_observers(zotonic_notifier:notifier(), zotonic_notifier:event()) -> list().

Return all observers for a particular event

handle_call(Message, From, State)

Add an observer to an event

handle_cast(Message, State)

Trap unknown casts

handle_info(Info, State)

Handling all non call/cast messages

init(Name)

-spec init(atom()) ->
              {ok,
               #state{name :: atom(), observers :: ets:tab(), monitors :: map(), pid2event :: map()}}.

Initiates the server, creates a new observer list

map(Notifier, Event, Msg, ContextArg)

Call all observers, return the list of answers. The prototype of the observer is: f(Msg, ContextArg)

notify1(Notifier, Event, Msg, ContextArg)

Cast the event to the first observer. The prototype of the observer is: f(Msg, Context) -> void

notify_async(Notifier, Event, Msg, ContextArg)

Cast the event to all observers. The prototype of the observer is: f(Msg, Context) -> void

notify_observer(Msg, _, IsCall, ContextArg)

Notify an observer of an event

notify_sync(Notifier, Event, Msg, ContextArg)

Cast the event to all observers. The prototype of the observer is: f(Msg, Context) -> void

observe(Notifier, Event, Observer, OwnerPid, Prio)

Subscribe to an event. Observer is a {M,F} or pid()

start_link(Name)

-spec start_link(Name :: atom()) -> gen_server:start_ret().

Starts the notification server

start_tests()

-spec start_tests() -> gen_server:start_ret().

Start a notifier server for unit testing

terminate(Reason, State)

-spec terminate(term(),
                #state{name :: atom(), observers :: ets:tab(), monitors :: map(), pid2event :: map()}) ->
                   ok.