Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/kino/control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test/kino/control_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down