Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kino.VegaLite.render/1 and deprecate periodically/4 #52

Merged
merged 3 commits into from
Nov 15, 2023
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
16 changes: 14 additions & 2 deletions lib/kino/vega_lite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ defmodule Kino.VegaLite do
|> Vl.mark(:line)
|> Vl.encode_field(:x, "x", type: :quantitative)
|> Vl.encode_field(:y, "y", type: :quantitative)
|> Kino.VegaLite.new()
|> Kino.render()
|> Kino.VegaLite.render()

for i <- 1..300 do
point = %{x: i / 10, y: :math.sin(i / 10)}
Expand Down Expand Up @@ -52,6 +51,18 @@ defmodule Kino.VegaLite do
)
end

@doc """
Renders and returns a new kino with the given VegaLite definition.

It is equivalent to:

vega_lite |> Kino.VegaLite.new() |> Kino.render()
"""
@spec render(VegaLite.t()) :: t()
def render(vl) when is_struct(vl, VegaLite) do
vl |> new() |> Kino.render()
end

@doc """
Appends a single data point to the graphic dataset.

Expand Down Expand Up @@ -145,6 +156,7 @@ defmodule Kino.VegaLite do

The callback is run for the first time immediately upon registration.
"""
@deprecated "Use Kino.listen/3 instead"
@spec periodically(t(), pos_integer(), term(), (term() -> {:cont, term()} | :halt)) :: :ok
def periodically(kino, interval_ms, acc, fun) do
Kino.JS.Live.cast(kino, {:periodically, interval_ms, acc, fun})
Expand Down
20 changes: 0 additions & 20 deletions test/kino/vega_lite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,6 @@ defmodule Kino.VegaLiteTest do
assert_broadcast_event(kino, "push", %{data: [], dataset: nil, window: 0})
end

test "periodically/4 evaluates the given callback in background until stopped" do
kino = start_kino()

parent = self()

Kino.VegaLite.periodically(kino, 1, 1, fn n ->
send(parent, {:ping, n})

if n < 2 do
{:cont, n + 1}
else
:halt
end
end)

assert_receive {:ping, 1}
assert_receive {:ping, 2}
refute_receive {:ping, 3}, 5
end

defp start_kino() do
Vl.new()
|> Vl.mark(:point)
Expand Down
Loading