@tanstack/history
Advanced tools
Comparing version 0.0.1-beta.267 to 0.0.1-beta.268
@@ -38,6 +38,7 @@ /** | ||
}; | ||
const tryNavigation = task => { | ||
const tryNavigation = async task => { | ||
if (typeof document !== 'undefined' && blockers.length) { | ||
for (let blocker of blockers) { | ||
if (!window.confirm(blocker.message)) { | ||
const allowed = await blocker(); | ||
if (!allowed) { | ||
opts.onBlocked?.(onUpdate); | ||
@@ -88,7 +89,4 @@ return; | ||
createHref: str => opts.createHref(str), | ||
block: message => { | ||
const payload = { | ||
message | ||
}; | ||
blockers.push(payload); | ||
block: blocker => { | ||
blockers.push(blocker); | ||
if (blockers.length === 1) { | ||
@@ -100,3 +98,3 @@ addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
return () => { | ||
blockers = blockers.filter(b => b !== payload); | ||
blockers = blockers.filter(b => b !== blocker); | ||
if (!blockers.length) { | ||
@@ -103,0 +101,0 @@ stopBlocking(); |
@@ -36,6 +36,7 @@ /** | ||
}; | ||
const tryNavigation = task => { | ||
const tryNavigation = async task => { | ||
if (typeof document !== 'undefined' && blockers.length) { | ||
for (let blocker of blockers) { | ||
if (!window.confirm(blocker.message)) { | ||
const allowed = await blocker(); | ||
if (!allowed) { | ||
opts.onBlocked?.(onUpdate); | ||
@@ -86,7 +87,4 @@ return; | ||
createHref: str => opts.createHref(str), | ||
block: message => { | ||
const payload = { | ||
message | ||
}; | ||
blockers.push(payload); | ||
block: blocker => { | ||
blockers.push(blocker); | ||
if (blockers.length === 1) { | ||
@@ -98,3 +96,3 @@ addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
return () => { | ||
blockers = blockers.filter(b => b !== payload); | ||
blockers = blockers.filter(b => b !== blocker); | ||
if (!blockers.length) { | ||
@@ -101,0 +99,0 @@ stopBlocking(); |
@@ -11,3 +11,3 @@ { | ||
"name": "packages/history/src/index.ts", | ||
"uid": "aaed-3" | ||
"uid": "f7a8-3" | ||
} | ||
@@ -20,14 +20,14 @@ ] | ||
"nodeParts": { | ||
"aaed-3": { | ||
"renderedLength": 9761, | ||
"gzipLength": 2836, | ||
"f7a8-3": { | ||
"renderedLength": 9731, | ||
"gzipLength": 2821, | ||
"brotliLength": 0, | ||
"metaUid": "aaed-2" | ||
"metaUid": "f7a8-2" | ||
} | ||
}, | ||
"nodeMetas": { | ||
"aaed-2": { | ||
"f7a8-2": { | ||
"id": "/packages/history/src/index.ts", | ||
"moduleParts": { | ||
"index.production.js": "aaed-3" | ||
"index.production.js": "f7a8-3" | ||
}, | ||
@@ -34,0 +34,0 @@ "imported": [], |
@@ -20,3 +20,3 @@ /** | ||
createHref: (href: string) => string; | ||
block: (message: string) => () => void; | ||
block: (blocker: BlockerFn) => () => void; | ||
flush: () => void; | ||
@@ -38,2 +38,4 @@ destroy: () => void; | ||
} | ||
type ShouldAllowNavigation = any; | ||
type BlockerFn = () => Promise<ShouldAllowNavigation> | ShouldAllowNavigation; | ||
declare function createHistory(opts: { | ||
@@ -80,2 +82,2 @@ getLocation: () => HistoryLocation; | ||
export { type HistoryLocation, type HistoryState, type ParsedPath, type RouterHistory, createBrowserHistory, createHashHistory, createHistory, createMemoryHistory }; | ||
export { type BlockerFn, type HistoryLocation, type HistoryState, type ParsedPath, type RouterHistory, createBrowserHistory, createHashHistory, createHistory, createMemoryHistory }; |
@@ -42,6 +42,7 @@ /** | ||
}; | ||
const tryNavigation = task => { | ||
const tryNavigation = async task => { | ||
if (typeof document !== 'undefined' && blockers.length) { | ||
for (let blocker of blockers) { | ||
if (!window.confirm(blocker.message)) { | ||
const allowed = await blocker(); | ||
if (!allowed) { | ||
opts.onBlocked?.(onUpdate); | ||
@@ -92,7 +93,4 @@ return; | ||
createHref: str => opts.createHref(str), | ||
block: message => { | ||
const payload = { | ||
message | ||
}; | ||
blockers.push(payload); | ||
block: blocker => { | ||
blockers.push(blocker); | ||
if (blockers.length === 1) { | ||
@@ -104,3 +102,3 @@ addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
return () => { | ||
blockers = blockers.filter(b => b !== payload); | ||
blockers = blockers.filter(b => b !== blocker); | ||
if (!blockers.length) { | ||
@@ -107,0 +105,0 @@ stopBlocking(); |
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TanStackHistory={})}(this,(function(e){"use strict";const t="pushstate",o="popstate",n="beforeunload",r=e=>(e.preventDefault(),e.returnValue=""),i=()=>{removeEventListener(n,r,{capture:!0})};function a(e){let t=e.getLocation(),o=new Set,a=[];const c=()=>{t=e.getLocation(),o.forEach((e=>e()))},h=t=>{if("undefined"!=typeof document&&a.length)for(let t of a)if(!window.confirm(t.message))return void e.onBlocked?.(c);t()};return{get location(){return t},subscribe:e=>(o.add(e),()=>{o.delete(e)}),push:(t,o)=>{o=s(o),h((()=>{e.pushState(t,o,c)}))},replace:(t,o)=>{o=s(o),h((()=>{e.replaceState(t,o,c)}))},go:t=>{h((()=>{e.go(t)}))},back:()=>{h((()=>{e.back()}))},forward:()=>{h((()=>{e.forward()}))},createHref:t=>e.createHref(t),block:e=>{const t={message:e};return a.push(t),1===a.length&&addEventListener(n,r,{capture:!0}),()=>{a=a.filter((e=>e!==t)),a.length||i()}},flush:()=>e.flush?.(),destroy:()=>e.destroy?.(),notify:c}}function s(e){return e||(e={}),{...e,key:u()}}function c(e){const n=e?.window??("undefined"!=typeof document?window:void 0),r=e?.createHref??(e=>e),i=e?.parseLocation??(()=>h(`${n.location.pathname}${n.location.search}${n.location.hash}`,n.history.state));let s,c=i();let u,f,l=!0;const d=()=>{l=!1,(()=>{u&&(n.history[u.isPush?"pushState":"replaceState"](u.state,"",u.href),u=void 0,f=void 0,s=void 0)})(),l=!0},p=(e,t,o,n)=>{const i=r(t);f||(s=c),c=h(t,o),u={href:i,state:o,isPush:u?.isPush||"push"===e},n(),f||(f=Promise.resolve().then((()=>d())))},y=()=>{c=i(),v.notify()};var g=n.history.pushState,m=n.history.replaceState;const v=a({getLocation:()=>c,pushState:(e,t,o)=>p("push",e,t,o),replaceState:(e,t,o)=>p("replace",e,t,o),back:()=>n.history.back(),forward:()=>n.history.forward(),go:e=>n.history.go(e),createHref:e=>r(e),flush:d,destroy:()=>{n.history.pushState=g,n.history.replaceState=m,n.removeEventListener(t,y),n.removeEventListener(o,y)},onBlocked:e=>{s&&c!==s&&(c=s,e())}});return n.addEventListener(t,y),n.addEventListener(o,y),n.history.pushState=function(){let e=g.apply(n.history,arguments);return l&&v.notify(),e},n.history.replaceState=function(){let e=m.apply(n.history,arguments);return l&&v.notify(),e},v}function h(e,t){let o=e.indexOf("#"),n=e.indexOf("?");return{href:e,pathname:e.substring(0,o>0?n>0?Math.min(o,n):o:n>0?n:e.length),hash:o>-1?e.substring(o):"",search:n>-1?e.slice(n,-1===o?void 0:o):"",state:t||{}}}function u(){return(Math.random()+1).toString(36).substring(7)}e.createBrowserHistory=c,e.createHashHistory=function(e){const t=e?.window??("undefined"!=typeof document?window:void 0);return c({window:t,parseLocation:()=>h(t.location.hash.split("#").slice(1).join("#")??"/",t.history.state),createHref:e=>`${t.location.pathname}${t.location.search}#${e}`})},e.createHistory=a,e.createMemoryHistory=function(e={initialEntries:["/"]}){const t=e.initialEntries;let o=e.initialIndex??t.length-1,n={key:u()};return a({getLocation:()=>h(t[o],n),pushState:(e,r)=>{n=r,t.push(e),o++},replaceState:(e,r)=>{n=r,t[o]=e},back:()=>{o--},forward:()=>{o=Math.min(o+1,t.length-1)},go:e=>{o=Math.min(Math.max(o+e,0),t.length-1)},createHref:e=>e})}})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TanStackHistory={})}(this,(function(t){"use strict";const e="pushstate",o="popstate",n="beforeunload",r=t=>(t.preventDefault(),t.returnValue=""),i=()=>{removeEventListener(n,r,{capture:!0})};function a(t){let e=t.getLocation(),o=new Set,a=[];const c=()=>{e=t.getLocation(),o.forEach((t=>t()))},h=async e=>{if("undefined"!=typeof document&&a.length)for(let e of a){if(!await e())return void t.onBlocked?.(c)}e()};return{get location(){return e},subscribe:t=>(o.add(t),()=>{o.delete(t)}),push:(e,o)=>{o=s(o),h((()=>{t.pushState(e,o,c)}))},replace:(e,o)=>{o=s(o),h((()=>{t.replaceState(e,o,c)}))},go:e=>{h((()=>{t.go(e)}))},back:()=>{h((()=>{t.back()}))},forward:()=>{h((()=>{t.forward()}))},createHref:e=>t.createHref(e),block:t=>(a.push(t),1===a.length&&addEventListener(n,r,{capture:!0}),()=>{a=a.filter((e=>e!==t)),a.length||i()}),flush:()=>t.flush?.(),destroy:()=>t.destroy?.(),notify:c}}function s(t){return t||(t={}),{...t,key:u()}}function c(t){const n=t?.window??("undefined"!=typeof document?window:void 0),r=t?.createHref??(t=>t),i=t?.parseLocation??(()=>h(`${n.location.pathname}${n.location.search}${n.location.hash}`,n.history.state));let s,c=i();let u,f,l=!0;const d=()=>{l=!1,(()=>{u&&(n.history[u.isPush?"pushState":"replaceState"](u.state,"",u.href),u=void 0,f=void 0,s=void 0)})(),l=!0},p=(t,e,o,n)=>{const i=r(e);f||(s=c),c=h(e,o),u={href:i,state:o,isPush:u?.isPush||"push"===t},n(),f||(f=Promise.resolve().then((()=>d())))},y=()=>{c=i(),m.notify()};var g=n.history.pushState,v=n.history.replaceState;const m=a({getLocation:()=>c,pushState:(t,e,o)=>p("push",t,e,o),replaceState:(t,e,o)=>p("replace",t,e,o),back:()=>n.history.back(),forward:()=>n.history.forward(),go:t=>n.history.go(t),createHref:t=>r(t),flush:d,destroy:()=>{n.history.pushState=g,n.history.replaceState=v,n.removeEventListener(e,y),n.removeEventListener(o,y)},onBlocked:t=>{s&&c!==s&&(c=s,t())}});return n.addEventListener(e,y),n.addEventListener(o,y),n.history.pushState=function(){let t=g.apply(n.history,arguments);return l&&m.notify(),t},n.history.replaceState=function(){let t=v.apply(n.history,arguments);return l&&m.notify(),t},m}function h(t,e){let o=t.indexOf("#"),n=t.indexOf("?");return{href:t,pathname:t.substring(0,o>0?n>0?Math.min(o,n):o:n>0?n:t.length),hash:o>-1?t.substring(o):"",search:n>-1?t.slice(n,-1===o?void 0:o):"",state:e||{}}}function u(){return(Math.random()+1).toString(36).substring(7)}t.createBrowserHistory=c,t.createHashHistory=function(t){const e=t?.window??("undefined"!=typeof document?window:void 0);return c({window:e,parseLocation:()=>h(e.location.hash.split("#").slice(1).join("#")??"/",e.history.state),createHref:t=>`${e.location.pathname}${e.location.search}#${t}`})},t.createHistory=a,t.createMemoryHistory=function(t={initialEntries:["/"]}){const e=t.initialEntries;let o=t.initialIndex??e.length-1,n={key:u()};return a({getLocation:()=>h(e[o],n),pushState:(t,r)=>{n=r,e.push(t),o++},replaceState:(t,r)=>{n=r,e[o]=t},back:()=>{o--},forward:()=>{o=Math.min(o+1,e.length-1)},go:t=>{o=Math.min(Math.max(o+t,0),e.length-1)},createHref:t=>t})}})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/history", | ||
"author": "Tanner Linsley", | ||
"version": "0.0.1-beta.267", | ||
"version": "0.0.1-beta.268", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "tanstack/history", |
@@ -14,3 +14,3 @@ // While the public API was clearly inspired by the "history" npm package, | ||
createHref: (href: string) => string | ||
block: (message: string) => () => void | ||
block: (blocker: BlockerFn) => () => void | ||
flush: () => void | ||
@@ -36,6 +36,8 @@ destroy: () => void | ||
type Blocker = { | ||
message: string | ||
} | ||
type ShouldAllowNavigation = any | ||
export type BlockerFn = () => | ||
| Promise<ShouldAllowNavigation> | ||
| ShouldAllowNavigation | ||
const pushStateEvent = 'pushstate' | ||
@@ -71,3 +73,3 @@ const popStateEvent = 'popstate' | ||
let subscribers = new Set<() => void>() | ||
let blockers: Blocker[] = [] | ||
let blockers: BlockerFn[] = [] | ||
@@ -79,6 +81,7 @@ const onUpdate = () => { | ||
const tryNavigation = (task: () => void) => { | ||
const tryNavigation = async (task: () => void) => { | ||
if (typeof document !== 'undefined' && blockers.length) { | ||
for (let blocker of blockers) { | ||
if (!window.confirm(blocker.message)) { | ||
const allowed = await blocker() | ||
if (!allowed) { | ||
opts.onBlocked?.(onUpdate) | ||
@@ -132,9 +135,5 @@ return | ||
createHref: (str) => opts.createHref(str), | ||
block: (message) => { | ||
const payload: Blocker = { | ||
message, | ||
} | ||
block: (blocker) => { | ||
blockers.push(blocker) | ||
blockers.push(payload) | ||
if (blockers.length === 1) { | ||
@@ -147,3 +146,3 @@ addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
return () => { | ||
blockers = blockers.filter((b) => b !== payload) | ||
blockers = blockers.filter((b) => b !== blocker) | ||
@@ -150,0 +149,0 @@ if (!blockers.length) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
290727
1397