-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from glennsl/feat/ml-hooks
Hooks using OCaml idioms and equality semantics
- Loading branch information
Showing
4 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
let use_ref initial = | ||
let state, _ = Core.use_state (fun () -> ref initial) in | ||
state | ||
|
||
let use_ref_lazy init = | ||
let state, _ = Core.use_state (fun () -> ref (init ())) in | ||
state | ||
|
||
let use_state initial = Core.use_state (fun () -> initial) | ||
let use_state_lazy init = Core.use_state init | ||
let use_reducer = Core.use_reducer | ||
|
||
let use_resource ~on:deps ?(equal = ( = )) ?before_render ~release acquire = | ||
let last_deps = use_ref deps in | ||
let resource = use_ref None in | ||
|
||
let release () = Option.iter release !resource in | ||
let acquire () = resource := Some (acquire ()) in | ||
|
||
let init () = | ||
acquire (); | ||
Some release | ||
in | ||
let update () = | ||
if not (equal deps !last_deps) then begin | ||
last_deps := deps; | ||
release (); | ||
acquire () | ||
end; | ||
None | ||
in | ||
|
||
Core.use_effect_once ?before_render init; | ||
Core.use_effect_always ?before_render update | ||
|
||
let use_effect ~on ?equal ?before_render ?(cleanup = fun () -> ()) f = | ||
use_resource ?before_render ~on ?equal ~release:cleanup f | ||
let use_effect_once ?before_render ?cleanup f = | ||
Core.use_effect_once ?before_render (fun () -> | ||
f (); | ||
cleanup) | ||
let use_effect_always ?before_render ?cleanup f = | ||
Core.use_effect_always ?before_render (fun () -> | ||
f (); | ||
cleanup) | ||
let use_memo ~on:deps ?(equal = ( = )) f = | ||
let last_deps = use_ref deps in | ||
let value = use_ref_lazy (fun () -> f ()) in | ||
if not (equal deps !last_deps) then begin | ||
last_deps := deps; | ||
value := f () | ||
end; | ||
!value | ||
let use_context = Core.use_context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
val use_ref : 'value -> 'value ref | ||
val use_ref_lazy : (unit -> 'value) -> 'value ref | ||
val use_state : 'state -> 'state * (('state -> 'state) -> unit) | ||
val use_state_lazy : (unit -> 'state) -> 'state * (('state -> 'state) -> unit) | ||
|
||
val use_reducer : | ||
init:(unit -> 'state) | ||
-> ('state -> 'action -> 'state) | ||
-> 'state * ('action -> unit) | ||
|
||
val use_effect : | ||
on:'deps | ||
-> ?equal:('deps -> 'deps -> bool) | ||
-> ?before_render:bool | ||
-> ?cleanup:(unit -> unit) | ||
-> (unit -> unit) | ||
-> unit | ||
|
||
val use_effect_once : | ||
?before_render:bool -> ?cleanup:(unit -> unit) -> (unit -> unit) -> unit | ||
|
||
val use_effect_always : | ||
?before_render:bool -> ?cleanup:(unit -> unit) -> (unit -> unit) -> unit | ||
|
||
val use_resource : | ||
on:'deps | ||
-> ?equal:('deps -> 'deps -> bool) | ||
-> ?before_render:bool | ||
-> release:('resource -> unit) | ||
-> (unit -> 'resource) | ||
-> unit | ||
|
||
val use_memo : | ||
on:'deps -> ?equal:('deps -> 'deps -> bool) -> (unit -> 'value) -> 'value | ||
|
||
val use_context : 'value Core.Context.t -> 'value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
include Core | ||
module Hooks = Hooks | ||
|
||
module Dom = struct | ||
include Dom | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters