Module React.S
Signal combinators.
Consult their semantics.
Primitive and basics
type 'a t= 'a signalThe type for signals of type
'a.
val const : 'a -> 'a signalconst vis alwaysv, [const v]t= v.
val create : ?eq:('a -> 'a -> bool) -> 'a -> 'a signal * (?step:step -> 'a -> unit)create iis a primitive signalsset toiand asetfunction. The functionsetis such that:set vsets the signal's value tovat the time it is called and triggers an update step.set ~step vsets the signal's value tovat the time it is called and updates it dependencies whenstepis executedset ~step vraisesInvalid_argumentif it was previously called with a step and this step has not executed yet or if the givenstepwas already executed.
Warning.
setmust not be executed inside an update step.
val value : 'a signal -> 'avalue siss's current value.Warning. If executed in an update step may return a non up-to-date value or raise
Failureif the signal is not yet initialized.
val retain : 'a signal -> (unit -> unit) -> [ `R of unit -> unit ]retain s ckeeps a reference to the closurecinsand returns the previously retained value.cwill never be invoked.Raises.
Invalid_argumenton constant signals.
val stop : ?strong:bool -> 'a signal -> unitstop s, stops updatings. It conceptually becomesconstwith the signal's last value and cannot be restarted. Allows to disable effectful signals.The
strongargument should only be used on platforms where weak arrays have a strong semantics (i.e. JavaScript). See details.Note. If executed in an update step the signal may still update in the step.
val equal : ?eq:('a -> 'a -> bool) -> 'a signal -> 'a signal -> boolequal s s'istrueiffsands'are equal. If both signals areconstanteqis used between their value (defauts to structural equality). If both signals are notconstant, physical equality is used.
val trace : ?iff:bool t -> ('a -> unit) -> 'a signal -> 'a signaltrace iff tr sissexcepttris invoked withs's current value and onschanges wheniffistrue(defaults toS.const true). For all t where [s]t= vand (t = 0 or ([s]t-dt= v'andeq v v' = false)) and [iff]t =true,tris invoked withv.
From events
Transforming and filtering
val app : ?eq:('b -> 'b -> bool) -> ('a -> 'b) signal -> 'a signal -> 'b signalapp sf sholds the value ofsfapplied to the value ofs, [app sf s]t=[sf]t [s]t.
val map : ?eq:('b -> 'b -> bool) -> ('a -> 'b) -> 'a signal -> 'b signalmap f sisstransformed byf, [map f s]t =f[s]t.
val filter : ?eq:('a -> 'a -> bool) -> ('a -> bool) -> 'a -> 'a signal -> 'a signalfilter f i siss's values that satisfyp. If a value does not satisfypit holds the last value that was satisfied oriif there is none.- [
filter p s]t=[s]t ifp[s]t= true. - [
filter p s]t=[s]t' ifp[s]t= falseand t' is the greatest t' < t withp[s]t'= true. - [
filter p e]t= iotherwise.
- [
val fmap : ?eq:('b -> 'b -> bool) -> ('a -> 'b option) -> 'b -> 'a signal -> 'b signalfmap fm i sissfiltered and mapped byfm.- [
fmap fm i s]t=v iffm[s]t= Some v. - [
fmap fm i s]t=[fmap fm i s]t' iffm[s]t= Noneand t' is the greatest t' < t withfm[s]t'<> None. - [
fmap fm i s]t= iotherwise.
- [
val diff : ('a -> 'a -> 'b) -> 'a signal -> 'b eventdiff f sis an event with occurrences wheneverschanges fromv'tovandeq v v'isfalse(eqis the signal's equality function). The value of the occurrence isf v v'.- [
diff f s]t= Some dif [s]t= vand [s]t-dt= v'andeq v v' = falseandf v v' = d. - [
diff f s]t= Noneotherwise.
- [
val sample : ('b -> 'a -> 'c) -> 'b event -> 'a signal -> 'c eventsample f e ssamplessate's occurrences.- [
sample f e s]t= Some (f ev sv)if [e]t= Some evand [s]t= sv. - [
sample e s]t= Noneotherwise.
- [
val on : ?eq:('a -> 'a -> bool) -> bool signal -> 'a -> 'a signal -> 'a signalon c i sis the signalswhenevercistrue. Whencisfalseit holds the last valueshad whencwas the last timetrueoriif it never was.- [
on c i s]t=[s]t if [c]t= true - [
on c i s]t=[s]t' if [c]t= falsewhere t' is the greatest t' < t with [c]t'= true. - [
on c i s]t=iotherwise.
- [
val dismiss : ?eq:('a -> 'a -> bool) -> 'b event -> 'a -> 'a signal -> 'a signaldismiss c i sis the signalsexcept changes whencoccurs are ignored. Ifcoccurs initiallyiis used.- [
dismiss c i s]t=[s]t' where t' is the greatest t' <= t with [c]t'= Noneand [s]t'-dt<>[s]t' - [
dismiss_ c i s]0=vwherev = iif [c]0= Some _andv =[s]0 otherwise.
- [
Accumulating
val accum : ?eq:('a -> 'a -> bool) -> ('a -> 'a) event -> 'a -> 'a signalaccum e iisS.hold i (Accumulatinge i).
Combining
val merge : ?eq:('a -> 'a -> bool) -> ('a -> 'b -> 'a) -> 'a -> 'b signal list -> 'a signalmerge f a slmerges the value of every signal inslusingfand the accumulatora.[
merge f a sl]t= List.fold_left f a (List.map[]tsl).
val switch : ?eq:('a -> 'a -> bool) -> 'a signal signal -> 'a signalswitch ssis the inner signal ofss.- [
switch ss]t=[[ss]t]t.
- [
val bind : ?eq:('b -> 'b -> bool) -> 'a signal -> ('a -> 'b signal) -> 'b signalbind s sfisswitch (map ~eq:( == ) sf s).
val fix : ?eq:('a -> 'a -> bool) -> 'a -> ('a signal -> 'a signal * 'b) -> 'bfix i sfallow to refer to the value a signal had an infinitesimal amount of time before.In
fix sf,sfis called with a signalsthat represents the signal returned bysfdelayed by an infinitesimal amount time. Ifs', r = sf sthenris returned byfixandsis such that :- [
s]t=ifor t = 0. - [
s]t=[s']t-dt otherwise.
eqis the equality used bys.Raises.
Invalid_argumentifs'is directly a delayed signal (i.e. a signal given to a fixing function).Note. Regarding values depending on the result
rofs', r = sf sthe following two cases need to be distinguished :After
sf sis applied,s'does not depend on a value that is in a step andshas no dependents in a step (e.g in the simple case wherefixis applied outside a step).In that case if the initial value of
s'differs fromi,sand its dependents need to be updated and a special update step will be triggered for this. Values depending on the resultrwill be created only after this special update step has finished (e.g. they won't see theiofsifr = s).- Otherwise, values depending on
rwill be created in the same step assands'(e.g. they will see theiofsifr = s).
- [
Lifting
Lifting combinators. For a given n the semantics is :
[ln f a1 ... an]t = f [a1]t ... [an]t
val l1 : ?eq:('b -> 'b -> bool) -> ('a -> 'b) -> 'a signal -> 'b signalval l2 : ?eq:('c -> 'c -> bool) -> ('a -> 'b -> 'c) -> 'a signal -> 'b signal -> 'c signalval l3 : ?eq:('d -> 'd -> bool) -> ('a -> 'b -> 'c -> 'd) -> 'a signal -> 'b signal -> 'c signal -> 'd signalval l4 : ?eq:('e -> 'e -> bool) -> ('a -> 'b -> 'c -> 'd -> 'e) -> 'a signal -> 'b signal -> 'c signal -> 'd signal -> 'e signalval l5 : ?eq:('f -> 'f -> bool) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f) -> 'a signal -> 'b signal -> 'c signal -> 'd signal -> 'e signal -> 'f signalval l6 : ?eq:('g -> 'g -> bool) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) -> 'a signal -> 'b signal -> 'c signal -> 'd signal -> 'e signal -> 'f signal -> 'g signal
module Bool : sig ... endmodule Int : sig ... endmodule Float : sig ... endmodule Pair : sig ... endmodule Option : sig ... endmodule Compare : sig ... endCombinator specialization
Given an equality function equal and a type t, the functor Make automatically applies the eq parameter of the combinators. The outcome is combinators whose results are signals with values in t.
Basic types are already specialized in the module Special, open this module to use them.
module Make : functor (Eq : EqType) -> S with type 'a v = 'a Eq.tFunctor specializing the combinators for the given signal value type
module Special : sig ... endSpecialization for booleans, integers and floats.