- I coded up the interface functions for the megaphone module.
- I coded up the get_data function
- I coded up the put_data function
Showing my usual decisive nature, I decided to change my mind about some of the code I had written. Specifically, the put_data function. The new version looks like this:
put_data(State, ConnectionID, Data) ->
NewState = update_table(State, ConnectionID, Data),
{ Result, NewerState } = remove_waiter(ConnectionID, State),
case Result of
undefined -> NewerState;
Waiter ->
notify_waiter(Waiter, ConnectionID, State)
end.
Note that get_waiter_for has been replaced with remove_waiter. In addition to ending with a noun instead of a preposition, the new function returns an updated version of the module state. This is because it changes the state of the module. By removing the first waiter from the list of waiters for the connection. Hence the name. Yeah.
While I was tempted to calling it quits for this post after changing a few lines in one source file, in a burst of creativity I also wrote the remove_waiter function:
remove_waiter(ConnectionID, State) ->
Table = State#megaphone_main_state.connectionToResults,
case dict:is_key(ConnectionID, Table) of
false -> { undefined, State};
true ->
NewTable = case dict:fetch(ConnectionID, Table) of
[ Waiter | Rest ] ->
dict:store(ConnectionID, Rest, Table);
[] ->
dict:erase(ConnectionID, Table),
end,
NewState = State#megaphone_main_state{connectionToResults = NewTable},
{ Waiter, NewState }
end.
I know, the excitement is enough to bowl you over. I'm gonna go take a nap now.
Next time, more of the megaphone module internals.
No comments:
Post a Comment