Packages
kino
0.13.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/attribute_store.ex
defmodule Kino.AttributeStore do
@moduledoc false
@table_name __MODULE__
@doc """
Initializes resources for global attrs.
"""
@spec initialize() :: :ok
def initialize() do
:ets.new(@table_name, [:set, :named_table, :public])
:ok
end
@doc """
Increments the given counter and returns the new value.
"""
@spec counter_next(term()) :: integer()
def counter_next(key) do
:ets.update_counter(@table_name, key, {2, 1}, {key, 0})
end
@doc """
Sets the counter to `value` unless it already has a higher value.
Returns the new counter value.
"""
@spec counter_put_max(term(), integer()) :: integer()
def counter_put_max(key, value) do
[_, counter] =
:ets.update_counter(@table_name, key, [{2, -1, value, value - 1}, {2, 1}], {key, 0})
counter
end
@doc """
Puts the shared attribute value.
"""
@spec put_attribute(term(), term()) :: boolean()
def put_attribute(key, value) do
:ets.insert(@table_name, {key, value})
end
@doc """
Returns the attribute value for a given key.
"""
@spec get_attribute(term()) :: term()
def get_attribute(key, default \\ nil) do
case :ets.lookup(@table_name, key) do
[] -> default
[{_key, value} | _] -> value
end
end
end