Skip to content

Switch to rescript webapi#26

Merged
pbiggar merged 5 commits intodarklang:masterfrom
OceanOak:switch-to-rescript-webapi
Sep 17, 2022
Merged

Switch to rescript webapi#26
pbiggar merged 5 commits intodarklang:masterfrom
OceanOak:switch-to-rescript-webapi

Conversation

@OceanOak
Copy link
Copy Markdown
Contributor

@OceanOak OceanOak commented Sep 9, 2022

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

    let removeItem = key =>
    nativeBinding(cb =>{
      let value=Dom.Storage.removeItem(key, Dom.Storage.localStorage)
      if(Js.testAny( value)) {
      cb(Error("localStorage is not available"))}
      else{
        cb(Ok(value))}})

I followed the same process in the following functions:

  • length
  • clear
  • removeItem
  • setItem

Is there a better way to do this?

tea_animationframe.res

  • Used Js.Date instead of Web.Date
  • Used Webapi's "requestCancellableAnimationFrame and "cancelAnimationFrame" instead of Web.Window 's "requestAnimationFrame" and "cancelAnimationFrame".

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.

Copy link
Copy Markdown
Member

@pbiggar pbiggar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! A few questions, and we should be good to merge.

Comment thread src/tea_animationframe.res Outdated
@@ -1,3 +1,5 @@
open Webapi
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to not open this module, and just use a qualified in each place instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are done.

Comment thread src/tea_animationframe.res Outdated
Comment thread src/tea_html_cmds.res Outdated
@@ -1,14 +1,19 @@

open Webapi
open Webapi.Dom
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think it would be better to skip the opens

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are done.

@OceanOak OceanOak requested a review from pbiggar September 10, 2022 14:46
@pbiggar
Copy link
Copy Markdown
Member

pbiggar commented Sep 10, 2022

let removeItem = key =>
nativeBinding(cb =>{
let value=Dom.Storage.removeItem(key, Dom.Storage.localStorage)
if(Js.testAny( value)) {
cb(Error("localStorage is not available"))}
else{
cb(Ok(value))}})

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?

@OceanOak
Copy link
Copy Markdown
Contributor Author

Running an availability test in the Js file throws this exception if the localstorage is not available:
localStorage is not defined

Testing it with undefined (Js.Nullable.undefined or Js.undefined) in the .res file throws an error.
This has type: Js.undefined<'a>
or
This has type: Js.Nullable.t<'a>
Somewhere wanted: Dom.Storage.t

I hope this answers your question. If it doesn't let me know.

@pbiggar
Copy link
Copy Markdown
Member

pbiggar commented Sep 12, 2022

If it throws an exception, then I guess we should catch the exception instead of comparing it. Right?

@OceanOak
Copy link
Copy Markdown
Contributor Author

Is that better?

@pbiggar
Copy link
Copy Markdown
Member

pbiggar commented Sep 15, 2022

Needs to be try/catch: https://rescript-lang.org/docs/manual/latest/exception

@OceanOak
Copy link
Copy Markdown
Contributor Author

I have tried using try/catch here is an example:

  let removeItemCmd = key => Tea_task.attemptOpt(_ => None, removeItem(key))
  let setItem = (key, value) =>
    nativeBinding(cb =>{
      try Dom.Storage.setItem(key, value, Dom.Storage.localStorage)
      catch{
        | Not_found=> cb(Error("localStorage is not available"))
      }}
    )

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.
Should I use try/catch for all of them and leave "length" as it is or is there a way to use try/catch for it ?

@pbiggar
Copy link
Copy Markdown
Member

pbiggar commented Sep 15, 2022

It works for all the functions except for "length" function because it returns an integer and "try" needs a unit type.

It's not that try needs a unit type, it's not try and catch need to have the same time. Something like this:

let length = nativeBinding(cb =>{
      try {
        cb(Ok(Dom.Storage.length(Dom.Storage.localStorage)))
       } catch {
      | exception Not_found =>cb(Error("localStorage is not available"))
    }}
  )

@OceanOak
Copy link
Copy Markdown
Contributor Author

It works for all the functions except for "length" function because it returns an integer and "try" needs a unit type.

It's not that try needs a unit type, it's not try and catch need to have the same time. Something like this:

let length = nativeBinding(cb =>{
      try {
        cb(Ok(Dom.Storage.length(Dom.Storage.localStorage)))
       } catch {
      | exception Not_found =>cb(Error("localStorage is not available"))
    }}
  )

I really don't know why I removed cb(Ok(...)) in the first place. Sorry for that and thanks for the information.

Copy link
Copy Markdown
Member

@pbiggar pbiggar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly there, thanks!

Comment thread src/tea_ex.res Outdated
try {
cb(Ok(Dom.Storage.length(Dom.Storage.localStorage)))
} catch {
| Not_found => cb(Error("localStorage is not available"))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why this is Not_found. I think any exception should go here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for all of these

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| _e => cb(Error("localStorage is not available")) - yeah, exactly

Comment thread src/tea_ex.res Outdated
let getItem = key =>
nativeBinding(cb =>
switch Web.Window.LocalStorage.getItem(Web.Window.window, key) {
switch Dom.Storage.getItem(key, Dom.Storage.localStorage) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use try

Comment thread src/tea_ex.res Outdated
let key = idx =>
nativeBinding(cb =>
switch Web.Window.LocalStorage.key(Web.Window.window, idx) {
switch Dom.Storage.key(idx, Dom.Storage.localStorage) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use try

@OceanOak OceanOak requested a review from pbiggar September 15, 2022 20:53
@pbiggar pbiggar merged commit a5a2b20 into darklang:master Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants