forked from rescript-lang/rescript-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRescriptReactRouter.bs.js
221 lines (201 loc) · 5.44 KB
/
RescriptReactRouter.bs.js
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
215
216
217
218
219
220
221
// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';
var Curry = require("rescript/lib/js/curry.js");
var React = require("react");
var Js_array = require("rescript/lib/js/js_array.js");
var Js_string = require("rescript/lib/js/js_string.js");
function safeMakeEvent(eventName) {
if (typeof Event === "function") {
return new Event(eventName);
}
var $$event = document.createEvent("Event");
$$event.initEvent(eventName, true, true);
return $$event;
}
function pathParse(str) {
switch (str) {
case "" :
case "/" :
return /* [] */0;
default:
var raw = Js_string.sliceToEnd(1, str);
var match = raw[raw.length - 1 | 0];
var raw$1 = match === "/" ? Js_string.slice(0, -1, raw) : raw;
var match$1 = Js_string.splitAtMost("?", 2, raw$1);
var raw$2 = match$1.length !== 2 ? raw$1 : match$1[0];
var a = Js_array.filter((function (item) {
return item.length !== 0;
}), Js_string.split("/", raw$2));
var _i = a.length - 1 | 0;
var _res = /* [] */0;
while(true) {
var res = _res;
var i = _i;
if (i < 0) {
return res;
}
_res = {
hd: a[i],
tl: res
};
_i = i - 1 | 0;
continue ;
};
}
}
function path(serverUrlString, param) {
var match = typeof window === "undefined" ? undefined : window;
if (serverUrlString !== undefined) {
return pathParse(serverUrlString);
} else if (match !== undefined) {
return pathParse(match.location.pathname);
} else {
return /* [] */0;
}
}
function hash(param) {
var $$window = typeof window === "undefined" ? undefined : window;
if ($$window === undefined) {
return "";
}
var raw = $$window.location.hash;
switch (raw) {
case "" :
case "#" :
return "";
default:
return Js_string.sliceToEnd(1, raw);
}
}
function searchParse(str) {
switch (str) {
case "" :
case "?" :
return "";
default:
var match = Js_string.splitAtMost("?", 2, str);
if (match.length !== 2) {
return "";
} else {
return match[1];
}
}
}
function search(serverUrlString, param) {
var match = typeof window === "undefined" ? undefined : window;
if (serverUrlString !== undefined) {
return searchParse(serverUrlString);
} else if (match !== undefined) {
return searchParse(match.location.search);
} else {
return "";
}
}
function push(path) {
var match = typeof history === "undefined" ? undefined : history;
var match$1 = typeof window === "undefined" ? undefined : window;
if (match !== undefined && match$1 !== undefined) {
match.pushState(null, "", path);
match$1.dispatchEvent(safeMakeEvent("popstate"));
return ;
}
}
function replace(path) {
var match = typeof history === "undefined" ? undefined : history;
var match$1 = typeof window === "undefined" ? undefined : window;
if (match !== undefined && match$1 !== undefined) {
match.replaceState(null, "", path);
match$1.dispatchEvent(safeMakeEvent("popstate"));
return ;
}
}
function urlNotEqual(a, b) {
if (a.hash !== b.hash || a.search !== b.search) {
return true;
} else {
var _aList = a.path;
var _bList = b.path;
while(true) {
var bList = _bList;
var aList = _aList;
if (!aList) {
if (bList) {
return true;
} else {
return false;
}
}
if (!bList) {
return true;
}
if (aList.hd !== bList.hd) {
return true;
}
_bList = bList.tl;
_aList = aList.tl;
continue ;
};
}
}
function url(serverUrlString, param) {
return {
path: path(serverUrlString, undefined),
hash: hash(undefined),
search: search(serverUrlString, undefined)
};
}
function watchUrl(callback) {
var $$window = typeof window === "undefined" ? undefined : window;
if ($$window === undefined) {
return function (param) {
};
}
var watcherID = function (param) {
Curry._1(callback, url(undefined, undefined));
};
$$window.addEventListener("popstate", watcherID);
return watcherID;
}
function unwatchUrl(watcherID) {
var $$window = typeof window === "undefined" ? undefined : window;
if ($$window !== undefined) {
$$window.removeEventListener("popstate", watcherID);
return ;
}
}
function useUrl(serverUrl, param) {
var match = React.useState(function (param) {
if (serverUrl !== undefined) {
return serverUrl;
} else {
return url(undefined, undefined);
}
});
var setUrl = match[1];
var url$1 = match[0];
React.useEffect((function (param) {
var watcherId = watchUrl(function (url) {
Curry._1(setUrl, (function (param) {
return url;
}));
});
var newUrl = url(undefined, undefined);
if (urlNotEqual(newUrl, url$1)) {
Curry._1(setUrl, (function (param) {
return newUrl;
}));
}
return (function (param) {
unwatchUrl(watcherId);
});
}), []);
return url$1;
}
var dangerouslyGetInitialUrl = url;
exports.push = push;
exports.replace = replace;
exports.watchUrl = watchUrl;
exports.unwatchUrl = unwatchUrl;
exports.dangerouslyGetInitialUrl = dangerouslyGetInitialUrl;
exports.useUrl = useUrl;
/* react Not a pure module */