 defmodule Genomu.Channel do
   use GenServer.Behaviour
   import GenX.GenServer
+
+  require Lager
 
   @spec start :: {:ok, pid} | {:error, reason :: term}
   def start do

      [{^channel, clock}] ->
        :ets.insert(c, [{channel, new_clock}, {new_clock, channel}])
        :ets.delete(c, clock)
      [] -> 
        :ok # TODO: log a warning?
    end
    {:noreply, state.join_outstanding(new_clock)}
  end

  def handle_info({:DOWN, _ref, :process, pid, _},
                  __MODULE__.State[clock: clock, children: c] = state) do
    [{^pid, child_clock}] = :ets.lookup(c, pid)
    :ets.delete(c, pid)
    :ets.delete(c, child_clock)
    new_clock = ITC.join(clock, child_clock)
    crash_clock = ITC.join(clock, state.outstanding)
    state = state.clock(new_clock).crash_clock(crash_clock)
    {:noreply, state}
  end  

  @spec execute(pid, Genomu.key | Genomu.cell, Genomu.command, Keyword.t) :: term
  defcall execute(key, cmd, options), state: State[] = state do

      state.outstanding(new)
    end
    def join_outstanding(new, __MODULE__[outstanding: clock] = state) do
      Lager.info "join_outstanding #{inspect new} #{inspect clock}"
      state.outstanding(ITC.join(clock, new))
    end

    defoverridable crash_clock: 2
