@tanstack/history 1.85.3 → 1.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,43 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const pushStateEvent = "pushstate";
4
3
  const popStateEvent = "popstate";
5
4
  const beforeUnloadEvent = "beforeunload";
6
- const beforeUnloadListener = (event) => {
7
- event.preventDefault();
8
- return event.returnValue = "";
9
- };
10
- const stopBlocking = () => {
11
- removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
12
- capture: true
13
- });
14
- };
15
5
  function createHistory(opts) {
16
6
  let location = opts.getLocation();
17
7
  const subscribers = /* @__PURE__ */ new Set();
18
- let blockers = [];
19
- const notify = () => {
8
+ const notify = (action) => {
20
9
  location = opts.getLocation();
21
- subscribers.forEach((subscriber) => subscriber({ location }));
10
+ subscribers.forEach((subscriber) => subscriber({ location, action }));
11
+ };
12
+ const _notifyRollback = () => {
13
+ location = opts.getLocation();
14
+ subscribers.forEach(
15
+ (subscriber) => subscriber({ location, action: { type: "ROLLBACK" } })
16
+ );
22
17
  };
23
- const tryNavigation = async (task, navigateOpts) => {
24
- var _a;
18
+ const tryNavigation = async ({
19
+ task,
20
+ navigateOpts,
21
+ ...actionInfo
22
+ }) => {
23
+ var _a, _b;
25
24
  const ignoreBlocker = (navigateOpts == null ? void 0 : navigateOpts.ignoreBlocker) ?? false;
26
- if (!ignoreBlocker && typeof document !== "undefined" && blockers.length) {
25
+ if (ignoreBlocker) {
26
+ task();
27
+ return;
28
+ }
29
+ const blockers = ((_a = opts.getBlockers) == null ? void 0 : _a.call(opts)) ?? [];
30
+ const isPushOrReplace = actionInfo.type === "PUSH" || actionInfo.type === "REPLACE";
31
+ if (typeof document !== "undefined" && blockers.length && isPushOrReplace) {
27
32
  for (const blocker of blockers) {
28
- const allowed = await blocker();
29
- if (!allowed) {
30
- (_a = opts.onBlocked) == null ? void 0 : _a.call(opts, notify);
33
+ const nextLocation = parseHref(actionInfo.path, actionInfo.state);
34
+ const isBlocked = await blocker.blockerFn({
35
+ currentLocation: location,
36
+ nextLocation,
37
+ action: actionInfo.type
38
+ });
39
+ if (isBlocked) {
40
+ (_b = opts.onBlocked) == null ? void 0 : _b.call(opts, _notifyRollback);
31
41
  return;
32
42
  }
33
43
  }
@@ -50,49 +60,71 @@ function createHistory(opts) {
50
60
  },
51
61
  push: (path, state, navigateOpts) => {
52
62
  state = assignKey(state);
53
- tryNavigation(() => {
54
- opts.pushState(path, state);
55
- notify();
56
- }, navigateOpts);
63
+ tryNavigation({
64
+ task: () => {
65
+ opts.pushState(path, state);
66
+ notify({ type: "PUSH" });
67
+ },
68
+ navigateOpts,
69
+ type: "PUSH",
70
+ path,
71
+ state
72
+ });
57
73
  },
58
74
  replace: (path, state, navigateOpts) => {
59
75
  state = assignKey(state);
60
- tryNavigation(() => {
61
- opts.replaceState(path, state);
62
- notify();
63
- }, navigateOpts);
76
+ tryNavigation({
77
+ task: () => {
78
+ opts.replaceState(path, state);
79
+ notify({ type: "REPLACE" });
80
+ },
81
+ navigateOpts,
82
+ type: "REPLACE",
83
+ path,
84
+ state
85
+ });
64
86
  },
65
87
  go: (index, navigateOpts) => {
66
- tryNavigation(() => {
67
- opts.go(index);
68
- notify();
69
- }, navigateOpts);
88
+ tryNavigation({
89
+ task: () => {
90
+ opts.go(index);
91
+ notify({ type: "GO", index });
92
+ },
93
+ navigateOpts,
94
+ type: "GO"
95
+ });
70
96
  },
71
97
  back: (navigateOpts) => {
72
- tryNavigation(() => {
73
- opts.back();
74
- notify();
75
- }, navigateOpts);
98
+ tryNavigation({
99
+ task: () => {
100
+ opts.back((navigateOpts == null ? void 0 : navigateOpts.ignoreBlocker) ?? false);
101
+ notify({ type: "BACK" });
102
+ },
103
+ navigateOpts,
104
+ type: "BACK"
105
+ });
76
106
  },
77
107
  forward: (navigateOpts) => {
78
- tryNavigation(() => {
79
- opts.forward();
80
- notify();
81
- }, navigateOpts);
108
+ tryNavigation({
109
+ task: () => {
110
+ opts.forward((navigateOpts == null ? void 0 : navigateOpts.ignoreBlocker) ?? false);
111
+ notify({ type: "FORWARD" });
112
+ },
113
+ navigateOpts,
114
+ type: "FORWARD"
115
+ });
82
116
  },
83
117
  createHref: (str) => opts.createHref(str),
84
118
  block: (blocker) => {
85
- blockers.push(blocker);
86
- if (blockers.length === 1) {
87
- addEventListener(beforeUnloadEvent, beforeUnloadListener, {
88
- capture: true
89
- });
90
- }
119
+ var _a;
120
+ if (!opts.setBlockers) return () => {
121
+ };
122
+ const blockers = ((_a = opts.getBlockers) == null ? void 0 : _a.call(opts)) ?? [];
123
+ opts.setBlockers([...blockers, blocker]);
91
124
  return () => {
92
- blockers = blockers.filter((b) => b !== blocker);
93
- if (!blockers.length) {
94
- stopBlocking();
95
- }
125
+ var _a2, _b;
126
+ const blockers2 = ((_a2 = opts.getBlockers) == null ? void 0 : _a2.call(opts)) ?? [];
127
+ (_b = opts.setBlockers) == null ? void 0 : _b.call(opts, blockers2.filter((b) => b !== blocker));
96
128
  };
97
129
  },
98
130
  flush: () => {
@@ -119,6 +151,9 @@ function createBrowserHistory(opts) {
119
151
  const win = (opts == null ? void 0 : opts.window) ?? (typeof document !== "undefined" ? window : void 0);
120
152
  const originalPushState = win.history.pushState;
121
153
  const originalReplaceState = win.history.replaceState;
154
+ let blockers = [];
155
+ const _getBlockers = () => blockers;
156
+ const _setBlockers = (newBlockers) => blockers = newBlockers;
122
157
  const createHref = (opts == null ? void 0 : opts.createHref) ?? ((path) => path);
123
158
  const parseLocation = (opts == null ? void 0 : opts.parseLocation) ?? (() => parseHref(
124
159
  `${win.location.pathname}${win.location.search}${win.location.hash}`,
@@ -126,6 +161,9 @@ function createBrowserHistory(opts) {
126
161
  ));
127
162
  let currentLocation = parseLocation();
128
163
  let rollbackLocation;
164
+ let ignoreNextPop = false;
165
+ let skipBlockerNextPop = false;
166
+ let ignoreNextBeforeUnload = false;
129
167
  const getLocation = () => currentLocation;
130
168
  let next;
131
169
  let scheduled;
@@ -161,33 +199,100 @@ function createBrowserHistory(opts) {
161
199
  };
162
200
  const onPushPop = () => {
163
201
  currentLocation = parseLocation();
164
- history.notify();
202
+ history.notify({ type: "POP" });
203
+ };
204
+ const onPushPopEvent = async () => {
205
+ if (ignoreNextPop) {
206
+ ignoreNextPop = false;
207
+ return;
208
+ }
209
+ if (skipBlockerNextPop) {
210
+ skipBlockerNextPop = false;
211
+ } else {
212
+ const blockers2 = _getBlockers();
213
+ if (typeof document !== "undefined" && blockers2.length) {
214
+ for (const blocker of blockers2) {
215
+ const nextLocation = parseLocation();
216
+ const isBlocked = await blocker.blockerFn({
217
+ currentLocation,
218
+ nextLocation,
219
+ action: "POP"
220
+ });
221
+ if (isBlocked) {
222
+ ignoreNextPop = true;
223
+ win.history.go(1);
224
+ history.notify({ type: "POP" });
225
+ return;
226
+ }
227
+ }
228
+ }
229
+ }
230
+ currentLocation = parseLocation();
231
+ history.notify({ type: "POP" });
232
+ };
233
+ const onBeforeUnload = (e) => {
234
+ if (ignoreNextBeforeUnload) {
235
+ ignoreNextBeforeUnload = false;
236
+ return;
237
+ }
238
+ let shouldBlock = false;
239
+ const blockers2 = _getBlockers();
240
+ if (typeof document !== "undefined" && blockers2.length) {
241
+ for (const blocker of blockers2) {
242
+ const shouldHaveBeforeUnload = blocker.enableBeforeUnload ?? true;
243
+ if (shouldHaveBeforeUnload === true) {
244
+ shouldBlock = true;
245
+ break;
246
+ }
247
+ if (typeof shouldHaveBeforeUnload === "function" && shouldHaveBeforeUnload() === true) {
248
+ shouldBlock = true;
249
+ break;
250
+ }
251
+ }
252
+ }
253
+ if (shouldBlock) {
254
+ e.preventDefault();
255
+ return e.returnValue = "";
256
+ }
257
+ return;
165
258
  };
166
259
  const history = createHistory({
167
260
  getLocation,
168
261
  getLength: () => win.history.length,
169
262
  pushState: (href, state) => queueHistoryAction("push", href, state),
170
263
  replaceState: (href, state) => queueHistoryAction("replace", href, state),
171
- back: () => win.history.back(),
172
- forward: () => win.history.forward(),
264
+ back: (ignoreBlocker) => {
265
+ if (ignoreBlocker) skipBlockerNextPop = true;
266
+ ignoreNextBeforeUnload = true;
267
+ return win.history.back();
268
+ },
269
+ forward: (ignoreBlocker) => {
270
+ if (ignoreBlocker) skipBlockerNextPop = true;
271
+ ignoreNextBeforeUnload = true;
272
+ win.history.forward();
273
+ },
173
274
  go: (n) => win.history.go(n),
174
275
  createHref: (href) => createHref(href),
175
276
  flush,
176
277
  destroy: () => {
177
278
  win.history.pushState = originalPushState;
178
279
  win.history.replaceState = originalReplaceState;
179
- win.removeEventListener(pushStateEvent, onPushPop);
180
- win.removeEventListener(popStateEvent, onPushPop);
280
+ win.removeEventListener(beforeUnloadEvent, onBeforeUnload, {
281
+ capture: true
282
+ });
283
+ win.removeEventListener(popStateEvent, onPushPopEvent);
181
284
  },
182
285
  onBlocked: (onUpdate) => {
183
286
  if (rollbackLocation && currentLocation !== rollbackLocation) {
184
287
  currentLocation = rollbackLocation;
185
288
  onUpdate();
186
289
  }
187
- }
290
+ },
291
+ getBlockers: _getBlockers,
292
+ setBlockers: _setBlockers
188
293
  });
189
- win.addEventListener(pushStateEvent, onPushPop);
190
- win.addEventListener(popStateEvent, onPushPop);
294
+ win.addEventListener(beforeUnloadEvent, onBeforeUnload, { capture: true });
295
+ win.addEventListener(popStateEvent, onPushPopEvent);
191
296
  win.history.pushState = function(...args) {
192
297
  const res = originalPushState.apply(win.history, args);
193
298
  if (!history._ignoreSubscribers) onPushPop();
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface NavigateOptions {\n ignoreBlocker?: boolean\n}\nexport interface RouterHistory {\n location: HistoryLocation\n length: number\n subscribers: Set<(opts: { location: HistoryLocation }) => void>\n subscribe: (cb: (opts: { location: HistoryLocation }) => void) => () => void\n push: (path: string, state?: any, navigateOpts?: NavigateOptions) => void\n replace: (path: string, state?: any, navigateOpts?: NavigateOptions) => void\n go: (index: number, navigateOpts?: NavigateOptions) => void\n back: (navigateOpts?: NavigateOptions) => void\n forward: (navigateOpts?: NavigateOptions) => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n _ignoreSubscribers?: boolean\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key?: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-expect-error\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n getLength: () => number\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n const subscribers = new Set<(opts: { location: HistoryLocation }) => void>()\n let blockers: Array<BlockerFn> = []\n\n const notify = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber({ location }))\n }\n\n const tryNavigation = async (\n task: () => void,\n navigateOpts?: NavigateOptions,\n ) => {\n const ignoreBlocker = navigateOpts?.ignoreBlocker ?? false\n if (!ignoreBlocker && typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(notify)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n get length() {\n return opts.getLength()\n },\n subscribers,\n subscribe: (cb: (opts: { location: HistoryLocation }) => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path, state, navigateOpts) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n notify()\n }, navigateOpts)\n },\n replace: (path, state, navigateOpts) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n notify()\n }, navigateOpts)\n },\n go: (index, navigateOpts) => {\n tryNavigation(() => {\n opts.go(index)\n notify()\n }, navigateOpts)\n },\n back: (navigateOpts) => {\n tryNavigation(() => {\n opts.back()\n notify()\n }, navigateOpts)\n },\n forward: (navigateOpts) => {\n tryNavigation(() => {\n opts.forward()\n notify()\n }, navigateOpts)\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify,\n }\n}\n\nfunction assignKey(state: HistoryState | undefined) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const originalPushState = win.history.pushState\n const originalReplaceState = win.history.replaceState\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function flushes the next update to the browser history\n const flush = () => {\n if (!next) {\n return\n }\n\n // We need to ignore any updates to the subscribers while we update the browser history\n history._ignoreSubscribers = true\n\n // Update the browser history\n ;(next.isPush ? win.history.pushState : win.history.replaceState)(\n next.state,\n '',\n next.href,\n )\n\n // Stop ignoring subscriber updates\n history._ignoreSubscribers = false\n\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n const history = createHistory({\n getLocation,\n getLength: () => win.history.length,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function (...args: Array<any>) {\n const res = originalPushState.apply(win.history, args)\n if (!history._ignoreSubscribers) onPushPop()\n return res\n }\n\n win.history.replaceState = function (...args: Array<any>) {\n const res = originalReplaceState.apply(win.history, args)\n if (!history._ignoreSubscribers) onPushPop()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: Array<string>\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n const states = entries.map(() => ({}) as HistoryState)\n\n const getLocation = () => parseHref(entries[index]!, states[index])\n\n return createHistory({\n getLocation,\n getLength: () => entries.length,\n pushState: (path, state) => {\n // Removes all subsequent entries after the current index to start a new branch\n if (index < entries.length - 1) {\n entries.splice(index + 1)\n states.splice(index + 1)\n }\n states.push(state)\n entries.push(path)\n index = Math.max(entries.length - 1, 0)\n },\n replaceState: (path, state) => {\n states[index] = state\n entries[index] = path\n },\n back: () => {\n index = Math.max(index - 1, 0)\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nexport function parseHref(\n href: string,\n state: HistoryState | undefined,\n): HistoryLocation {\n const hashIndex = href.indexOf('#')\n const searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":[],"mappings":";;AA8CA,MAAM,iBAAiB;AACvB,MAAM,gBAAgB;AACtB,MAAM,oBAAoB;AAE1B,MAAM,uBAAuB,CAAC,UAAiB;AAC7C,QAAM,eAAe;AAErB,SAAQ,MAAM,cAAc;AAC9B;AAEA,MAAM,eAAe,MAAM;AACzB,sBAAoB,mBAAmB,sBAAsB;AAAA,IAC3D,SAAS;AAAA,EAAA,CACV;AACH;AAEO,SAAS,cAAc,MAYZ;AACZ,MAAA,WAAW,KAAK,YAAY;AAC1B,QAAA,kCAAkB,IAAmD;AAC3E,MAAI,WAA6B,CAAC;AAElC,QAAM,SAAS,MAAM;AACnB,eAAW,KAAK,YAAY;AAC5B,gBAAY,QAAQ,CAAC,eAAe,WAAW,EAAE,SAAA,CAAU,CAAC;AAAA,EAC9D;AAEM,QAAA,gBAAgB,OACpB,MACA,iBACG;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AAI;AACjB,cAAQ,KAAK,IAAI,QAAQ,SAAS,GAAG,CAAC;AAAA,IACxC;AAAA,IACA,cAAc,CAAC,MAAM,UAAU;AAC7B,aAAO,KAAK,IAAI;AAChB,cAAQ,KAAK,IAAI;AAAA,IACnB;AAAA,IACA,MAAM,MAAM;AACV,cAAQ,KAAK,IAAI,QAAQ,GAAG,CAAC;AAAA,IAC/B;AAAA,IACA,SAAS,MAAM;AACb,cAAQ,KAAK,IAAI,QAAQ,GAAG,QAAQ,SAAS,CAAC;AAAA,IAChD;AAAA,IACA,IAAI,CAAC,MAAM;AACD,cAAA,KAAK,IAAI,KAAK,IAAI,QAAQ,GAAG,CAAC,GAAG,QAAQ,SAAS,CAAC;AAAA,IAC7D;AAAA,IACA,YAAY,CAAC,SAAS;AAAA,EAAA,CACvB;AACH;AAEgB,SAAA,UACd,MACA,OACiB;AACX,QAAA,YAAY,KAAK,QAAQ,GAAG;AAC5B,QAAA,cAAc,KAAK,QAAQ,GAAG;AAE7B,SAAA;AAAA,IACL;AAAA,IACA,UAAU,KAAK;AAAA,MACb;AAAA,MACA,YAAY,IACR,cAAc,IACZ,KAAK,IAAI,WAAW,WAAW,IAC/B,YACF,cAAc,IACZ,cACA,KAAK;AAAA,IACb;AAAA,IACA,MAAM,YAAY,KAAK,KAAK,UAAU,SAAS,IAAI;AAAA,IACnD,QACE,cAAc,KACV,KAAK,MAAM,aAAa,cAAc,KAAK,SAAY,SAAS,IAChE;AAAA,IACN,OAAO,SAAS,CAAA;AAAA,EAClB;AACF;AAGA,SAAS,kBAAkB;AACjB,UAAA,KAAK,WAAW,GAAG,SAAS,EAAE,EAAE,UAAU,CAAC;AACrD;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface NavigateOptions {\n ignoreBlocker?: boolean\n}\n\ntype SubscriberHistoryAction =\n | {\n type: HistoryAction | 'ROLLBACK'\n }\n | {\n type: 'GO'\n index: number\n }\n\ntype SubscriberArgs = {\n location: HistoryLocation\n action: SubscriberHistoryAction\n}\n\nexport interface RouterHistory {\n location: HistoryLocation\n length: number\n subscribers: Set<(opts: SubscriberArgs) => void>\n subscribe: (cb: (opts: SubscriberArgs) => void) => () => void\n push: (path: string, state?: any, navigateOpts?: NavigateOptions) => void\n replace: (path: string, state?: any, navigateOpts?: NavigateOptions) => void\n go: (index: number, navigateOpts?: NavigateOptions) => void\n back: (navigateOpts?: NavigateOptions) => void\n forward: (navigateOpts?: NavigateOptions) => void\n createHref: (href: string) => string\n block: (blocker: NavigationBlocker) => () => void\n flush: () => void\n destroy: () => void\n notify: (action: SubscriberHistoryAction) => void\n _ignoreSubscribers?: boolean\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key?: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type HistoryAction =\n | 'PUSH'\n | 'POP'\n | 'REPLACE'\n | 'FORWARD'\n | 'BACK'\n | 'GO'\n\nexport type BlockerFnArgs = {\n currentLocation: HistoryLocation\n nextLocation: HistoryLocation\n action: HistoryAction\n}\n\nexport type BlockerFn = (\n args: BlockerFnArgs,\n) => Promise<ShouldAllowNavigation> | ShouldAllowNavigation\n\nexport type NavigationBlocker = {\n blockerFn: BlockerFn\n enableBeforeUnload?: (() => boolean) | boolean\n}\n\ntype TryNavigateArgs = {\n task: () => void\n type: 'PUSH' | 'REPLACE' | 'BACK' | 'FORWARD' | 'GO'\n navigateOpts?: NavigateOptions\n} & (\n | {\n type: 'PUSH' | 'REPLACE'\n path: string\n state: any\n }\n | {\n type: 'BACK' | 'FORWARD' | 'GO'\n }\n)\n\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n getLength: () => number\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: (ignoreBlocker: boolean) => void\n forward: (ignoreBlocker: boolean) => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n getBlockers?: () => Array<NavigationBlocker>\n setBlockers?: (blockers: Array<NavigationBlocker>) => void\n}): RouterHistory {\n let location = opts.getLocation()\n const subscribers = new Set<(opts: SubscriberArgs) => void>()\n\n const notify = (action: SubscriberHistoryAction) => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber({ location, action }))\n }\n\n const _notifyRollback = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) =>\n subscriber({ location, action: { type: 'ROLLBACK' } }),\n )\n }\n\n const tryNavigation = async ({\n task,\n navigateOpts,\n ...actionInfo\n }: TryNavigateArgs) => {\n const ignoreBlocker = navigateOpts?.ignoreBlocker ?? false\n if (ignoreBlocker) {\n task()\n return\n }\n\n const blockers = opts.getBlockers?.() ?? []\n const isPushOrReplace =\n actionInfo.type === 'PUSH' || actionInfo.type === 'REPLACE'\n if (typeof document !== 'undefined' && blockers.length && isPushOrReplace) {\n for (const blocker of blockers) {\n const nextLocation = parseHref(actionInfo.path, actionInfo.state)\n const isBlocked = await blocker.blockerFn({\n currentLocation: location,\n nextLocation,\n action: actionInfo.type,\n })\n if (isBlocked) {\n opts.onBlocked?.(_notifyRollback)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n get length() {\n return opts.getLength()\n },\n subscribers,\n subscribe: (cb: (opts: SubscriberArgs) => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path, state, navigateOpts) => {\n state = assignKey(state)\n tryNavigation({\n task: () => {\n opts.pushState(path, state)\n notify({ type: 'PUSH' })\n },\n navigateOpts,\n type: 'PUSH',\n path,\n state,\n })\n },\n replace: (path, state, navigateOpts) => {\n state = assignKey(state)\n tryNavigation({\n task: () => {\n opts.replaceState(path, state)\n notify({ type: 'REPLACE' })\n },\n navigateOpts,\n type: 'REPLACE',\n path,\n state,\n })\n },\n go: (index, navigateOpts) => {\n tryNavigation({\n task: () => {\n opts.go(index)\n notify({ type: 'GO', index })\n },\n navigateOpts,\n type: 'GO',\n })\n },\n back: (navigateOpts) => {\n tryNavigation({\n task: () => {\n opts.back(navigateOpts?.ignoreBlocker ?? false)\n notify({ type: 'BACK' })\n },\n navigateOpts,\n type: 'BACK',\n })\n },\n forward: (navigateOpts) => {\n tryNavigation({\n task: () => {\n opts.forward(navigateOpts?.ignoreBlocker ?? false)\n notify({ type: 'FORWARD' })\n },\n navigateOpts,\n type: 'FORWARD',\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n if (!opts.setBlockers) return () => {}\n const blockers = opts.getBlockers?.() ?? []\n opts.setBlockers([...blockers, blocker])\n\n return () => {\n const blockers = opts.getBlockers?.() ?? []\n opts.setBlockers?.(blockers.filter((b) => b !== blocker))\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify,\n }\n}\n\nfunction assignKey(state: HistoryState | undefined) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const originalPushState = win.history.pushState\n const originalReplaceState = win.history.replaceState\n\n let blockers: Array<NavigationBlocker> = []\n const _getBlockers = () => blockers\n const _setBlockers = (newBlockers: Array<NavigationBlocker>) =>\n (blockers = newBlockers)\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n let ignoreNextPop = false\n let skipBlockerNextPop = false\n let ignoreNextBeforeUnload = false\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function flushes the next update to the browser history\n const flush = () => {\n if (!next) {\n return\n }\n\n // We need to ignore any updates to the subscribers while we update the browser history\n history._ignoreSubscribers = true\n\n // Update the browser history\n ;(next.isPush ? win.history.pushState : win.history.replaceState)(\n next.state,\n '',\n next.href,\n )\n\n // Stop ignoring subscriber updates\n history._ignoreSubscribers = false\n\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify({ type: 'POP' })\n }\n\n const onPushPopEvent = async () => {\n if (ignoreNextPop) {\n ignoreNextPop = false\n return\n }\n\n if (skipBlockerNextPop) {\n skipBlockerNextPop = false\n } else {\n const blockers = _getBlockers()\n if (typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const nextLocation = parseLocation()\n const isBlocked = await blocker.blockerFn({\n currentLocation,\n nextLocation,\n action: 'POP',\n })\n if (isBlocked) {\n ignoreNextPop = true\n win.history.go(1)\n history.notify({ type: 'POP' })\n return\n }\n }\n }\n }\n\n currentLocation = parseLocation()\n history.notify({ type: 'POP' })\n }\n\n const onBeforeUnload = (e: BeforeUnloadEvent) => {\n if (ignoreNextBeforeUnload) {\n ignoreNextBeforeUnload = false\n return\n }\n\n let shouldBlock = false\n\n // If one blocker has a non-disabled beforeUnload, we should block\n const blockers = _getBlockers()\n if (typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const shouldHaveBeforeUnload = blocker.enableBeforeUnload ?? true\n if (shouldHaveBeforeUnload === true) {\n shouldBlock = true\n break\n }\n\n if (\n typeof shouldHaveBeforeUnload === 'function' &&\n shouldHaveBeforeUnload() === true\n ) {\n shouldBlock = true\n break\n }\n }\n }\n\n if (shouldBlock) {\n e.preventDefault()\n return (e.returnValue = '')\n }\n return\n }\n\n const history = createHistory({\n getLocation,\n getLength: () => win.history.length,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: (ignoreBlocker) => {\n if (ignoreBlocker) skipBlockerNextPop = true\n ignoreNextBeforeUnload = true\n return win.history.back()\n },\n forward: (ignoreBlocker) => {\n if (ignoreBlocker) skipBlockerNextPop = true\n ignoreNextBeforeUnload = true\n win.history.forward()\n },\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(beforeUnloadEvent, onBeforeUnload, {\n capture: true,\n })\n win.removeEventListener(popStateEvent, onPushPopEvent)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n getBlockers: _getBlockers,\n setBlockers: _setBlockers,\n })\n\n win.addEventListener(beforeUnloadEvent, onBeforeUnload, { capture: true })\n win.addEventListener(popStateEvent, onPushPopEvent)\n\n win.history.pushState = function (...args: Array<any>) {\n const res = originalPushState.apply(win.history, args as any)\n if (!history._ignoreSubscribers) onPushPop()\n return res\n }\n\n win.history.replaceState = function (...args: Array<any>) {\n const res = originalReplaceState.apply(win.history, args as any)\n if (!history._ignoreSubscribers) onPushPop()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: Array<string>\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n const states = entries.map(() => ({}) as HistoryState)\n\n const getLocation = () => parseHref(entries[index]!, states[index])\n\n return createHistory({\n getLocation,\n getLength: () => entries.length,\n pushState: (path, state) => {\n // Removes all subsequent entries after the current index to start a new branch\n if (index < entries.length - 1) {\n entries.splice(index + 1)\n states.splice(index + 1)\n }\n states.push(state)\n entries.push(path)\n index = Math.max(entries.length - 1, 0)\n },\n replaceState: (path, state) => {\n states[index] = state\n entries[index] = path\n },\n back: () => {\n index = Math.max(index - 1, 0)\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nexport function parseHref(\n href: string,\n state: HistoryState | undefined,\n): HistoryLocation {\n const hashIndex = href.indexOf('#')\n const searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["blockers","_a"],"mappings":";;AA+FA,MAAM,gBAAgB;AACtB,MAAM,oBAAoB;AAEnB,SAAS,cAAc,MAcZ;AACZ,MAAA,WAAW,KAAK,YAAY;AAC1B,QAAA,kCAAkB,IAAoC;AAEtD,QAAA,SAAS,CAAC,WAAoC;AAClD,eAAW,KAAK,YAAY;AAChB,gBAAA,QAAQ,CAAC,eAAe,WAAW,EAAE,UAAU,OAAA,CAAQ,CAAC;AAAA,EACtE;AAEA,QAAM,kBAAkB,MAAM;AAC5B,eAAW,KAAK,YAAY;AAChB,gBAAA;AAAA,MAAQ,CAAC,eACnB,WAAW,EAAE,UAAU,QAAQ,EAAE,MAAM,aAAc,CAAA;AAAA,IACvD;AAAA,EACF;AAEA,QAAM,gBAAgB,OAAO;AAAA,IAC3B;AAAA,IACA;AAAA,IACA,GAAG;AAAA,EAAA,MACkB;;AACf,UAAA,iBAAgB,6CAAc,kBAAiB;AACrD,QAAI,eAAe;AACZ,WAAA;AACL;AAAA,IAAA;AAGF,UAAM,aAAW,UAAK,gBAAL,kCAAwB,CAAC;AAC1C,UAAM,kBACJ,WAAW,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AAA,KAAK,IAAI,KAAK,IAAI,QAAQ,GAAG,CAAC,GAAG,QAAQ,SAAS,CAAC;AAAA,IAC7D;AAAA,IACA,YAAY,CAAC,SAAS;AAAA,EAAA,CACvB;AACH;AAEgB,SAAA,UACd,MACA,OACiB;AACX,QAAA,YAAY,KAAK,QAAQ,GAAG;AAC5B,QAAA,cAAc,KAAK,QAAQ,GAAG;AAE7B,SAAA;AAAA,IACL;AAAA,IACA,UAAU,KAAK;AAAA,MACb;AAAA,MACA,YAAY,IACR,cAAc,IACZ,KAAK,IAAI,WAAW,WAAW,IAC/B,YACF,cAAc,IACZ,cACA,KAAK;AAAA,IACb;AAAA,IACA,MAAM,YAAY,KAAK,KAAK,UAAU,SAAS,IAAI;AAAA,IACnD,QACE,cAAc,KACV,KAAK,MAAM,aAAa,cAAc,KAAK,SAAY,SAAS,IAChE;AAAA,IACN,OAAO,SAAS,CAAA;AAAA,EAClB;AACF;AAGA,SAAS,kBAAkB;AACjB,UAAA,KAAK,WAAW,GAAG,SAAS,EAAE,EAAE,UAAU,CAAC;AACrD;;;;;;"}
@@ -1,25 +1,31 @@
1
1
  export interface NavigateOptions {
2
2
  ignoreBlocker?: boolean;
3
3
  }
4
+ type SubscriberHistoryAction = {
5
+ type: HistoryAction | 'ROLLBACK';
6
+ } | {
7
+ type: 'GO';
8
+ index: number;
9
+ };
10
+ type SubscriberArgs = {
11
+ location: HistoryLocation;
12
+ action: SubscriberHistoryAction;
13
+ };
4
14
  export interface RouterHistory {
5
15
  location: HistoryLocation;
6
16
  length: number;
7
- subscribers: Set<(opts: {
8
- location: HistoryLocation;
9
- }) => void>;
10
- subscribe: (cb: (opts: {
11
- location: HistoryLocation;
12
- }) => void) => () => void;
17
+ subscribers: Set<(opts: SubscriberArgs) => void>;
18
+ subscribe: (cb: (opts: SubscriberArgs) => void) => () => void;
13
19
  push: (path: string, state?: any, navigateOpts?: NavigateOptions) => void;
14
20
  replace: (path: string, state?: any, navigateOpts?: NavigateOptions) => void;
15
21
  go: (index: number, navigateOpts?: NavigateOptions) => void;
16
22
  back: (navigateOpts?: NavigateOptions) => void;
17
23
  forward: (navigateOpts?: NavigateOptions) => void;
18
24
  createHref: (href: string) => string;
19
- block: (blocker: BlockerFn) => () => void;
25
+ block: (blocker: NavigationBlocker) => () => void;
20
26
  flush: () => void;
21
27
  destroy: () => void;
22
- notify: () => void;
28
+ notify: (action: SubscriberHistoryAction) => void;
23
29
  _ignoreSubscribers?: boolean;
24
30
  }
25
31
  export interface HistoryLocation extends ParsedPath {
@@ -35,19 +41,31 @@ export interface HistoryState {
35
41
  key?: string;
36
42
  }
37
43
  type ShouldAllowNavigation = any;
38
- export type BlockerFn = () => Promise<ShouldAllowNavigation> | ShouldAllowNavigation;
44
+ export type HistoryAction = 'PUSH' | 'POP' | 'REPLACE' | 'FORWARD' | 'BACK' | 'GO';
45
+ export type BlockerFnArgs = {
46
+ currentLocation: HistoryLocation;
47
+ nextLocation: HistoryLocation;
48
+ action: HistoryAction;
49
+ };
50
+ export type BlockerFn = (args: BlockerFnArgs) => Promise<ShouldAllowNavigation> | ShouldAllowNavigation;
51
+ export type NavigationBlocker = {
52
+ blockerFn: BlockerFn;
53
+ enableBeforeUnload?: (() => boolean) | boolean;
54
+ };
39
55
  export declare function createHistory(opts: {
40
56
  getLocation: () => HistoryLocation;
41
57
  getLength: () => number;
42
58
  pushState: (path: string, state: any) => void;
43
59
  replaceState: (path: string, state: any) => void;
44
60
  go: (n: number) => void;
45
- back: () => void;
46
- forward: () => void;
61
+ back: (ignoreBlocker: boolean) => void;
62
+ forward: (ignoreBlocker: boolean) => void;
47
63
  createHref: (path: string) => string;
48
64
  flush?: () => void;
49
65
  destroy?: () => void;
50
66
  onBlocked?: (onUpdate: () => void) => void;
67
+ getBlockers?: () => Array<NavigationBlocker>;
68
+ setBlockers?: (blockers: Array<NavigationBlocker>) => void;
51
69
  }): RouterHistory;
52
70
  /**
53
71
  * Creates a history object that can be used to interact with the browser's
@@ -1,25 +1,31 @@
1
1
  export interface NavigateOptions {
2
2
  ignoreBlocker?: boolean;
3
3
  }
4
+ type SubscriberHistoryAction = {
5
+ type: HistoryAction | 'ROLLBACK';
6
+ } | {
7
+ type: 'GO';
8
+ index: number;
9
+ };
10
+ type SubscriberArgs = {
11
+ location: HistoryLocation;
12
+ action: SubscriberHistoryAction;
13
+ };
4
14
  export interface RouterHistory {
5
15
  location: HistoryLocation;
6
16
  length: number;
7
- subscribers: Set<(opts: {
8
- location: HistoryLocation;
9
- }) => void>;
10
- subscribe: (cb: (opts: {
11
- location: HistoryLocation;
12
- }) => void) => () => void;
17
+ subscribers: Set<(opts: SubscriberArgs) => void>;
18
+ subscribe: (cb: (opts: SubscriberArgs) => void) => () => void;
13
19
  push: (path: string, state?: any, navigateOpts?: NavigateOptions) => void;
14
20
  replace: (path: string, state?: any, navigateOpts?: NavigateOptions) => void;
15
21
  go: (index: number, navigateOpts?: NavigateOptions) => void;
16
22
  back: (navigateOpts?: NavigateOptions) => void;
17
23
  forward: (navigateOpts?: NavigateOptions) => void;
18
24
  createHref: (href: string) => string;
19
- block: (blocker: BlockerFn) => () => void;
25
+ block: (blocker: NavigationBlocker) => () => void;
20
26
  flush: () => void;
21
27
  destroy: () => void;
22
- notify: () => void;
28
+ notify: (action: SubscriberHistoryAction) => void;
23
29
  _ignoreSubscribers?: boolean;
24
30
  }
25
31
  export interface HistoryLocation extends ParsedPath {
@@ -35,19 +41,31 @@ export interface HistoryState {
35
41
  key?: string;
36
42
  }
37
43
  type ShouldAllowNavigation = any;
38
- export type BlockerFn = () => Promise<ShouldAllowNavigation> | ShouldAllowNavigation;
44
+ export type HistoryAction = 'PUSH' | 'POP' | 'REPLACE' | 'FORWARD' | 'BACK' | 'GO';
45
+ export type BlockerFnArgs = {
46
+ currentLocation: HistoryLocation;
47
+ nextLocation: HistoryLocation;
48
+ action: HistoryAction;
49
+ };
50
+ export type BlockerFn = (args: BlockerFnArgs) => Promise<ShouldAllowNavigation> | ShouldAllowNavigation;
51
+ export type NavigationBlocker = {
52
+ blockerFn: BlockerFn;
53
+ enableBeforeUnload?: (() => boolean) | boolean;
54
+ };
39
55
  export declare function createHistory(opts: {
40
56
  getLocation: () => HistoryLocation;
41
57
  getLength: () => number;
42
58
  pushState: (path: string, state: any) => void;
43
59
  replaceState: (path: string, state: any) => void;
44
60
  go: (n: number) => void;
45
- back: () => void;
46
- forward: () => void;
61
+ back: (ignoreBlocker: boolean) => void;
62
+ forward: (ignoreBlocker: boolean) => void;
47
63
  createHref: (path: string) => string;
48
64
  flush?: () => void;
49
65
  destroy?: () => void;
50
66
  onBlocked?: (onUpdate: () => void) => void;
67
+ getBlockers?: () => Array<NavigationBlocker>;
68
+ setBlockers?: (blockers: Array<NavigationBlocker>) => void;
51
69
  }): RouterHistory;
52
70
  /**
53
71
  * Creates a history object that can be used to interact with the browser's