W3cubDocs

/Elixir 1.11

GenEvent behaviour

This behaviour is deprecated. Use Erlang/OTP's :gen_event module instead.

A event manager with event handlers behaviour.

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.

Summary

Types

Callbacks

Types

handler()

Specs

handler() :: atom() | {atom(), term()}

manager()

Specs

manager() :: pid() | name() | {atom(), node()}

name()

Specs

name() :: atom() | {:global, term()} | {:via, module(), term()}

on_start()

Specs

on_start() :: {:ok, pid()} | {:error, {:already_started, pid()}}

options()

Specs

options() :: [{:name, name()}]

Callbacks

code_change(old_vsn, state, extra)

Specs

code_change(old_vsn, state :: term(), extra :: term()) ::
  {:ok, new_state :: term()}
when old_vsn: term() | {:down, term()}

handle_call(request, state)

Specs

handle_call(request :: term(), state :: term()) ::
  {:ok, reply, new_state}
  | {:ok, reply, new_state, :hibernate}
  | {:remove_handler, reply}
when reply: term(), new_state: term()

handle_event(event, state)

Specs

handle_event(event :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()

handle_info(msg, state)

Specs

handle_info(msg :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()

init(args)

Specs

init(args :: term()) ::
  {:ok, state} | {:ok, state, :hibernate} | {:error, reason :: any()}
when state: any()

terminate(reason, state)

Specs

terminate(reason, state :: term()) :: term()
when reason:
       :stop | {:stop, term()} | :remove_handler | {:error, term()} | term()

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.11.2/GenEvent.html