Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
React switch with newly created events.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-12-23 (12:37)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: React switch with newly created events.

I've recently been working again of React, and I have the following 
question. Current code is below.

I'm creating an event with React.E.switch, that creates a new event 
through the schedule function, and this new events replaces the old 
event. React.E.switch is used for this replacement.

React.E.switch initial_event ( begin function () -> schedule 
(rescheduler ()) end tick)

However, it may happen (and it does happen) that as soon as this new 
React.event is created, an event is fired before the React.E.switch has 
been executed to replace the old event by the new event.

This is due to Lwt concurrency.

I therefore have two potential solutions:

-1- clutter my code with mutexes to synchronise the whole stuff, with 
the disadvantage that there is not, to my knowledge, to execute a 
function just after the React.E.switch function has effectively switched 

-2- look for a way in React to do it within React only. Which would mean 
to somehow implement within React a way to switch to a newly created 
event, without race conditions.

What would you do in this context?

Here's the code:

> let reschedule ?attach:(f = begin fun x -> x end) start rescheduler =
>   (* The let define tick in E.fix define is the proper way to
>      implement recursive events. define has type React.event ->
>      (React.event * React.event) and its argument is a placeholder
>      for the event at time t-dt. *)
>   let attach initial_event =
>     let define tick =
>       (* Here is something I worry about: It is possible that the event created
>          with the schedule function is fired before it is attached to the switched
>          event, hence losing an event, and stalling the whole regular event. *)
>       let tick' = React.E.switch initial_event (
>         begin function () -> schedule (rescheduler ()) end tick) in
>       tick', tick'
>     in f (React.E.fix define) in
>   schedule ~attach:attach start
> let regular_schedule ?attach:(f = begin fun x -> x end) start period =
>   reschedule ~attach:f start (fun () -> Calendar.add ( ()) period)

      Guillaume Yziquel