- Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathRescriptReactRouter.res
214 lines (182 loc) · 5.83 KB
/
RescriptReactRouter.res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
@scope("globalThis")
externalwindow: option<Dom.window> ="window"
@scope("globalThis")
externalhistory: option<Dom.history> ="history"
@getexternallocation: Dom.window=>Dom.location="location"
/* actually the cb is Dom.event => unit, but let's restrict the access for now */
@send
externaladdEventListener: (Dom.window, string, unit=>unit) =>unit="addEventListener"
@send
externalremoveEventListener: (Dom.window, string, unit=>unit) =>unit="removeEventListener"
@send
externaldispatchEvent: (Dom.window, Dom.event) =>unit="dispatchEvent"
@getexternalpathname: Dom.location=>string="pathname"
@getexternalhash: Dom.location=>string="hash"
@getexternalsearch: Dom.location=>string="search"
@send
externalpushState: (Dom.history, @as(json`null`) _, @as("") _, ~href: string) =>unit="pushState"
@send
externalreplaceState: (Dom.history, @as(json`null`) _, @as("") _, ~href: string) =>unit=
"replaceState"
@valexternalevent: 'a="Event"
@newexternalmakeEventIE11Compatible: string=>Dom.event="Event"
@val @scope("document")
externalcreateEventNonIEBrowsers: string=>Dom.event="createEvent"
@send
externalinitEventNonIEBrowsers: (Dom.event, string, bool, bool) =>unit="initEvent"
letsafeMakeEvent=eventName=>
ifJs.typeof(event) =="function" {
makeEventIE11Compatible(eventName)
} else {
letevent=createEventNonIEBrowsers("Event")
initEventNonIEBrowsers(event, eventName, true, true)
event
}
/* This is copied from array.ml. We want to cut dependencies for rescript-react so
that it's friendlier to use in size-constrained codebases */
letarrayToList=a=> {
letrectolist= (i, res) =>
ifi < 0 {
res
} else {
tolist(i-1, list{a->Js.Array2.unsafe_get(i), ...res})
}
tolist(a->Js.Array2.length-1, list{})
}
/* if we ever roll our own parser in the future, make sure you test all url combinations
e.g. foo.com/?#bar
*/
/* sigh URLSearchParams doesn't work on IE11, edge16, etc. */
/* actually you know what, not gonna provide search for now. It's a mess.
We'll let users roll their own solution/data structure for now */
letpathParse=str=>
switchstr {
| ""
| "/"=>
list{}
| raw=>
/* remove the preceeding /, which every pathname seems to have */
letraw=raw->Js.String2.sliceToEnd(~from=1)
/* remove the trailing /, which some pathnames might have. Ugh */
letraw=switchraw->Js.String2.get(raw->Js.String2.length-1) {
| "/"=>raw->Js.String2.slice(~from=0, ~to_=-1)
| _=>raw
}
/* remove search portion if present in string */
letraw=switchraw->Js.String2.splitAtMost("?", ~limit=2) {
| [path, _] =>path
| _=>raw
}
raw->Js.String2.split("/")->Js.Array2.filter(item=>item->Js.String2.length!=0)->arrayToList
}
letpath= (~serverUrlString=?, ()) =>
switch (serverUrlString, window) {
| (None, None) =>list{}
| (Some(serverUrlString), _) =>pathParse(serverUrlString)
| (_, Some(window: Dom.window)) =>pathParse(window->location->pathname)
}
lethash= () =>
switchwindow {
| None=>""
| Some(window: Dom.window) =>
switchwindow->location->hash {
| ""
| "#"=>""
| raw=>
/* remove the preceeding #, which every hash seems to have.
Why is this even included in location.hash?? */
raw->Js.String2.sliceToEnd(~from=1)
}
}
letsearchParse=str=>
switchstr {
| ""
| "?"=>""
| raw=>
switchraw->Js.String2.splitAtMost("?", ~limit=2) {
| [_, search] =>search
| _=>""
}
}
letsearch= (~serverUrlString=?, ()) =>
switch (serverUrlString, window) {
| (None, None) =>""
| (Some(serverUrlString), _) =>searchParse(serverUrlString)
| (_, Some(window: Dom.window)) =>searchParse(window->location->search)
}
letpush=path=>
switch (history, window) {
| (None, _)
| (_, None) => ()
| (Some(history: Dom.history), Some(window: Dom.window)) =>
pushState(history, ~href=path)
dispatchEvent(window, safeMakeEvent("popstate"))
}
letreplace=path=>
switch (history, window) {
| (None, _)
| (_, None) => ()
| (Some(history: Dom.history), Some(window: Dom.window)) =>
replaceState(history, ~href=path)
dispatchEvent(window, safeMakeEvent("popstate"))
}
typeurl= {
path: list<string>,
hash: string,
search: string,
}
leturlNotEqual= (a, b) => {
letreclistNotEqual= (aList, bList) =>
switch (aList, bList) {
| (list{}, list{}) =>false
| (list{}, list{_, ..._})
| (list{_, ..._}, list{}) =>true
| (list{aHead, ...aRest}, list{bHead, ...bRest}) =>
ifaHead!==bHead {
true
} else {
listNotEqual(aRest, bRest)
}
}
a.hash!==b.hash|| (a.search!==b.search||listNotEqual(a.path, b.path))
}
typewatcherID=unit=>unit
leturl= (~serverUrlString=?, ()) => {
path: path(~serverUrlString?, ()),
hash: hash(),
search: search(~serverUrlString?, ()),
}
/* alias exposed publicly */
letdangerouslyGetInitialUrl=url
letwatchUrl=callback=>
switchwindow {
| None=> () => ()
| Some(window: Dom.window) =>
letwatcherID= () =>callback(url())
addEventListener(window, "popstate", watcherID)
watcherID
}
letunwatchUrl=watcherID=>
switchwindow {
| None=> ()
| Some(window: Dom.window) =>removeEventListener(window, "popstate", watcherID)
}
letuseUrl= (~serverUrl=?, ()) => {
let (url, setUrl) =React.useState(() =>
switchserverUrl {
| Some(url) =>url
| None=>dangerouslyGetInitialUrl()
}
)
React.useEffect(() => {
letwatcherId=watchUrl(url=>setUrl(_=>url))
// check for updates that may have occured between the initial state and
// the subscribe above
letnewUrl=dangerouslyGetInitialUrl()
ifurlNotEqual(newUrl, url) {
setUrl(_=>newUrl)
}
Some(() =>unwatchUrl(watcherId))
}, [])
url
}