moduleP = PreactmoduleClock = structlet make =
fun [@preact.component "Clock"] () ->
let[@hook] isRunning, setIsRunning = P.useState trueinlet[@hook] now, setNow = P.useState (Js.Date.make ()) inlet[@hook] () = AnimationFrame.use (isRunning, fun _ -> setNow (Js.Date.make ())) inlet f n = P.string ((if n < 10.0then"0"else"") ^ Js.Float.toString n) inP.h2
[ P.style "text-align""center" ]
[ f (Js.Date.getHours now)
; P.string":"
; f (Js.Date.getMinutes now)
; P.string":"
; f (Js.Date.getSeconds now)
; P.div
[]
[ P.button
[ P.onClick (fun _ -> setIsRunning (not isRunning)) ]
[ P.string (if isRunning then"Stop"else"Start") ]
]
]
endlet() =
matchP.find "main"with
| Some element -> P.render (Clock.make ()) element
| None -> Js.Console.error "<main> not found!"
AnimationFrame.ml
moduleP = Preactlet use =
fun [@preact.hook] (enabled, func) ->
let[@hook] () =
P.useEffect
(fun() ->
let id = refNoneinletrec loop t =
let() = func t inlet() = id := Some (Webapi.requestCancellableAnimationFrame loop) in()inlet() =
if enabled
then id := Some (Webapi.requestCancellableAnimationFrame loop)
else()inSome
(fun() ->
match !id with
| Some id -> Webapi.cancelAnimationFrame id
| None -> ()))
(P._1 enabled)
in()