Packages
kino
0.5.2
0.19.0
0.18.0
0.17.0
0.16.1
0.16.0
0.15.3
0.15.2
0.15.1
0.15.0
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.3
0.12.2
0.12.1
0.12.0
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.0
0.6.2
0.6.1
0.6.0
retired
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Interactive widgets for Livebook
Current section
Files
Jump to
Current section
Files
lib/kino/js/live/context.ex
defmodule Kino.JS.Live.Context do
@moduledoc """
State available in `Kino.JS.Live` server callbacks.
## Properties
* `:assigns` - custom server state kept across callback calls
* `:origin` - an opaque identifier of the client that triggered
the given action. It is set in `c:Kino.JS.Live.handle_connect/1`
and `c:Kino.JS.Live.handle_event/3`
"""
defstruct [:assigns, :origin, :__private__]
@type t :: %__MODULE__{assigns: map(), origin: origin(), __private__: map()}
@type origin :: nil | term()
@doc false
def new() do
%__MODULE__{assigns: %{}, origin: nil, __private__: %{}}
end
@doc """
Stores key-value pairs in the state.
## Examples
assign(ctx, count: 1, timestamp: DateTime.utc_now())
"""
@spec assign(t(), Enumerable.t()) :: t()
def assign(%__MODULE__{} = ctx, assigns) do
assigns =
Enum.reduce(assigns, ctx.assigns, fn {key, val}, assigns ->
Map.put(assigns, key, val)
end)
%{ctx | assigns: assigns}
end
@doc """
Updates an existing key with the given function in the state.
## Examples
assign(ctx, count: 1, timestamp: DateTime.utc_now())
"""
@spec update(t(), term(), (term() -> term())) :: t()
def update(%__MODULE__{} = ctx, key, fun) when is_function(fun, 1) do
val = Map.fetch!(ctx.assigns, key)
assign(ctx, [{key, fun.(val)}])
end
@doc """
Sends an event to the client.
The event is dispatched to the registered JavaScript callback
on all connected clients.
## Examples
broadcast_event(ctx, "new_point", %{x: 10, y: 10})
"""
@spec broadcast_event(t(), String.t(), term()) :: :ok
def broadcast_event(%__MODULE__{} = ctx, event, payload \\ nil) when is_binary(event) do
Kino.JS.LiveServer.broadcast_event(ctx, event, payload)
end
end