RecentSubject
Rocket.RecentSubject
— FunctionRecentSubject(::Type{D}) where D
RecentSubject(::Type{D}, factory::F) where { D, F <: AbstractSubjectFactory }
RecentSubject(::Type{D}, subject::S) where { D, S }
A variant of Subject that emits its recent value whenever it is subscribed to.
See also: RecentSubjectFactory
, Subject
, SubjectFactory
Rocket.RecentSubjectFactory
— FunctionRecentSubjectFactory(factory::F) where { F <: AbstractSubjectFactory }
RecentSubjectFactory(; scheduler::H = AsapScheduler()) where { H <: AbstractScheduler }
A variant of SubjectFactory that creates an instance of RecentSubject.
See also: SubjectFactory
, AbstractSubjectFactory
, RecentSubject
, Subject
Description
One of the variants of Subjects is the RecentSubject
, which has a notion of "the recent value". It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "recent value" from the RecentSubject
.
RecentSubjects is a more efficient version of ReplaySubjects with replay size equal to one.
Examples
In the following example, after RecentSubject
is initialized the first Observer receives nothing when it subscribes. The second Observer receives the value 2 even though it subscribed after the value 2 was sent.
using Rocket
subject = RecentSubject(Int)
subscription1 = subscribe!(subject, logger("1"))
next!(subject, 1)
next!(subject, 2)
subscription2 = subscribe!(subject, logger("2"))
next!(subject, 3)
unsubscribe!(subscription1)
unsubscribe!(subscription2)
// Logs
// [1] Data: 1
// [1] Data: 2
// [2] Data: 2
// [1] Data: 3
// [2] Data: 3