UNPKG

@captaincodeman/rdx-model

Version:

A small wrapper for [rdx](https://github.com/CaptainCodeman/rdx), my tiny Redux alternative, which makes bundling a state model small and simple.

3 lines (2 loc) 1.27 kB
import{createModel as t}from"./createModel.js";const e=window.history,r=()=>dispatchEvent(new Event("popstate")),a=(a,o)=>{const s={transform:t=>t,...o};return{model:t({state:{page:"",params:{}},reducers:{change:(t,e)=>e},effects:t=>({back(){e.back(),r()},forward(){e.forward(),r()},go(t){e.go(t),r()},push(t){e.pushState(null,"",t),r()},replace(t){e.replaceState(null,"",t),r()}})}),onStore(t){const o=t.dispatch.routing,c=()=>{const t=a(location.pathname);t&&o.change(s.transform(t))};window.addEventListener("popstate",c),window.addEventListener("click",t=>{const a=n(t);a&&(t.preventDefault(),e.pushState(null,"",a),r())}),queueMicrotask(c)}}},n=t=>{const e=(t=>t.composedPath().find(t=>"A"===t.tagName))(t);return void 0===e||!e.href.startsWith(document.baseURI)||(t=>t.hasAttribute("download"))(e)||(t=>(t.getAttribute("rel")||"").includes("external"))(e)||(t=>t.target)(e)||(t=>t.button&&0!==t.button||t.metaKey||t.altKey||t.ctrlKey||t.shiftKey||t.defaultPrevented)(t)?null:e.href},o=t=>({...t,queries:function(t){const e={};for(const r of t.entries()){const[t,a]=r,n=e[t];n?Array.isArray(n)?n.push(a):e[t]=[n,a]:e[t]=a}return e}(new URLSearchParams(location.search))});export{a as routingPluginFactory,o as withQuerystring}; //# sourceMappingURL=routing.js.map