Skip to content

Commit

Permalink
Add Kino.VegaLite.render/1 and deprecate periodically/4 (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored Nov 15, 2023
1 parent a5fa09f commit be438a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
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

0 comments on commit be438a9

Please sign in to comment.