@@ -33,6 +33,12 @@ external createEventNonIEBrowsers: string => Dom.event = "createEvent"
3333@send
3434external initEventNonIEBrowsers : (Dom .event , string , bool , bool ) => unit = "initEvent"
3535
36+ @val @scope ("globalThis" )
37+ external window : option <Dom .window > = "window"
38+
39+ @val @scope ("globalThis" )
40+ external history : option <Dom .history > = "history"
41+
3642let safeMakeEvent = eventName =>
3743 if Js .typeof (event ) == "function" {
3844 makeEventIE11Compatible (eventName )
@@ -60,9 +66,9 @@ let arrayToList = a => {
6066/* actually you know what, not gonna provide search for now. It's a mess.
6167 We'll let users roll their own solution/data structure for now */
6268let path = () =>
63- switch % external ( window ) {
69+ switch window {
6470 | None => list {}
65- | Some (window : Dom . window ) =>
71+ | Some (window ) =>
6672 switch window |> location |> pathname {
6773 | ""
6874 | "/" => list {}
@@ -78,9 +84,9 @@ let path = () =>
7884 }
7985 }
8086let hash = () =>
81- switch % external ( window ) {
87+ switch window {
8288 | None => ""
83- | Some (window : Dom . window ) =>
89+ | Some (window ) =>
8490 switch window |> location |> hash {
8591 | ""
8692 | "#" => ""
@@ -91,9 +97,9 @@ let hash = () =>
9197 }
9298 }
9399let search = () =>
94- switch % external ( window ) {
100+ switch window {
95101 | None => ""
96- | Some (window : Dom . window ) =>
102+ | Some (window ) =>
97103 switch window |> location |> search {
98104 | ""
99105 | "?" => ""
@@ -103,18 +109,18 @@ let search = () =>
103109 }
104110 }
105111let push = path =>
106- switch (% external ( history ), % external ( window ) ) {
112+ switch (history , window ) {
107113 | (None , _ )
108114 | (_ , None ) => ()
109- | (Some (history : Dom . history ), Some (window : Dom . window )) =>
115+ | (Some (history ), Some (window )) =>
110116 pushState (history , ~href = path )
111117 dispatchEvent (window , safeMakeEvent ("popstate" ))
112118 }
113119let replace = path =>
114- switch (% external ( history ), % external ( window ) ) {
120+ switch (history , window ) {
115121 | (None , _ )
116122 | (_ , None ) => ()
117- | (Some (history : Dom . history ), Some (window : Dom . window )) =>
123+ | (Some (history ), Some (window )) =>
118124 replaceState (history , ~href = path )
119125 dispatchEvent (window , safeMakeEvent ("popstate" ))
120126 }
@@ -143,17 +149,17 @@ let url = () => {path: path(), hash: hash(), search: search()}
143149/* alias exposed publicly */
144150let dangerouslyGetInitialUrl = url
145151let watchUrl = callback =>
146- switch % external ( window ) {
152+ switch window {
147153 | None => () => ()
148- | Some (window : Dom . window ) =>
154+ | Some (window ) =>
149155 let watcherID = () => callback (url ())
150156 addEventListener (window , "popstate" , watcherID )
151157 watcherID
152158 }
153159let unwatchUrl = watcherID =>
154- switch % external ( window ) {
160+ switch window {
155161 | None => ()
156- | Some (window : Dom . window ) => removeEventListener (window , "popstate" , watcherID )
162+ | Some (window ) => removeEventListener (window , "popstate" , watcherID )
157163 }
158164
159165let useUrl = (~serverUrl = ?, ()) => {
0 commit comments