Switch to rescript webapi#26
Conversation
pbiggar
left a comment
There was a problem hiding this comment.
This looks great! A few questions, and we should be good to merge.
| @@ -1,3 +1,5 @@ | |||
| open Webapi | |||
There was a problem hiding this comment.
I think it would be better to not open this module, and just use a qualified in each place instead.
| @@ -1,14 +1,19 @@ | |||
|
|
|||
| open Webapi | |||
| open Webapi.Dom | |||
There was a problem hiding this comment.
Again, I think it would be better to skip the opens
It looks like localstorage is pretty pervasive: it's got 97% on canIuse. If local storage was unavailable, would this be undefined or throw an exception? If undefined, maybe test for that directly? |
|
Running an availability test in the Js file throws this exception if the localstorage is not available: Testing it with undefined (Js.Nullable.undefined or Js.undefined) in the .res file throws an error. I hope this answers your question. If it doesn't let me know. |
|
If it throws an exception, then I guess we should catch the exception instead of comparing it. Right? |
|
Is that better? |
|
Needs to be try/catch: https://rescript-lang.org/docs/manual/latest/exception |
|
I have tried using try/catch here is an example: It works for all the functions except for "length" function because it returns an integer and "try" needs a unit type. That is why I used the second way of handling exeptions which I found in this manual. |
It's not that try needs a unit type, it's not try and catch need to have the same time. Something like this: |
I really don't know why I removed cb(Ok(...)) in the first place. Sorry for that and thanks for the information. |
| try { | ||
| cb(Ok(Dom.Storage.length(Dom.Storage.localStorage))) | ||
| } catch { | ||
| | Not_found => cb(Error("localStorage is not available")) |
There was a problem hiding this comment.
It's not clear to me why this is Not_found. I think any exception should go here.
There was a problem hiding this comment.
Do we know what other exceptions do we need to catch ?
Do we need to catch
| Invalid_argument(_) =>...
( if yes, should I change the error message?)
Do we need to catch JS Exceptions using :
| Js.Exn.Error(obj)
even though they don't encourage that in the documentation.
Or is there a better way to catch all exceptions without writing them one by one?
I don't know if it is correct to do something like that
| _e => cb(Error("localStorage is not available"))
There was a problem hiding this comment.
| _e => cb(Error("localStorage is not available")) - yeah, exactly
| let getItem = key => | ||
| nativeBinding(cb => | ||
| switch Web.Window.LocalStorage.getItem(Web.Window.window, key) { | ||
| switch Dom.Storage.getItem(key, Dom.Storage.localStorage) { |
| let key = idx => | ||
| nativeBinding(cb => | ||
| switch Web.Window.LocalStorage.key(Web.Window.window, idx) { | ||
| switch Dom.Storage.key(idx, Dom.Storage.localStorage) { |
Changes made :
tea_ex.res
Some functions used to return option type, their equivalents in Rescript API return "unit" which we can't pass in a switch statement. I instead used if-else expressions. Here is an example of how it was used
I followed the same process in the following functions:
Is there a better way to do this?
tea_animationframe.res
tea_time.res
Instead of Web.Window's "setInterval" and "clearTimeout", I used Js.Global module "setIntervalFloat" and "clearInterval"
Note: There are still other files that need to be switched, currentely working on them.