Skip to content

Commit 481c944

Browse files
authored
Add Kino.JS.Live events support to subscribe/unsubscribe (#505)
1 parent cb7dceb commit 481c944

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/kino/control.ex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ defmodule Kino.Control do
307307
end
308308

309309
@doc """
310-
Subscribes the calling process to control or input events.
310+
Subscribes the calling process to control, input, or `Kino.JS.Live` events.
311311
312312
This is an alternative API to `stream/1`, such that event
313313
messages are consumed via process messages instead of streams.
@@ -316,18 +316,20 @@ defmodule Kino.Control do
316316
event details. In particular, it always includes `:origin`, which
317317
is an opaque identifier of the client that triggered the event.
318318
"""
319-
@spec subscribe(t() | Kino.Input.t(), term()) :: :ok
319+
@spec subscribe(t() | Kino.Input.t() | Kino.JS.Live.t(), term()) :: :ok
320320
def subscribe(source, tag)
321-
when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) do
321+
when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) or
322+
is_struct(source, Kino.JS.Live) do
322323
Kino.SubscriptionManager.subscribe(source.ref, self(), tag)
323324
end
324325

325326
@doc """
326-
Unsubscribes the calling process from control or input events.
327+
Unsubscribes the calling process from control, input, or `Kino.JS.Live` events.
327328
"""
328-
@spec unsubscribe(t() | Kino.Input.t()) :: :ok
329+
@spec unsubscribe(t() | Kino.Input.t() | Kino.JS.Live.t()) :: :ok
329330
def unsubscribe(source)
330-
when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) do
331+
when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) or
332+
is_struct(source, Kino.JS.Live) do
331333
Kino.SubscriptionManager.unsubscribe(source.ref, self())
332334
end
333335

test/kino/control_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ defmodule Kino.ControlTest do
8080

8181
assert_receive {:name, ^info}
8282
end
83+
84+
test "subscribes to Kino.JS.Live events" do
85+
kino = Kino.TestModules.LiveCounter.new(0)
86+
87+
Kino.Control.subscribe(kino, :counter)
88+
89+
Kino.TestModules.LiveCounter.bump(kino, 5)
90+
91+
assert_receive {:counter, %{event: :bump, by: 5}}
92+
end
8393
end
8494

8595
describe "stream/1" do

0 commit comments

Comments
 (0)