-
Notifications
You must be signed in to change notification settings - Fork 130
Algorithm.h
#include "react/Algorithm.h"
Contains various reactive operations to combine and convert signals and events.
namespace react
{
// Iteratively combines signal value with values from event stream
Signal<D,S> Iterate(const Events<D,E>& events, V&& init, F&& func);
Signal<D,S> Iterate(const Events<D,E>& events, V&& init,
const SignalPack<D,TDepValues...>& depPack, FIn&& func);
// Hold the most recent event in a signal
Signal<D,T> Hold(const Events<D,T>& events, V&& init);
// Sets signal value to value of target when event is received
Signal<D,S> Snapshot(const Events<D,E>& trigger, const Signal<D,S>& target);
// Emits value changes of target signal
Events<D,S> Monitor(const Signal<D,S>& target);
// Emits value of target signal when event is received
Events<D,S> Pulse(const Events<D,E>& trigger, const Signal<D,S>& target);
// Emits token when target signal was changed
Events<D,Token> OnChanged(const Signal<D,S>& target);
// Emits token when target signal was changed to value
Events<D,Token> OnChangedTo(const Signal<D,S>& target, V&& value);
}
// (1)
template
<
typename D,
typename E,
typename V,
typename F,
typename S = decay<V>::type
>
Signal<D,S> Iterate(const Events<D,E>& events, V&& init, F&& func);
// (2)
template
<
typename D,
typename E,
typename V,
typename F,
typename ... TDepValues,
typename S = decay<V>::type
>
Signal<D,S> Iterate(const Events<D,E>& events, V&& init,
const SignalPack<D,TDepValues...>& depPack, F&& func);
(1) Creates a signal with an initial value v = init
.
-
If the return type of
func
isS
: For every received evente
inevents
,v
is updated tov = func(e,v)
. -
If the return type of
func
isvoid
: For every received evente
inevents
,v
is passed by non-cost reference tofunc(e,v)
, making it mutable. This variant can be used if copying and comparingS
is prohibitively expensive. Because the old and new values cannot be compared, updates will always trigger a change.
(2) Similar to (1), but the synchronized values of signals in depPack
are passed to func
as additional arguments. Changes of signals in depPack
do not trigger an update - only received events do.
The signature of func
should be equivalent to:
- (1a)
S func(const E&, const S&)
- (1b)
void func(const E&, S&)
- (2a)
S func(const E&, const S&, const TDepValues& ...)
- (2b)
void func(const E&, S&, const TDepValues& ...)
(1a)
(2a)
template
<
typename D,
typename V,
typename T = decay<V>::type
>
Signal<D,T> Hold(const Events<D,T>& events, V&& init);
Creates a signal with an initial value v = init
.
For received event values e1, e2, ... eN
in events
, it is updated to v = eN
.
template
<
typename D,
typename S,
typename E
>
Signal<D,S> Snapshot(const Events<D,E>& trigger, const Signal<D,S>& target);
Creates a signal with value v = target.Value()
.
The value is set on construction and updated only when receiving an event from trigger
.
template
<
typename D,
typename S
>
Events<D,S> Monitor(const Signal<D,S>& target);
When target
changes, emit the new value e = target.Value()
.
template
<
typename D,
typename S,
typename E
>
Events<D,S> Pulse(const Events<D,E>& trigger, const Signal<D,S>& target);
Creates an event stream that emits target.Value()
when receiving an event from trigger
The values of the received events are irrelevant.
template
<
typename D,
typename S
>
Events<D,Token> OnChangedTo(const Signal<D,S>& target);
Creates a token stream that emits when target
is changed.
template
<
typename D,
typename V,
typename S = decay<V>::type
>
Events<D,Token> OnChangedTo(const Signal<D,S>& target, V&& value);
Creates a token stream that emits when target
is changed and target.Value() == value
.
V
and S
should be comparable with ==
.