ShouldEmitValue
public enum ShouldEmitValue<StateType>
A predicate that determines if a state change should notify subscribers or not, by comparing previous and new states and returning a Bool true in
case it should emit it, or false in case it should not emit it.
It comes with some standard options like .always
, .never
, .when(old, new) -> Bool
and, for Equatable
structures, .whenDifferent
.
-
It will always emit changes, regardless of previous and new state
Declaration
Swift
case always
-
It will never emit changes, regardless of previous and new state
Declaration
Swift
case never
-
It’s a custom-defined predicate, you’ll be given old and new state, and must return a Bool indicating what you’ve decided from that change, being
true
when you want this change to be notified, orfalse
when you want it to be ignored.Declaration
Swift
case when((StateType, StateType) -> Bool)
-
Evaluates the predicate and returns
true
in case this should be emitted, orfalse
in case this change should be ignoredDeclaration
Swift
public func shouldEmit(previous: StateType, new: StateType) -> Bool
-
Evaluates the predicate and returns
true
in case this should be ignored, orfalse
in case this change should be emitted. It’s the exact inversion ofshouldEmit
and useful for operator.removeDuplicates
that some Reactive libraries offer.Declaration
Swift
public func shouldRemove(previous: StateType, new: StateType) -> Bool
-
For
Equatable
structures,.whenDifferent
will run==
operator between old and new state, and notify when they are different, or ignore when they are equal.Declaration
Swift
public static var whenDifferent: ShouldEmitValue<StateType> { get }