-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathRescriptReactRouter.res.js
More file actions
196 lines (181 loc) · 4.47 KB
/
Copy pathRescriptReactRouter.res.js
File metadata and controls
196 lines (181 loc) · 4.47 KB
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
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as React from "react";
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
function pathParse(str) {
switch (str) {
case "" :
case "/" :
return /* [] */0;
default:
let raw = str.slice(1);
let match = raw[raw.length - 1 | 0];
let raw$1 = match === "/" ? raw.slice(0, -1) : raw;
let match$1 = raw$1.split("?", 2);
let raw$2 = match$1.length !== 2 ? raw$1 : match$1[0];
let a = raw$2.split("/").filter(item => item.length !== 0);
let _i = a.length - 1 | 0;
let _res = /* [] */0;
while (true) {
let res = _res;
let i = _i;
if (i < 0) {
return res;
}
_res = {
hd: a[i],
tl: res
};
_i = i - 1 | 0;
continue;
};
}
}
function path(serverUrlString, param) {
let match = globalThis.window;
if (serverUrlString !== undefined) {
return pathParse(serverUrlString);
} else if (match !== undefined) {
return pathParse(Primitive_option.valFromOption(match).location.pathname);
} else {
return /* [] */0;
}
}
function hash() {
let window = globalThis.window;
if (window === undefined) {
return "";
}
let raw = Primitive_option.valFromOption(window).location.hash;
switch (raw) {
case "" :
case "#" :
return "";
default:
return raw.slice(1);
}
}
function searchParse(str) {
switch (str) {
case "" :
case "?" :
return "";
default:
let match = str.split("?", 2);
if (match.length !== 2) {
return "";
} else {
return match[1];
}
}
}
function search(serverUrlString, param) {
let match = globalThis.window;
if (serverUrlString !== undefined) {
return searchParse(serverUrlString);
} else if (match !== undefined) {
return searchParse(Primitive_option.valFromOption(match).location.search);
} else {
return "";
}
}
function push(path) {
let match = globalThis.history;
let match$1 = globalThis.window;
if (match !== undefined && match$1 !== undefined) {
Primitive_option.valFromOption(match).pushState(null, "", path);
Primitive_option.valFromOption(match$1).dispatchEvent(new Event("popstate", {
bubbles: true,
cancelable: true
}));
return;
}
}
function replace(path) {
let match = globalThis.history;
let match$1 = globalThis.window;
if (match !== undefined && match$1 !== undefined) {
Primitive_option.valFromOption(match).replaceState(null, "", path);
Primitive_option.valFromOption(match$1).dispatchEvent(new Event("popstate", {
bubbles: true,
cancelable: true
}));
return;
}
}
function urlNotEqual(a, b) {
if (a.hash !== b.hash || a.search !== b.search) {
return true;
} else {
let _aList = a.path;
let _bList = b.path;
while (true) {
let bList = _bList;
let aList = _aList;
if (aList === 0) {
return bList !== 0;
}
if (bList === 0) {
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(),
search: search(serverUrlString, undefined)
};
}
function watchUrl(callback) {
let window = globalThis.window;
if (window === undefined) {
return () => {};
}
let watcherID = () => callback(url(undefined, undefined));
Primitive_option.valFromOption(window).addEventListener("popstate", watcherID);
return watcherID;
}
function unwatchUrl(watcherID) {
let window = globalThis.window;
if (window !== undefined) {
Primitive_option.valFromOption(window).removeEventListener("popstate", watcherID);
return;
}
}
function useUrl(serverUrl, param) {
let match = React.useState(() => {
if (serverUrl !== undefined) {
return serverUrl;
} else {
return url(undefined, undefined);
}
});
let setUrl = match[1];
let url$1 = match[0];
React.useEffect(() => {
let watcherId = watchUrl(url => setUrl(param => url));
let newUrl = url(undefined, undefined);
if (urlNotEqual(newUrl, url$1)) {
setUrl(param => newUrl);
}
return () => unwatchUrl(watcherId);
}, []);
return url$1;
}
let dangerouslyGetInitialUrl = url;
export {
push,
replace,
watchUrl,
unwatchUrl,
dangerouslyGetInitialUrl,
useUrl,
}
/* react Not a pure module */