Handle direct event messages in JS view channel (#1332)

This commit is contained in:
Jonatan Kłosko 2022-08-04 23:01:09 +02:00 committed by GitHub
parent e8920257cd
commit c68df08849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View file

@ -82,6 +82,15 @@ defmodule LivebookWeb.JSViewChannel do
{:noreply, socket}
end
def handle_info({:event, event, payload, %{ref: ref}}, socket) do
with {:error, error} <- try_push(socket, "event:#{ref}", [event], payload) do
message = "Failed to serialize widget data, " <> error
push(socket, "error:#{ref}", %{"message" => message})
end
{:noreply, socket}
end
def handle_info({:pong, _, %{ref: ref}}, socket) do
push(socket, "pong:#{ref}", %{})
{:noreply, socket}

View file

@ -47,6 +47,16 @@ defmodule LivebookWeb.JSViewChannelTest do
assert_receive {:event, "ping", [1, 2, 3], %{origin: _origin}}
end
test "sends server events to the target client", %{socket: socket} do
push(socket, "connect", %{"session_token" => session_token(), "ref" => "1", "id" => "id1"})
assert_receive {:connect, from, %{}}
send(from, {:connect_reply, [1, 2, 3], %{ref: "1"}})
send(from, {:event, "ping", [1, 2, 3], %{ref: "1"}})
assert_push "event:1", %{"root" => [["ping"], [1, 2, 3]]}
end
describe "binary payload" do
test "initial data", %{socket: socket} do
push(socket, "connect", %{"session_token" => session_token(), "ref" => "1", "id" => "id1"})