anchor.diff:  -behaviour(gen_server).
   
   %% API
  --export([start_link/2, build/1, emit/3, done/1, start/2]).
  +-export([start_link/3, build/1, emit/3, done/1, start/2]).
   
   %% gen_server callbacks
   -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  
positive.code: %%                                   {stop, Reason, State}
  %% @end
  %%--------------------------------------------------------------------
  handle_call(_Request, _From, State) ->
      Reply = ok,
      {reply, Reply, State}.
  
negative.code: %%                     {stop, Reason}
  %% @end
  %%--------------------------------------------------------------------
  init([{PRef, Parent}, {Module, Args}, Queries]) ->
      {ok, CState, SubQs} = Module:init(Args),
      {Queries1, Children} =
          lists:foldl(
            fun ({Ref, Query}, {QAcc, CAcc}) ->
                    case dict:find(Query, QAcc) of
                        error ->
                            {ok, Pid} = start_link({Ref, self()}, Query, QAcc),
                            receive
                                {queries, Ref, QAcc1} ->
                                    QAcc2 = dict:store(Query, Pid, QAcc1),
                                    {QAcc2, [{Ref, Pid} | CAcc]}
                            after
                                1000 ->
                                    error(timeout)
                            end;
                        {ok, Pid} ->
                            add_parent(Pid, Ref),
                            {QAcc, [{Ref, Pid} | CAcc]}
                    end
            end, {Queries, []}, ensure_refed(SubQs, [])),
      Parent ! {queries, PRef, Queries1},
      {ok, #state{
              callback_module = Module,
              callback_state = CState,
              parents = [{PRef, Parent}],
              children = Children
             }}.
  
  