Last active
December 21, 2015 07:08
-
-
Save ToJans/6269026 to your computer and use it in GitHub Desktop.
An attempt for a more succint event handler in elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Listen.NewFSM do | |
use OtpDsl.Genfsm, register: {:local, :listen_fsm}, | |
initial_state: :start, | |
init_params: [] | |
@timeout 3*1000 | |
defrecord CallInfo, from: "", to: "", suspicious_segments: 0 | |
defstate start do | |
defhandler call_initiated(from,to) do | |
IO.puts "Initiating a call from #{from} to #{to}" | |
emit listening(CallInfo.new(from: from, to: to)) | |
end | |
end | |
defstate listening do | |
defhandler hang_up do | |
debug("Hangup", context) | |
emit start | |
end | |
defhandler suspicious_phrase_heard do | |
debug("Heard something suspicious", context) | |
emit transcribing(context.update_suspicious_segments(&1+1), @timeout) | |
end | |
end | |
defstate transcribing do | |
defhandler hang_up do | |
debug("Report on call", context) | |
emit start(CallData.new) | |
end | |
defhandlerp timeout do | |
emit listening(context) | |
end | |
end | |
# Helpers | |
defp debug(msg, CallInfo[from: from, to: to, suspicious_segments: suspicious_segments]) do | |
IO.puts("Call from #{from} to #{to}: #{msg}") | |
if suspicious_segments > 0 do | |
IO.puts(" (suspicious_segments: #{suspicious_segments})") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added the
emit
keyword to emit state transitions