( routes: RouteDefinition[], initialURL?: string, root: string = "" )
| 170 | } |
| 171 | |
| 172 | function createRouter( |
| 173 | routes: RouteDefinition[], |
| 174 | initialURL?: string, |
| 175 | root: string = "" |
| 176 | ): [Router, RouterActions] { |
| 177 | const recognizer = new RouteRecognizer<RouteHandler>(); |
| 178 | processRoutes(recognizer, routes, root); |
| 179 | |
| 180 | const [location, setLocation] = createSignal( |
| 181 | initialURL ? initialURL : window.location.pathname.replace(root, "") + window.location.search |
| 182 | ); |
| 183 | const current = createMemo( |
| 184 | () => |
| 185 | recognizer.recognize(root + location()) || ([] as unknown as RecognizeResults<RouteHandler>) |
| 186 | ); |
| 187 | const data: unknown[] = []; |
| 188 | const [pending, start] = useTransition(); |
| 189 | const [routeState, setRouteState] = createStore({ |
| 190 | params: {}, |
| 191 | query: {} |
| 192 | }); |
| 193 | const state = { |
| 194 | root, |
| 195 | get location() { |
| 196 | return location(); |
| 197 | }, |
| 198 | get pending() { |
| 199 | return pending(); |
| 200 | }, |
| 201 | get params() { |
| 202 | return routeState.params; |
| 203 | }, |
| 204 | get query() { |
| 205 | return routeState.query; |
| 206 | }, |
| 207 | level: 0 |
| 208 | }; |
| 209 | // make it non-enumerable |
| 210 | Object.defineProperties(state, { |
| 211 | current: { |
| 212 | get() { |
| 213 | return current(); |
| 214 | } |
| 215 | }, |
| 216 | data: { |
| 217 | value: data |
| 218 | } |
| 219 | }); |
| 220 | const actions: RouterActions = { |
| 221 | push(path) { |
| 222 | window.history.pushState("", "", root + path); |
| 223 | start( |
| 224 | () => setLocation(path), |
| 225 | () => window.scrollTo(0, 0) |
| 226 | ); |
| 227 | }, |
| 228 | replace(path) { |
| 229 | window.history.replaceState("", "", root + path); |
no test coverage detected
searching dependent graphs…