diff --git a/lib/kino/control.ex b/lib/kino/control.ex index 7e262b6c..33f50804 100644 --- a/lib/kino/control.ex +++ b/lib/kino/control.ex @@ -307,7 +307,7 @@ defmodule Kino.Control do end @doc """ - Subscribes the calling process to control or input events. + Subscribes the calling process to control, input, or `Kino.JS.Live` events. This is an alternative API to `stream/1`, such that event messages are consumed via process messages instead of streams. @@ -316,18 +316,20 @@ defmodule Kino.Control do event details. In particular, it always includes `:origin`, which is an opaque identifier of the client that triggered the event. """ - @spec subscribe(t() | Kino.Input.t(), term()) :: :ok + @spec subscribe(t() | Kino.Input.t() | Kino.JS.Live.t(), term()) :: :ok def subscribe(source, tag) - when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) do + when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) or + is_struct(source, Kino.JS.Live) do Kino.SubscriptionManager.subscribe(source.ref, self(), tag) end @doc """ - Unsubscribes the calling process from control or input events. + Unsubscribes the calling process from control, input, or `Kino.JS.Live` events. """ - @spec unsubscribe(t() | Kino.Input.t()) :: :ok + @spec unsubscribe(t() | Kino.Input.t() | Kino.JS.Live.t()) :: :ok def unsubscribe(source) - when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) do + when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) or + is_struct(source, Kino.JS.Live) do Kino.SubscriptionManager.unsubscribe(source.ref, self()) end diff --git a/test/kino/control_test.exs b/test/kino/control_test.exs index e6869862..5ea1f75d 100644 --- a/test/kino/control_test.exs +++ b/test/kino/control_test.exs @@ -80,6 +80,16 @@ defmodule Kino.ControlTest do assert_receive {:name, ^info} end + + test "subscribes to Kino.JS.Live events" do + kino = Kino.TestModules.LiveCounter.new(0) + + Kino.Control.subscribe(kino, :counter) + + Kino.TestModules.LiveCounter.bump(kino, 5) + + assert_receive {:counter, %{event: :bump, by: 5}} + end end describe "stream/1" do