-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Observables.Operator.DistinctUntilChanged do | ||
@moduledoc false | ||
use Observables.GenObservable | ||
|
||
def init([comparator]) do | ||
Logger.debug("Distinct Until Changed: #{inspect(self())}") | ||
{:ok, %{:comp => comparator, :seen => []}} | ||
end | ||
|
||
def handle_event(v, state = %{:comp => f, :seen => xs}) do | ||
seen? = Enum.any?(xs, fn seen -> f.(v, seen) end) | ||
|
||
if not seen? do | ||
{:value, v, %{:comp => f, :seen => []}} | ||
else | ||
{:novalue, state} | ||
end | ||
end | ||
|
||
def handle_done(pid, _state) do | ||
Logger.debug("#{inspect(self())}: dependency stopping: #{inspect(pid)}") | ||
{:ok, :continue} | ||
end | ||
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
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,36 @@ | ||
defmodule DistinctUntilChangedTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs} | ||
require Logger | ||
|
||
@tag :distinctuntilchanged | ||
test "distinctuntilchanged" do | ||
testproc = self() | ||
|
||
xs = [1, 1, 2, 2, 3, 3] | ||
|
||
expected = [1, 2, 3] | ||
|
||
xs | ||
|> Obs.from_enum(100) | ||
|> Obs.distinctuntilchanged() | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
expected | ||
|> Enum.map(fn x -> | ||
assert_receive(^x, 1000, "did not get this message!") | ||
|
||
receive do | ||
^x -> assert "duplicates" == "" | ||
after | ||
1000 -> :ok | ||
end | ||
end) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
0 -> :ok | ||
end | ||
end | ||
end |