Skip to content

Commit

Permalink
Don't mix function head pattern matching with value extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Apr 21, 2024
1 parent 7da4bb1 commit 4412034
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
14 changes: 6 additions & 8 deletions lib/asciinema/streaming/live_stream_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Asciinema.Streaming.LiveStreamServer do
end

def reset(stream_id, {_, _} = vt_size, vt_init \\ nil, stream_time \\ nil, theme \\ nil) do
GenServer.call(via_tuple(stream_id), {:reset, vt_size, vt_init, stream_time, theme})
GenServer.call(via_tuple(stream_id), {:reset, {vt_size, vt_init, stream_time, theme}})
end

def feed(stream_id, event) do
Expand Down Expand Up @@ -77,11 +77,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do
{:reply, :ok, %{state | producer: pid}}
end

def handle_call(
{:reset, vt_size, vt_init, stream_time, theme},
{pid, _} = _from,
%{producer: pid} = state
) do
def handle_call({:reset, args}, {pid, _} = _from, %{producer: pid} = state) do
{vt_size, vt_init, stream_time, theme} = args
stream_time = stream_time || 0.0
state = reset_stream(state, vt_size, stream_time, theme)

Expand All @@ -98,13 +95,14 @@ defmodule Asciinema.Streaming.LiveStreamServer do
{:reply, :ok, state}
end

def handle_call({:reset, _vt_size, _vt_init, _stream_time, _theme}, _from, state) do
def handle_call({:reset, _args}, _from, state) do
Logger.info("stream/#{state.stream_id}: rejecting reset from non-leader producer")

{:reply, {:error, :leadership_lost}, state}
end

def handle_call({:feed, {time, data} = event}, {pid, _} = _from, %{producer: pid} = state) do
def handle_call({:feed, event}, {pid, _} = _from, %{producer: pid} = state) do
{time, data} = event
new_size = Vt.feed(state.vt, data)

publish(state.stream_id, %Update{
Expand Down
3 changes: 2 additions & 1 deletion lib/asciinema_web/live/live_stream_status_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ defmodule AsciinemaWeb.LiveStreamStatusLive do
end
end

def handle_info(%LiveStreamServer.Update{event: e, data: {_, _, time, _}}, socket)
def handle_info(%LiveStreamServer.Update{event: e} = update, socket)
when e in [:info, :reset] do
{_, _, time, _} = update.data
started_at = Timex.shift(Timex.now(), milliseconds: -round(time * 1000.0))

socket =
Expand Down
10 changes: 5 additions & 5 deletions lib/asciinema_web/live_stream_consumer_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
@impl true
def websocket_info(message, state)

def websocket_info(%LiveStreamServer.Update{event: e, data: data}, state)
def websocket_info(%LiveStreamServer.Update{event: e} = update, state)
when e in [:info, :reset] do
{{cols, rows}, _, _, _} = data
{{cols, rows}, _, _, _} = update.data
Logger.debug("consumer/#{state.stream_id}: reset (#{cols}x#{rows})")

{:reply, reset_message(data), %{state | reset: true}}
{:reply, reset_message(update.data), %{state | reset: true}}
end

def websocket_info(%LiveStreamServer.Update{event: :feed, data: data}, %{reset: true} = state) do
{:reply, feed_message(data), state}
def websocket_info(%LiveStreamServer.Update{event: :feed} = update, %{reset: true} = state) do
{:reply, feed_message(update.data), state}
end

def websocket_info(%LiveStreamServer.Update{}, %{reset: false} = state) do
Expand Down
7 changes: 4 additions & 3 deletions lib/asciinema_web/live_stream_producer_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ defmodule AsciinemaWeb.LiveStreamProducerSocket do

def websocket_info(:parser_check, state), do: {:ok, state}

def websocket_info(:bucket_fill, %{bucket: bucket} = state) do
def websocket_info(:bucket_fill, state) do
bucket = state.bucket
tokens = min(bucket.size, bucket.tokens + bucket.fill_amount)

if tokens > bucket.tokens && tokens < bucket.size do
Expand Down Expand Up @@ -180,8 +181,8 @@ defmodule AsciinemaWeb.LiveStreamProducerSocket do
{:error, {:invalid_vt_size, size}}
end

defp run_command({:feed, {time, data}}, %{status: :online} = state) do
with :ok <- LiveStreamServer.feed(state.stream_id, {time, data}) do
defp run_command({:feed, args}, %{status: :online} = state) do
with :ok <- LiveStreamServer.feed(state.stream_id, args) do
{:ok, state}
end
end
Expand Down

0 comments on commit 4412034

Please sign in to comment.