# `Yex.SharedType`
[🔗](https://github.com/satoren/y_ex/blob/main/lib/shared_type/shared_type.ex#L1)

The SharedType protocol defines the behavior of shared types in Yex.
This module provides functionality for observing changes to shared types,
including arrays, maps, text, and XML nodes.

# `t`

```elixir
@type t() ::
  %Yex.Array{doc: term(), reference: term()}
  | %Yex.Map{doc: term(), reference: term()}
  | %Yex.Text{doc: term(), reference: term()}
  | %Yex.XmlElement{doc: term(), reference: term()}
  | %Yex.XmlText{doc: term(), reference: term()}
  | %Yex.XmlFragment{doc: term(), reference: term()}
```

# `observe`

```elixir
@spec observe(
  t(),
  keyword()
) :: reference()
```

Registers a change observer that will be message every time this shared type is modified.

If the shared type changes, a message is delivered to the
monitoring process in the shape of:
    {:observe_event, ref, event, origin, metadata}

where:
  * `ref` is a monitor reference returned by this function;
  * `event` is a struct that describes the change;
  * `origin` is the origin passed to the `Yex.Doc.transaction()` function, `nil` when
    none was given, or the `%Yex.UndoManager{}` for an undo or redo.
  * `metadata` is the metadata passed to the `observe` function.

## Options
  * `:metadata` - provides metadata to be attached to this observe.

## Returns
  * A reference that can be used to unsubscribe the observer

# `observe_deep`

```elixir
@spec observe_deep(
  t(),
  keyword()
) :: reference()
```

Registers a change observer that will be message every time this shared type or any of its children is modified.

If the shared type changes, a message is delivered to the
monitoring process in the shape of:
    {:observe_deep_event, ref, events, origin, metadata}

where:
  * `ref` is a monitor reference returned by this function;
  * `events` is a list of structs that describes the changes;
  * `origin` is the origin passed to the `Yex.Doc.transaction()` function, `nil` when
    none was given, or the `%Yex.UndoManager{}` for an undo or redo.
  * `metadata` is the metadata passed to the `observe_deep` function.

## Options
  * `:metadata` - provides metadata to be attached to this observe.

## Returns
  * A reference that can be used to unsubscribe the deep observer

# `unobserve`

```elixir
@spec unobserve(reference()) :: :ok
```

Unobserve the shared type for changes.
Removes the observer registered with the given reference.

# `unobserve_deep`

```elixir
@spec unobserve_deep(reference()) :: :ok
```

Unobserve the shared type and its children for changes.
Removes the deep observer registered with the given reference.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
