caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* React switch with newly created events.
@ 2009-12-23 12:37 Guillaume Yziquel
  2009-12-23 13:23 ` Daniel Bünzli
  0 siblings, 1 reply; 3+ messages in thread
From: Guillaume Yziquel @ 2009-12-23 12:37 UTC (permalink / raw)
  To: OCaml List, Daniel Bünzli

Hello.

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 (React.E.map 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 
events.

-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 (React.E.map
>         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 (Calendar.now ()) period)

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: React switch with newly created events.
  2009-12-23 12:37 React switch with newly created events Guillaume Yziquel
@ 2009-12-23 13:23 ` Daniel Bünzli
  2009-12-23 13:54   ` Guillaume Yziquel
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Bünzli @ 2009-12-23 13:23 UTC (permalink / raw)
  To: guillaume.yziquel; +Cc: OCaml List

> 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.

You are creating a new update cycle inside another update cycle and
this is disallowed. No primitives are allowed to update in an update
cycle (to be precise unless the primitive does not belong to the
dependency graph of the update cycle).

> -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 events.

I don't understand what you are saying (a word must be missing). But
yes you need to serialize the updates, see
http://erratique.ch/software/react/doc/React#update but I guess you
already know that.
.
> -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.

You can perfectly switch to a newly created event, primitive or not.
If the event is not a primitive it may even have an occurence in the
same update cycle (e.g. if the dynamic creation depends on the
updating primitive itself). However if it's a primitive event it
cannot have an occurence in the same update cycle as this would start
a new update cycle and this is disallowed.

Daniel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: React switch with newly created events.
  2009-12-23 13:23 ` Daniel Bünzli
@ 2009-12-23 13:54   ` Guillaume Yziquel
  0 siblings, 0 replies; 3+ messages in thread
From: Guillaume Yziquel @ 2009-12-23 13:54 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: OCaml List

Daniel Bünzli a écrit :
>> 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.
> 
> You are creating a new update cycle inside another update cycle and
> this is disallowed. No primitives are allowed to update in an update
> cycle (to be precise unless the primitive does not belong to the
> dependency graph of the update cycle).

No, this is not the case. There's only one update cycle. I was confused 
and sked the question too quickly. It works perfectly...

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-12-23 13:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-23 12:37 React switch with newly created events Guillaume Yziquel
2009-12-23 13:23 ` Daniel Bünzli
2009-12-23 13:54   ` Guillaume Yziquel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).