@@ -33,6 +33,12 @@ external createEventNonIEBrowsers: string => Dom.event = "createEvent"
33
33
@send
34
34
external initEventNonIEBrowsers : (Dom .event , string , bool , bool ) => unit = "initEvent"
35
35
36
+ @val @scope ("globalThis" )
37
+ external window : option <Dom .window > = "window"
38
+
39
+ @val @scope ("globalThis" )
40
+ external history : option <Dom .history > = "history"
41
+
36
42
let safeMakeEvent = eventName =>
37
43
if Js .typeof (event ) == "function" {
38
44
makeEventIE11Compatible (eventName )
@@ -60,9 +66,9 @@ let arrayToList = a => {
60
66
/* actually you know what, not gonna provide search for now. It's a mess.
61
67
We'll let users roll their own solution/data structure for now */
62
68
let path = () =>
63
- switch % external ( window ) {
69
+ switch window {
64
70
| None => list {}
65
- | Some (window : Dom . window ) =>
71
+ | Some (window ) =>
66
72
switch window |> location |> pathname {
67
73
| ""
68
74
| "/" => list {}
@@ -78,9 +84,9 @@ let path = () =>
78
84
}
79
85
}
80
86
let hash = () =>
81
- switch % external ( window ) {
87
+ switch window {
82
88
| None => ""
83
- | Some (window : Dom . window ) =>
89
+ | Some (window ) =>
84
90
switch window |> location |> hash {
85
91
| ""
86
92
| "#" => ""
@@ -91,9 +97,9 @@ let hash = () =>
91
97
}
92
98
}
93
99
let search = () =>
94
- switch % external ( window ) {
100
+ switch window {
95
101
| None => ""
96
- | Some (window : Dom . window ) =>
102
+ | Some (window ) =>
97
103
switch window |> location |> search {
98
104
| ""
99
105
| "?" => ""
@@ -103,18 +109,18 @@ let search = () =>
103
109
}
104
110
}
105
111
let push = path =>
106
- switch (% external ( history ), % external ( window ) ) {
112
+ switch (history , window ) {
107
113
| (None , _ )
108
114
| (_ , None ) => ()
109
- | (Some (history : Dom . history ), Some (window : Dom . window )) =>
115
+ | (Some (history ), Some (window )) =>
110
116
pushState (history , ~href = path )
111
117
dispatchEvent (window , safeMakeEvent ("popstate" ))
112
118
}
113
119
let replace = path =>
114
- switch (% external ( history ), % external ( window ) ) {
120
+ switch (history , window ) {
115
121
| (None , _ )
116
122
| (_ , None ) => ()
117
- | (Some (history : Dom . history ), Some (window : Dom . window )) =>
123
+ | (Some (history ), Some (window )) =>
118
124
replaceState (history , ~href = path )
119
125
dispatchEvent (window , safeMakeEvent ("popstate" ))
120
126
}
@@ -143,17 +149,17 @@ let url = () => {path: path(), hash: hash(), search: search()}
143
149
/* alias exposed publicly */
144
150
let dangerouslyGetInitialUrl = url
145
151
let watchUrl = callback =>
146
- switch % external ( window ) {
152
+ switch window {
147
153
| None => () => ()
148
- | Some (window : Dom . window ) =>
154
+ | Some (window ) =>
149
155
let watcherID = () => callback (url ())
150
156
addEventListener (window , "popstate" , watcherID )
151
157
watcherID
152
158
}
153
159
let unwatchUrl = watcherID =>
154
- switch % external ( window ) {
160
+ switch window {
155
161
| None => ()
156
- | Some (window : Dom . window ) => removeEventListener (window , "popstate" , watcherID )
162
+ | Some (window ) => removeEventListener (window , "popstate" , watcherID )
157
163
}
158
164
159
165
let useUrl = (~serverUrl = ?, ()) => {
0 commit comments