Skip to content

Get rid of %external usage #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/RescriptReactRouter.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions src/RescriptReactRouter.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
@scope("globalThis")
external window: option<Dom.window> = "window"

@scope("globalThis")
external history: option<Dom.history> = "history"

@get external location: Dom.window => Dom.location = "location"

/* actually the cb is Dom.event => unit, but let's restrict the access for now */
Expand Down Expand Up @@ -81,13 +87,13 @@ let pathParse = str =>
raw |> Js.String.split("/") |> Js.Array.filter(item => String.length(item) != 0) |> arrayToList
}
let path = (~serverUrlString=?, ()) =>
switch (serverUrlString, %external(window)) {
switch (serverUrlString, window) {
| (None, None) => list{}
| (Some(serverUrlString), _) => pathParse(serverUrlString)
| (_, Some(window: Dom.window)) => pathParse(window |> location |> pathname)
}
let hash = () =>
switch %external(window) {
switch window {
| None => ""
| Some(window: Dom.window) =>
switch window |> location |> hash {
Expand All @@ -111,14 +117,14 @@ let searchParse = str =>
}

let search = (~serverUrlString=?, ()) =>
switch (serverUrlString, %external(window)) {
switch (serverUrlString, window) {
| (None, None) => ""
| (Some(serverUrlString), _) => searchParse(serverUrlString)
| (_, Some(window: Dom.window)) => searchParse(window |> location |> search)
}

let push = path =>
switch (%external(history), %external(window)) {
switch (history, window) {
| (None, _)
| (_, None) => ()
| (Some(history: Dom.history), Some(window: Dom.window)) =>
Expand All @@ -127,7 +133,7 @@ let push = path =>
}

let replace = path =>
switch (%external(history), %external(window)) {
switch (history, window) {
| (None, _)
| (_, None) => ()
| (Some(history: Dom.history), Some(window: Dom.window)) =>
Expand Down Expand Up @@ -169,7 +175,7 @@ let url = (~serverUrlString=?, ()) => {
let dangerouslyGetInitialUrl = url

let watchUrl = callback =>
switch %external(window) {
switch window {
| None => () => ()
| Some(window: Dom.window) =>
let watcherID = () => callback(url())
Expand All @@ -178,7 +184,7 @@ let watchUrl = callback =>
}

let unwatchUrl = watcherID =>
switch %external(window) {
switch window {
| None => ()
| Some(window: Dom.window) => removeEventListener(window, "popstate", watcherID)
}
Expand Down
Loading