GenNotify v0.1.2 GenNotify behaviour View Source

This is the main module that you will come in contact with.

you can use this module to inherit its functionality.

Example

defmodule MyNotification do
  use GenNotify

  def on_message(msg) do
    IO.puts("I got a message: #{msg}")
  end
end

To get up and running we do need to make sure that our Notifier is started.

GenNotify.Notifier.start_link()

Our Custom Notification has to be initialized at some point after GenNotify.Notifier. This will tell the Notifier to watch this Module

MyNotification.gen_notify_init()

somewhere else in the code...

GenNotify.send_notification("im a message") # => This will cause MyNotification.on_message/1 to be called

Example - GenServer

Sometimes a notification for a module isn't quite what you want. You want notifications for a specific Process. GenNotify does support GenServer

defmodule MyGenServer do
  use GenServer
  use GenNotify, server: true # we need to tell GenNotify that this is indeed is a server

  def start_link(_) do
    GenServer.start_link(__MODULE__, :ok)
  end

  def init(_) do
    gen_notify_init() # => This will supply our PID to GenNotify.Notifier (Keep in mind, GenNotify.Notifier already needs to be alive!)
    {:ok, %{}}
  end

  def on_message(msg) do
    IO.puts(msg) # => will be "im a message" in our example
  end
end

somewhere else in the code...

GenNotify.send_notification("im a message")

Link to this section Summary

Link to this section Types

Link to this type

recipient()

View Source
recipient() :: atom() | pid()

Link to this section Functions

Link to this section Callbacks

Link to this callback

on_message(msg)

View Source
on_message(msg()) :: any()