|
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +// IMPLEMENTATION |
| 4 | +function Router() { |
| 5 | + let listeners = []; |
| 6 | + let currentPath = location.pathname; |
| 7 | + let previousPath = null; |
| 8 | + |
| 9 | + const isMatch = (match, path) => |
| 10 | + (match instanceof RegExp && match.test(path)) || |
| 11 | + (typeof match === "function" && match(path)) || |
| 12 | + (typeof match === "string" && match === path); |
| 13 | + |
| 14 | + const handleListener = ({ match, onEnter, onLeave }) => { |
| 15 | + const args = { currentPath, previousPath, state: history.state }; |
| 16 | + |
| 17 | + isMatch(match, currentPath) && onEnter(args); |
| 18 | + onLeave && isMatch(match, previousPath) && onLeave(args); |
| 19 | + }; |
| 20 | + |
| 21 | + const handleAllListeners = () => listeners.forEach(handleListener); |
| 22 | + |
| 23 | + const generateId = () => { |
| 24 | + const getRandomNumber = () => |
| 25 | + Math.floor(Math.random() * listeners.length * 1000); |
| 26 | + const doesExist = (id) => listeners.find((listener) => listener.id === id); |
| 27 | + |
| 28 | + let id = getRandomNumber(); |
| 29 | + while (doesExist(id)) { |
| 30 | + id = getRandomNumber(); |
| 31 | + } |
| 32 | + return id; |
| 33 | + }; |
| 34 | + |
| 35 | + const on = (match, onEnter, onLeave) => { |
| 36 | + const id = generateId(); |
| 37 | + |
| 38 | + const listener = { id, match, onEnter, onLeave }; |
| 39 | + listeners.push(listener); |
| 40 | + handleListener(listener); |
| 41 | + |
| 42 | + return () => { |
| 43 | + listeners = listeners.filter((listeners) => listeners.id !== id); |
| 44 | + }; |
| 45 | + }; |
| 46 | + |
| 47 | + const go = (url, state) => { |
| 48 | + previousPath = currentPath; |
| 49 | + history.pushState(state, url, url); |
| 50 | + currentPath = location.pathname; |
| 51 | + |
| 52 | + handleAllListeners(); |
| 53 | + }; |
| 54 | + |
| 55 | + window.addEventListener("popstate", handleAllListeners); |
| 56 | + |
| 57 | + return { on, go }; |
| 58 | +} |
| 59 | + |
| 60 | +// USAGE |
| 61 | +const render = (content) => |
| 62 | + (document.getElementById("root").innerHTML = `<h2>${content}</h2>`); |
| 63 | + |
| 64 | +const createLogger = |
| 65 | + (content, shouldRender = true) => |
| 66 | + (...args) => { |
| 67 | + console.log(`LOGGER: ${content} args=${JSON.stringify(args)}`); |
| 68 | + if (shouldRender) { |
| 69 | + render(content); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | +const router = Router(); |
| 74 | + |
| 75 | +const unsubscribe = router.on(/.*/, createLogger("/.*")); |
| 76 | +router.on( |
| 77 | + (path) => path === "/contacts", |
| 78 | + createLogger("/contacts"), // onEnter |
| 79 | + createLogger("[leaving] /contacts", false) // onLeave |
| 80 | +); |
| 81 | +router.on("/about", createLogger("/about")); |
| 82 | +router.on("/about/us", createLogger("/about/us")); |
| 83 | + |
| 84 | +document.body.addEventListener("click", (event) => { |
| 85 | + if (!event.target.matches("a")) { |
| 86 | + return; |
| 87 | + } |
| 88 | + event.preventDefault(); |
| 89 | + let url = event.target.getAttribute("href"); |
| 90 | + router.go(url); |
| 91 | + unsubscribe(); |
| 92 | +}); |
0 commit comments