Editing
GenEvent
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== GenEvent == [[File:GenEvent.png|thumb|right|GenEvent Elixir]] '''GenEvent''' is a behavior module in the [[Elixir (programming language)|Elixir]] programming language that allows for event-driven programming. With GenEvent, users can implement generic event handling modules that can be subscribed to by multiple processes. === Usage === To define and use a GenEvent module, the following steps can be followed: 1. First, define the behavior by using the `defmodule` directive with the `@behaviour` attribute: ```elixir defmodule MyEventModule do @behaviour GenEvent ... end ``` 2. Implement the callback functions required by the GenEvent behavior: ```elixir def handle_event(state, event, acc) do # handle the event ... {:ok, new_state} end def handle_call(state, event, acc) do # handle synchronous call ... {:reply, reply, new_state} end ``` 3. Create a GenEvent process by calling `GenEvent.start_link(module, args)`: ```elixir {:ok, pid} = GenEvent.start_link(MyEventModule, initial_state) ``` 4. Subscribe interested processes to the GenEvent by using `GenEvent.add_handler`: ```elixir GenEvent.add_handler(pid, handler) ``` 5. Trigger an event by invoking `GenEvent.notify`: ```elixir GenEvent.notify(pid, event) ``` === Example === Here is an example that illustrates the usage of GenEvent: ```elixir defmodule MyEventModule do @behaviour GenEvent def handle_event(state, event, acc) do IO.puts("Handling event: #{event}") {:ok, state} end def handle_call(state, event, acc) do IO.puts("Handling synchronous call: #{event}") {:reply, event * 2, state} end end {:ok, pid} = GenEvent.start_link(MyEventModule, {}) GenEvent.add_handler(pid, self()) GenEvent.notify(pid, :my_event) reply = GenEvent.call(pid, :my_synchronous_event) IO.puts("Received reply from event: #{reply}") ``` === See Also === * [[Elixir (programming language)|Elixir]] * [[Event-driven programming]] * [[Processes (Elixir)]] === External Links === * [https://hexdocs.pm/elixir/GenEvent.html GenEvent Documentation]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information