-
Couldn't load subscription status.
- Fork 18
Description
This looks really interesting!
I am an Elixir noob who wrote his first GenServer last weekend, so this feedback may be very naive and stupid; please ignore it if so.
Although the API is very much simpler and more streamlined than the traditional GenServer, it still has a few moving parts and I wonder if they're totally necessary. From my reading of the readme, it sounds like there are exactly 3 things that you might want to do when you interact with a component:
- Update the state of the component
- Get a reply from the component
- Update the state and get a reply
With the current API, there are two things that the user needs to do to achieve these results:
- Choose between
one_wayandtwo_way(2 options) - Choose whether to use
set_state,set_state_and_return, or neither (3 options)
That gives you 6 possible combinations of things you could write, but only 4 of them will do what you want.
two_way -> OK (replies without updating the state)
two_way + set_state -> OK (updates the state and replies with the new state)
two_way + set_state_and_return -> OK (updates the state and replies with something else)
one_way -> OK (updates the state without replying)
one_way + set_state -> ?
one_way + set_state_and_return -> ?
Would it be possible/desirable to have an API that was more like this?
defmodule KV do
use Component.Strategy.Dynamic,
state_name: :kv_store,
initial_state: %{}
handle add(kv_store, key, value) do
%{state: Map.put(kv_store, key, value)}
end
handle get(kv_store, key) do
%{reply: Map.get(kv_store, key)}
end
handle merge_and_tell_me_the_new_value(kv_store, key, new_value) do
new_kv_store = Map.update(kv_store, key, new_value, fn old_value -> old_value + new_value end)
merged_value = Map.get(new_kv_store, key)
%{
state: new_kv_store,
reply: merged_value
}
end
end
That way, the user doesn't have to think about which type of function they're writing - they just need to specify what they want to get out of it - a new state, a reply, or both.