'state tkeeps track of updates of type
'state -> 'statequeued to it and runs them sequentially.
This has the primary feature that if an update itself schedules another update, that other update will be run after the first update has finished.
For example, consider the code:
let x = create ~init:1 () in enqueue_update x (fun x -> enqueue_update (fun x -> x + 1); x + 1)
At the end,
x's state will be 2, as you would expect. However, suppose you did
this with an
let x = ref 1 in let update_x f = x := f !x in update_x (fun x -> update_x (fun x -> x + 1); x + 1 )
At the end of this,
!x would be 1.
Another feature is that the initial value does not have to be specified at creation.
If it is not, enqueued updates will be kept around until an initial value is specified
val create :
?init:'state -> unit -> ('a, 'state) t
val init :
(Core.Std.read_write, 'state) t -> 'state -> unit
val enqueue :
(Core.Std.read_write, 'state) t -> ('state -> 'state) -> unit
val watch :
('a, 'state) t -> f:('state -> unit) -> unit
watch t fwill call
fevery time that that
t's state is updated.
fshould not call
val map :
('a, 'state1) t -> f:('state1 -> 'state2) -> (Core.Std.read_only, 'state2) t
val read_only :
('a, 'state) t -> (Core.Std.read_only, 'state) t