Elixir v1.6.6 GenEvent behaviour View Source

WARNING: this module is deprecated.

If you are interested in implementing an event manager, please read the “Alternatives” section below. If you have to implement an event handler to integrate with an existing system, such as Elixir’s Logger, please use :gen_event instead.

Alternatives

There are a few suitable alternatives to replace GenEvent. Each of them can be the most beneficial based on the use case.

Supervisor and GenServers

One alternative to GenEvent is a very minimal solution consisting of using a supervisor and multiple GenServers started under it. The supervisor acts as the “event manager” and the children GenServers act as the “event handlers”. This approach has some shortcomings (it provides no backpressure for example) but can still replace GenEvent for low-profile usages of it. This blog post by José Valim has more detailed information on this approach.

GenStage

If the use case where you were using GenEvent requires more complex logic, GenStage provides a great alternative. GenStage is an external Elixir library maintained by the Elixir team; it provides a tool to implement systems that exchange events in a demand-driven way with built-in support for backpressure. See the GenStage documentation for more information.

:gen_event

If your use case requires exactly what GenEvent provided, or you have to integrate with an existing :gen_event-based system, you can still use the :gen_event Erlang module.

Link to this section Summary

Link to this section Types

Link to this type handler() View Source
handler() :: atom() | {atom(), term()}
Link to this type manager() View Source
manager() :: pid() | name() | {atom(), node()}
Link to this type name() View Source
name() :: atom() | {:global, term()} | {:via, module(), term()}
Link to this type on_start() View Source
on_start() :: {:ok, pid()} | {:error, {:already_started, pid()}}
Link to this type options() View Source
options() :: [{:name, name()}]

Link to this section Callbacks

Link to this callback code_change(old_vsn, state, extra) View Source
code_change(old_vsn, state :: term(), extra :: term()) ::
  {:ok, new_state :: term()}
when old_vsn: term() | {:down, term()}
Link to this callback handle_call(request, state) View Source
handle_call(request :: term(), state :: term()) ::
  {:ok, reply, new_state}
  | {:ok, reply, new_state, :hibernate}
  | {:remove_handler, reply}
when reply: term(), new_state: term()
Link to this callback handle_event(event, state) View Source
handle_event(event :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()
Link to this callback handle_info(msg, state) View Source
handle_info(msg :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()
Link to this callback init(args) View Source
init(args :: term()) ::
  {:ok, state} | {:ok, state, :hibernate} | {:error, reason :: any()}
when state: any()
Link to this callback terminate(reason, state) View Source
terminate(reason, state :: term()) :: term()
when reason:
       :stop | {:stop, term()} | :remove_handler | {:error, term()} | term()