Skip to content

Instantly share code, notes, and snippets.

@rambocoder
Forked from Licenser/gen_server_consumer.erl
Created March 22, 2012 19:41
Show Gist options
  • Save rambocoder/2162749 to your computer and use it in GitHub Desktop.
Save rambocoder/2162749 to your computer and use it in GitHub Desktop.
process(Data) ->
gen_server:cast(?SERVER, {process, Data});
...
handle_cast({process, Data}, {}) ->
do_some_funky_stuff(Data),
reader:pop(self);
{ok, {}}
pop(Reader) ->
gen_server:cast(?SERVER, {pop, Reader});
push(Data) ->
gen_server:cast(?SERVER, {push, Data});
...
handle_cast({pop, Reader}, {[], Readers}) ->
{ok, {[], [Reader | Readers]}};
handle_cast({pop, Reader}, {[Data | R], []}) ->
consumer:process(Reader, Data),
{ok, {R, []}};
handle_cast({pop, Reader}, {[Data | R], [First | Rdrs]}) ->
consumer:process(First, Data),
{ok, {R, [Reader | Rdrs]}};
handle_cast({push, Data}, {Dates, []}) ->
{ok, {[Data| Dates], []}};
handle_cast({push, Data}, {[], [First | Rdrs]}) ->
consumer:process(First, Data),
{ok, {[], Rdrs}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment