Skip to content

Commit

Permalink
fix(): fix and tests for lazy observable
Browse files Browse the repository at this point in the history
  • Loading branch information
bvdmitri committed Jan 4, 2022
1 parent 099f14b commit 0f57455
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Rocket"
uuid = "df971d30-c9d6-4b37-b8ff-e965b2cb3a40"
authors = ["Dmitri Bagaev <bvdmitri@gmail.com>"]
version = "1.3.18"
version = "1.3.19"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
7 changes: 5 additions & 2 deletions src/observable/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end
end

getpending(lazy::LazyObservable) = lazy.pending
pushpending!(lazy::LazyObservable, subscription::LazySubscription, actor) = push!(lazy.pending, (subscription, actor))

getstream(lazy::LazyObservable) = lazy.stream
setstream!(lazy::LazyObservable, stream) = lazy.stream = stream
Expand Down Expand Up @@ -49,7 +50,9 @@ function on_subscribe!(observable::LazyObservable{D}, actor) where D
if stream !== nothing
return LazySubscription(subscribe!(stream, actor))
else
return LazySubscription(observable)
subscription = LazySubscription(observable)
pushpending!(observable, subscription, actor)
return subscription
end
end

Expand All @@ -62,7 +65,7 @@ function on_unsubscribe!(subscription::LazySubscription)
end

function unsubscribe_lazy!(subscription::LazySubscription, observable::LazyObservable)
filter!(p -> first(p) === subscription, observable.pending)
filter!(p -> first(p) !== subscription, observable.pending)
return nothing
end

Expand Down
98 changes: 98 additions & 0 deletions test/observable/test_observable_lazy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module RocketLazyObservableTest

using Test
using Rocket

include("../test_helpers.jl")

@testset "LazyObservable" begin

println("Testing: lazy")

@testset begin

values = []
stream = lazy(Int)
subscription = subscribe!(stream, (x) -> push!(values, x))
set!(stream, from(1:3))
@test values == [ 1, 2, 3 ]
subscription = subscribe!(stream, (x) -> push!(values, x))
@test values == [ 1, 2, 3, 1, 2, 3 ]

end

@testset begin

values = []
stream = lazy(Int)
subscription = subscribe!(stream, (x) -> push!(values, x))
unsubscribe!(subscription)
set!(stream, from(1:3))
@test values == [ ]
subscription = subscribe!(stream, (x) -> push!(values, x))
@test values == [ 1, 2, 3 ]

end

@testset begin

values1 = []
values2 = []
stream = lazy(Int)
subscription1 = subscribe!(stream, (x) -> push!(values1, x))
subscription2 = subscribe!(stream, (x) -> push!(values2, x))
set!(stream, from(1:3))
@test values1 == [ 1, 2, 3 ]
@test values2 == [ 1, 2, 3 ]

end

@testset begin

values1 = []
values2 = []
stream = lazy(Int)
subscription1 = subscribe!(stream, (x) -> push!(values1, x))
subscription2 = subscribe!(stream, (x) -> push!(values2, x))
unsubscribe!(subscription1)
set!(stream, from(1:3))
@test values1 == [ ]
@test values2 == [ 1, 2, 3 ]

end

@testset begin

values1 = []
values2 = []
stream = lazy(Int)
subscription1 = subscribe!(stream, (x) -> push!(values1, x))
subscription2 = subscribe!(stream, (x) -> push!(values2, x))
unsubscribe!(subscription2)
set!(stream, from(1:3))
@test values1 == [ 1, 2, 3]
@test values2 == [ ]

end

@testset begin

values = []
stream = lazy(Int)
subject = Subject(Int)
next!(subject, 1)
subscription = subscribe!(stream, (x) -> push!(values, x))
set!(stream, subject)
@test values == [ ]
next!(subject, 2)
@test values == [ 2 ]
unsubscribe!(subscription)
next!(subject, 3)
@test values == [ 2 ]

end


end

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ doctest(Rocket)
include("./observable/test_observable_timer.jl")
include("./observable/test_observable_interval.jl")
include("./observable/test_observable_proxy.jl")
include("./observable/test_observable_lazy.jl")
include("./observable/test_observable_combine_latest.jl")
include("./observable/test_observable_combine_updates.jl")
include("./observable/test_observable_collect_latest.jl")
Expand Down

2 comments on commit 0f57455

@bvdmitri
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/51631

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.19 -m "<description of version>" 0f574551eacd05d8c273f9745c8645230a14d2e3
git push origin v1.3.19

Please sign in to comment.