View Source Yex.Awareness (y_ex v0.6.3)

Examples

iex> doc = Yex.Doc.new()
iex> {:ok, _awareness} = Yex.Awareness.new(doc)

Summary

Functions

Applies an update (incoming from remote channel or generated using [Awareness.encode_update] method) and modifies a state of a current instance.

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.clean_local_state(awareness)
iex> Yex.Awareness.get_client_ids(awareness)
[]

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 10 }))
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> Yex.Awareness.encode_update(awareness, [10])
{:ok, <<1, 10, 1, 15, 123, 34, 107, 101, 121, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125>>}

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.new())
iex> Yex.Awareness.apply_update(awareness, <<1, 210, 165, 202, 167, 8, 1, 2, 123, 125>>)
iex> Yex.Awareness.get_client_ids(awareness)
[2230489810]

get local state of the current client

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> Yex.Awareness.get_states(awareness)
%{100 => %{"key" => "value"}}

Listen to remote and local state changes. Get notified when a state is either added, updated, or removed.

Monitor to remote and local awareness changes. This event is called even when the awareness state does not change but is only updated to notify other users that this client is still online. Use this event if you want to propagate awareness state to other users.

create a new awareness instance

Clears out a state of a given client, effectively marking it as disconnected.

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> Yex.Awareness.get_states(awareness)
%{100 => %{"key" => "value"}}

Types

@type t() :: %Yex.Awareness{reference: any()}

Functions

Link to this function

apply_update(awareness, update, origin \\ nil)

View Source
@spec apply_update(t(), binary(), String.t() | nil) :: :ok

Applies an update (incoming from remote channel or generated using [Awareness.encode_update] method) and modifies a state of a current instance.

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.new())
iex> Yex.Awareness.clean_local_state(awareness)
iex> Yex.Awareness.apply_update(awareness, <<1, 210, 165, 202, 167, 8, 1, 2, 123, 125>>)
:ok
iex> Yex.Awareness.get_client_ids(awareness)
[2230489810]
Link to this function

clean_local_state(awareness)

View Source
@spec clean_local_state(t()) :: :ok

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.clean_local_state(awareness)
iex> Yex.Awareness.get_client_ids(awareness)
[]
@spec client_id(t()) :: integer()
@spec demonitor_change(reference()) :: :ok
@spec demonitor_update(reference()) :: :ok
Link to this function

encode_update(awareness, clients)

View Source
@spec encode_update(t(), [integer()]) :: {:ok, binary()}

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 10 }))
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> Yex.Awareness.encode_update(awareness, [10])
{:ok, <<1, 10, 1, 15, 123, 34, 107, 101, 121, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125>>}
Link to this function

get_client_ids(awareness)

View Source
@spec get_client_ids(t()) :: [integer()]

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.new())
iex> Yex.Awareness.apply_update(awareness, <<1, 210, 165, 202, 167, 8, 1, 2, 123, 125>>)
iex> Yex.Awareness.get_client_ids(awareness)
[2230489810]
Link to this function

get_local_state(awareness)

View Source
@spec get_local_state(t()) :: map()

get local state of the current client

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.get_states(awareness)
%{}
@spec get_states(t()) :: %{required(integer()) => term()}

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> Yex.Awareness.get_states(awareness)
%{100 => %{"key" => "value"}}
Link to this function

monitor_change(awareness)

View Source
@spec monitor_change(t()) :: reference()

Listen to remote and local state changes. Get notified when a state is either added, updated, or removed.

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 10 }))
iex> Yex.Awareness.monitor_change(awareness)
iex> Yex.Awareness.apply_update(awareness, <<1, 210, 165, 202, 167, 8, 1, 2, 123, 125>>)
iex> receive do {:awareness_change, %{removed: [], added: [2230489810], updated: []}, _origin, _awareness} -> :ok end
Link to this function

monitor_update(awareness)

View Source
@spec monitor_update(t()) :: reference()

Monitor to remote and local awareness changes. This event is called even when the awareness state does not change but is only updated to notify other users that this client is still online. Use this event if you want to propagate awareness state to other users.

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 10 }))
iex> Yex.Awareness.monitor_update(awareness)
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> receive do {:awareness_update, %{removed: [], added: [10], updated: []}, _origin, _awareness} -> :ok end

create a new awareness instance

Link to this function

remove_states(awareness, clients)

View Source
@spec remove_states(t(), [integer()]) :: :ok

Clears out a state of a given client, effectively marking it as disconnected.

Examples

iex> {:ok, _awareness} = Yex.Awareness.new(Yex.Doc.new())
Link to this function

set_local_state(awareness, map)

View Source
@spec set_local_state(t(), map()) :: :ok

Examples

iex> {:ok, awareness} = Yex.Awareness.new(Yex.Doc.with_options(%Yex.Doc.Options{ client_id: 100 }))
iex> Yex.Awareness.set_local_state(awareness, %{ "key" => "value" })
iex> Yex.Awareness.get_states(awareness)
%{100 => %{"key" => "value"}}