-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement observe/observe_deep
Unlike the original observe/observe_deep, it is not called in the middle of a transaction. It is called in the form of a process message after the transaction is completed.
- Loading branch information
Showing
25 changed files
with
1,795 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule Yex.ArrayEvent do | ||
@moduledoc """ | ||
Event when Array type changes | ||
@see Yex.Array.observe/1 | ||
@see Yex.Array.observe_deep/1 | ||
@see Yex.Map.observe_deep/1 | ||
""" | ||
defstruct [ | ||
:path, | ||
:target, | ||
:change | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
path: list(number() | String.t()), | ||
target: Yex.Array.t(), | ||
change: %{insert: list()} | %{delete: number()} | %{} | ||
} | ||
end | ||
|
||
defmodule Yex.MapEvent do | ||
@moduledoc """ | ||
Event when Map type changes | ||
@see Yex.Map.observe/1 | ||
@see Yex.Array.observe_deep/1 | ||
@see Yex.Map.observe_deep/1 | ||
""" | ||
defstruct [ | ||
:path, | ||
:target, | ||
:keys | ||
] | ||
|
||
@type change :: | ||
%{action: :add, new_value: term()} | ||
| %{action: :delete, old_value: term()} | ||
| %{action: :update, old_value: term(), new_value: term()} | ||
@type keys :: %{String.t() => %{}} | ||
|
||
@type t :: %__MODULE__{ | ||
path: list(number() | String.t()), | ||
target: Yex.Map.t(), | ||
keys: keys | ||
} | ||
end | ||
|
||
defmodule Yex.TextEvent do | ||
@moduledoc """ | ||
Event when Text type changes | ||
@see Yex.Text.observe/1 | ||
@see Yex.Array.observe_deep/1 | ||
@see Yex.Map.observe_deep/1 | ||
""" | ||
defstruct [ | ||
:path, | ||
:target, | ||
:delta | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
path: list(number() | String.t()), | ||
target: Yex.Map.t(), | ||
delta: Yex.Text.delta() | ||
} | ||
end | ||
|
||
defmodule Yex.XmlEvent do | ||
@moduledoc """ | ||
Event when XMLFragment/Element type changes | ||
@see Yex.Text.observe/1 | ||
@see Yex.Array.observe_deep/1 | ||
@see Yex.Map.observe_deep/1 | ||
""" | ||
defstruct [ | ||
:path, | ||
:target, | ||
:delta, | ||
:keys | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
path: list(number() | String.t()), | ||
target: Yex.Map.t(), | ||
delta: Yex.Text.delta(), | ||
keys: %{insert: list()} | %{delete: number()} | %{} | ||
} | ||
end | ||
|
||
defmodule Yex.XmlTextEvent do | ||
@moduledoc """ | ||
Event when Text type changes | ||
@see Yex.Text.observe/1 | ||
@see Yex.Array.observe_deep/1 | ||
@see Yex.Map.observe_deep/1 | ||
""" | ||
defstruct [ | ||
:path, | ||
:target, | ||
:delta | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
path: list(number() | String.t()), | ||
target: Yex.Map.t(), | ||
delta: Yex.Text.delta() | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
defprotocol Yex.SharedType do | ||
@moduledoc """ | ||
The SharedType protocol defines the behavior of shared types in Yex. | ||
""" | ||
|
||
@doc """ | ||
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. | ||
* `metadata` is the metadata passed to the `observe_deep` function. | ||
""" | ||
@spec observe(t) :: reference() | ||
def observe(shared_type) | ||
|
||
@doc """ | ||
Registers a change observer that will be message every time this shared type is modified. | ||
see `observe/1` for more information. | ||
## Options | ||
* `:metadata` - provides metadata to be attached to this observe. | ||
""" | ||
@spec observe(t, keyword()) :: reference() | ||
def observe(shared_type, opt) | ||
|
||
@doc """ | ||
Unobserve the shared type for changes. | ||
""" | ||
@spec unobserve(reference()) :: :ok | ||
def unobserve(observe_ref) | ||
|
||
@doc """ | ||
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 array of event struct that describes the change; | ||
* `origin` is the origin passed to the `Yex.Doc.transaction()` function. | ||
* `metadata` is the metadata passed to the `observe_deep` function. | ||
""" | ||
@spec observe_deep(t) :: reference() | ||
def observe_deep(shared_type) | ||
|
||
@doc """ | ||
Registers a change observer that will be message every time this shared type or any of its children is modified. | ||
see `observe_deep/1` for more information. | ||
## Options | ||
* `:metadata` - provides metadata to be attached to this observe. | ||
""" | ||
@spec observe_deep(t, keyword()) :: reference() | ||
def observe_deep(shared_type, opt) | ||
|
||
@doc """ | ||
Unobserve the shared type for changes. | ||
""" | ||
@spec unobserve_deep(reference()) :: :ok | ||
def unobserve_deep(observe_ref) | ||
end | ||
|
||
defimpl Yex.SharedType, | ||
for: [Yex.Array, Yex.Map, Yex.Text, Yex.XmlElement, Yex.XmlFragment, Yex.XmlText] do | ||
defdelegate observe(shared_type), to: @for | ||
defdelegate observe(shared_type, option), to: @for | ||
defdelegate observe_deep(shared_type), to: @for | ||
defdelegate observe_deep(shared_type, option), to: @for | ||
|
||
def unobserve(reference), | ||
do: raise(Protocol.UndefinedError, protocol: @protocol, value: reference) | ||
|
||
def unobserve_deep(reference), | ||
do: raise(Protocol.UndefinedError, protocol: @protocol, value: reference) | ||
end | ||
|
||
defimpl Yex.SharedType, for: Reference do | ||
def observe(reference), | ||
do: raise(Protocol.UndefinedError, protocol: @protocol, value: reference) | ||
|
||
def observe(reference, _option), | ||
do: raise(Protocol.UndefinedError, protocol: @protocol, value: reference) | ||
|
||
def observe_deep(reference), | ||
do: raise(Protocol.UndefinedError, protocol: @protocol, value: reference) | ||
|
||
def observe_deep(reference, _option), | ||
do: raise(Protocol.UndefinedError, protocol: @protocol, value: reference) | ||
|
||
def unobserve(reference), do: unsubscribe(reference) | ||
def unobserve_deep(reference), do: unsubscribe(reference) | ||
|
||
defp unsubscribe(ref) do | ||
case Process.get(ref) do | ||
nil -> | ||
:ok | ||
|
||
sub -> | ||
Process.delete(ref) | ||
Yex.Nif.sub_unsubscribe(sub) | ||
end | ||
end | ||
end |
Oops, something went wrong.