Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): Add storage actor #22

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ makedocs(
"Void" => "actors/types/void.md",
"Function" => "actors/types/function.md",
"Server" => "actors/types/server.md",
"Storage" => "actors/types/storage.md",
],
"Subscriptions" => [
"Void" => "teardown/types/void.md",
Expand Down
9 changes: 9 additions & 0 deletions docs/src/actors/types/storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# [Storage actor](@id actor_storage)

```@docs
storage
```

```@docs
StorageActor
```
1 change: 1 addition & 0 deletions src/Rocket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ include("actor/sync.jl")
include("actor/keep.jl")
include("actor/buffer.jl")
include("actor/server.jl")
include("actor/storage.jl")
include("actor/test.jl")

include("subjects/subject.jl")
Expand Down
75 changes: 75 additions & 0 deletions src/actor/storage.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
export StorageActor, storage, getvalues

"""
StorageActor{D}() where D

Storage actor provides an actor that stores a single (last) value passed to the `next!` callback.
It saves last incoming successful `next` event in the `value` field, throws an ErrorException on `error!` event and does nothing on completion event.
Before any events `value` initialised with `nothing`.

# Examples
```jldoctest
using Rocket

source = from(1:5)
actor = storage(Int)

subscribe!(source, actor)
show(getvalues(actor))

# output
5
```

See also: [`Actor`](@ref), [`storage`](@ref)
"""
mutable struct StorageActor{T} <: Actor{T}
value :: Union{Nothing, T}

StorageActor{T}() where T = new(nothing)
end

getvalues(actor::StorageActor) = actor.value

on_next!(actor::StorageActor{T}, data::T) where T = actor.value = data
on_error!(actor::StorageActor, err) = error(err)
on_complete!(actor::StorageActor) = begin end

"""
storage(::Type{T}) where T

# Arguments
- `::Type{T}`: Type of storage data

Creation operator for the `StorageActor` actor.

# Examples

```jldoctest
using Rocket

actor = storage(Int)
actor isa StorageActor{Int}

# output
true
```

See also: [`StorageActor`](@ref), [`AbstractActor`](@ref)
"""
storage(::Type{T}) where T = StorageActor{T}()

# Julia iterable interface

Base.IteratorSize(::Type{ <: StorageActor }) = Base.HasLength()
Base.IteratorEltype(::Type{ <: StorageActor }) = Base.HasEltype()

Base.IndexStyle(::Type{ <: StorageActor }) = Base.IndexLinear()

Base.eltype(::Type{ <: StorageActor{T} }) where T = T

Base.iterate(actor::StorageActor{T}) where T = iterate(actor.value::T)
Base.iterate(actor::StorageActor{T}, state) where T = iterate(actor.value::T, state)

Base.size(actor::StorageActor) = (length(actor.values), )
Base.length(actor::StorageActor) = 1
38 changes: 38 additions & 0 deletions test/actor/test_storage_actor.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module RocketStorageActorTest

using Test
using Rocket

@testset "StorageActor" begin

println("Testing: actor StorageActor")

@testset begin
source = from([ 1, 2, 3 ])
actor = StorageActor{Int}()

@test actor.value === nothing

subscribe!(source, actor)

@test actor.value == 3
@test getvalues(actor) == 3
end

@testset begin
source = faulted(Int, "Error")
actor = StorageActor{Int}()

@test actor.value === nothing
@test_throws ErrorException subscribe!(source, actor)
@test actor.value === nothing
@test getvalues(actor) === nothing
end

@testset begin
@test storage(Int) isa StorageActor{Int}
@test storage(Real) isa StorageActor{Real}
end
end

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ doctest(Rocket)
include("./actor/test_buffer_actor.jl")
include("./actor/test_sync_actor.jl")
include("./actor/test_function_actor.jl")
include("./actor/test_storage_actor.jl")

include("./test_subscribable.jl")
include("./observable/test_observable_function.jl")
Expand Down