automata
automata copied to clipboard
Handle user update asynchronously
Find a way to pass a function to state in user-defined update
function so that the update can be run async as a Task so subsequent ticks will return :bh_running until the task completes (which sets the resulting status). This makes the agent reactive. Or is there a better more elixir way to do this? I'm thinking similar to this:
def handle_cast({:run, fun}, state) do
Task.async(fun) # sends a message back to the automaton when completed
{:noreply, state}
end
# handle_info/2 receives generic messages from the Task processes
def handle_info({_task, {:ok, result}}, state) do
new_state = %{state | status: result}
{:noreply, new_state}
end
def handle_info({_task, {:error, reason}}, state) do
state = %{state | status: :bh_failure, fail_reason: reason}
{:noreply, state}
end