ml-in-barcelona / jsoo-react

js_of_ocaml bindings for ReactJS. Based on ReasonReact.
https://ml-in-barcelona.github.io/jsoo-react
MIT License
138 stars 19 forks source link

Keep useReducer dispatch and useState updater refs constant #101

Closed jchavarri closed 2 years ago

jchavarri commented 2 years ago

Fixes #100.

The fix consist mostly on inlining the previously generated code by gen_js_api and removing the wrapping over the following functions:

Both functions can take only 1 argument, so I don't see any reason why the currying handling is needed in these case.

Added some tests for this as well as a base test for useState, which was not existent.

jchavarri commented 2 years ago

Here's an example of the transformation:

let (useState_internal :
      Imports.react -> (unit -> 'state) -> 'state * (('state -> 'state) -> unit)
      ) =
 fun (react : Imports.react) (init : unit -> 'state) ->
  let result =
    Ojs.call
      (Imports.react_to_js react)
      "useState"
      [|Ojs.fun_to_js 1 (fun _ -> Obj.magic (init ()))|]
  in
  ( Obj.magic (Ojs.array_get result 0)
- , fun (updater : 'state -> 'state) ->
-     ignore
-       (Ojs.apply (Ojs.array_get result 1)
-          [| Ojs.fun_to_js 1 (fun (data : Ojs.t) ->
-                 Obj.magic (updater (Obj.magic data)) ) |] ) )
+ Obj.magic (Ojs.array_get result 1))

let useState initial = useState_internal Imports.react initial