@tanstack/router-core 0.0.1-beta.194 → 0.0.1-beta.195
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.
- package/build/cjs/fileRoute.js.map +1 -1
- package/build/cjs/history.js +88 -15
- package/build/cjs/history.js.map +1 -1
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/utils.js +2 -0
- package/build/cjs/utils.js.map +1 -1
- package/build/esm/index.js +90 -15
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +126 -126
- package/build/types/history.d.ts +17 -0
- package/build/types/route.d.ts +21 -21
- package/build/types/routeInfo.d.ts +1 -1
- package/build/types/router.d.ts +3 -3
- package/build/umd/index.development.js +90 -15
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/fileRoute.ts +1 -8
- package/src/history.ts +113 -15
- package/src/route.ts +5 -47
- package/src/routeInfo.ts +1 -3
- package/src/router.ts +0 -3
- package/src/utils.ts +1 -0
package/build/cjs/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["export type NoInfer<T> = [T][T extends any ? 0 : never]\nexport type IsAny<T, Y, N = T> = 1 extends 0 & T ? Y : N\nexport type IsAnyBoolean<T> = 1 extends 0 & T ? true : false\nexport type IsKnown<T, Y, N> = unknown extends T ? N : Y\nexport type PickAsRequired<T, K extends keyof T> = Omit<T, K> &\n Required<Pick<T, K>>\nexport type PickAsPartial<T, K extends keyof T> = Omit<T, K> &\n Partial<Pick<T, K>>\nexport type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never\nexport type PickExtra<T, K> = {\n [TKey in keyof K as string extends TKey\n ? never\n : TKey extends keyof T\n ? never\n : TKey]: K[TKey]\n}\n\nexport type PickRequired<T> = {\n [K in keyof T as undefined extends T[K] ? never : K]: T[K]\n}\n\nexport type Expand<T> = T extends object\n ? T extends infer O\n ? { [K in keyof O]: O[K] }\n : never\n : T\n\nexport type UnionToIntersection<U> = (\n U extends any ? (k: U) => void : never\n) extends (k: infer I) => any\n ? I\n : never\n\n// type Compute<T> = { [K in keyof T]: T[K] } | never\n\n// type AllKeys<T> = T extends any ? keyof T : never\n\n// export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<\n// {\n// [K in Keys]: T[Keys]\n// } & {\n// [K in AllKeys<T>]?: T extends any\n// ? K extends keyof T\n// ? T[K]\n// : never\n// : never\n// }\n// >\n\nexport type DeepMerge<A, B> = {\n [K in keyof (A & B)]: K extends keyof B\n ? B[K]\n : K extends keyof A\n ? A[K]\n : never\n} & (A extends Record<string, any>\n ? Pick<A, Exclude<keyof A, keyof B>>\n : never) &\n (B extends Record<string, any> ? Pick<B, Exclude<keyof B, keyof A>> : never)\n\nexport type DeepMergeAll<T extends any[]> = T extends [\n infer Left,\n ...infer Rest,\n]\n ? Rest extends any[]\n ? DeepMerge<Left, DeepMergeAll<Rest>>\n : Left\n : {}\n\nexport type Values<O> = O[ValueKeys<O>]\nexport type ValueKeys<O> = Extract<keyof O, PropertyKey>\n\nexport type DeepAwaited<T> = T extends Promise<infer A>\n ? DeepAwaited<A>\n : T extends Record<infer A, Promise<infer B>>\n ? { [K in A]: DeepAwaited<B> }\n : T\n\nexport type PathParamMask<TRoutePath extends string> =\n TRoutePath extends `${infer L}/$${infer C}/${infer R}`\n ? PathParamMask<`${L}/${string}/${R}`>\n : TRoutePath extends `${infer L}/$${infer C}`\n ? PathParamMask<`${L}/${string}`>\n : TRoutePath\n\nexport type Timeout = ReturnType<typeof setTimeout>\n\nexport type Updater<TPrevious, TResult = TPrevious> =\n | TResult\n | ((prev?: TPrevious) => TResult)\n\nexport type NonNullableUpdater<TPrevious, TResult = TPrevious> =\n | TResult\n | ((prev: TPrevious) => TResult)\n\nexport type PickExtract<T, U> = {\n [K in keyof T as T[K] extends U ? K : never]: T[K]\n}\n\nexport type PickExclude<T, U> = {\n [K in keyof T as T[K] extends U ? never : K]: T[K]\n}\n\nexport function last<T>(arr: T[]) {\n return arr[arr.length - 1]\n}\n\nfunction isFunction(d: any): d is Function {\n return typeof d === 'function'\n}\n\nexport function functionalUpdate<TResult>(\n updater: Updater<TResult> | NonNullableUpdater<TResult>,\n previous: TResult,\n): TResult {\n if (isFunction(updater)) {\n return updater(previous as TResult)\n }\n\n return updater\n}\n\nexport function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {\n return keys.reduce((obj: any, key: K) => {\n obj[key] = parent[key]\n return obj\n }, {} as any)\n}\n\n/**\n * This function returns `a` if `b` is deeply equal.\n * If not, it will replace any deeply equal children of `b` with those of `a`.\n * This can be used for structural sharing between immutable JSON values for example.\n * Do not use this with signals\n */\nexport function replaceEqualDeep<T>(prev: any, _next: T): T {\n if (prev === _next) {\n return prev\n }\n\n const next = _next as any\n\n const array = Array.isArray(prev) && Array.isArray(next)\n\n if (array || (isPlainObject(prev) && isPlainObject(next))) {\n const prevSize = array ? prev.length : Object.keys(prev).length\n const nextItems = array ? next : Object.keys(next)\n const nextSize = nextItems.length\n const copy: any = array ? [] : {}\n\n let equalItems = 0\n\n for (let i = 0; i < nextSize; i++) {\n const key = array ? i : nextItems[i]\n copy[key] = replaceEqualDeep(prev[key], next[key])\n if (copy[key] === prev[key]) {\n equalItems++\n }\n }\n\n return prevSize === nextSize && equalItems === prevSize ? prev : copy\n }\n\n return next\n}\n\n// Copied from: https://github.com/jonschlinkert/is-plain-object\nexport function isPlainObject(o: any) {\n if (!hasObjectPrototype(o)) {\n return false\n }\n\n // If has modified constructor\n const ctor = o.constructor\n if (typeof ctor === 'undefined') {\n return true\n }\n\n // If has modified prototype\n const prot = ctor.prototype\n if (!hasObjectPrototype(prot)) {\n return false\n }\n\n // If constructor does not have an Object-specific method\n if (!prot.hasOwnProperty('isPrototypeOf')) {\n return false\n }\n\n // Most likely a plain Object\n return true\n}\n\nfunction hasObjectPrototype(o: any) {\n return Object.prototype.toString.call(o) === '[object Object]'\n}\n\nexport function partialDeepEqual(a: any, b: any): boolean {\n if (a === b) {\n return true\n }\n\n if (typeof a !== typeof b) {\n return false\n }\n\n if (isPlainObject(a) && isPlainObject(b)) {\n return !Object.keys(b).some((key) => !partialDeepEqual(a[key], b[key]))\n }\n\n if (Array.isArray(a) && Array.isArray(b)) {\n return (\n a.length === b.length &&\n a.every((item, index) => partialDeepEqual(item, b[index]))\n )\n }\n\n return false\n}\n"],"names":["last","arr","length","isFunction","d","functionalUpdate","updater","previous","pick","parent","keys","reduce","obj","key","replaceEqualDeep","prev","_next","next","array","Array","isArray","isPlainObject","prevSize","Object","nextItems","nextSize","copy","equalItems","i","o","hasObjectPrototype","ctor","constructor","prot","prototype","hasOwnProperty","toString","call","partialDeepEqual","a","b","some","every","item","index"],"mappings":";;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["export type NoInfer<T> = [T][T extends any ? 0 : never]\nexport type IsAny<T, Y, N = T> = 1 extends 0 & T ? Y : N\nexport type IsAnyBoolean<T> = 1 extends 0 & T ? true : false\nexport type IsKnown<T, Y, N> = unknown extends T ? N : Y\nexport type PickAsRequired<T, K extends keyof T> = Omit<T, K> &\n Required<Pick<T, K>>\nexport type PickAsPartial<T, K extends keyof T> = Omit<T, K> &\n Partial<Pick<T, K>>\nexport type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never\nexport type PickExtra<T, K> = {\n [TKey in keyof K as string extends TKey\n ? never\n : TKey extends keyof T\n ? never\n : TKey]: K[TKey]\n}\n\nexport type PickRequired<T> = {\n [K in keyof T as undefined extends T[K] ? never : K]: T[K]\n}\n\n// export type Expand<T> = T\nexport type Expand<T> = T extends object\n ? T extends infer O\n ? { [K in keyof O]: O[K] }\n : never\n : T\n\nexport type UnionToIntersection<U> = (\n U extends any ? (k: U) => void : never\n) extends (k: infer I) => any\n ? I\n : never\n\n// type Compute<T> = { [K in keyof T]: T[K] } | never\n\n// type AllKeys<T> = T extends any ? keyof T : never\n\n// export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<\n// {\n// [K in Keys]: T[Keys]\n// } & {\n// [K in AllKeys<T>]?: T extends any\n// ? K extends keyof T\n// ? T[K]\n// : never\n// : never\n// }\n// >\n\nexport type DeepMerge<A, B> = {\n [K in keyof (A & B)]: K extends keyof B\n ? B[K]\n : K extends keyof A\n ? A[K]\n : never\n} & (A extends Record<string, any>\n ? Pick<A, Exclude<keyof A, keyof B>>\n : never) &\n (B extends Record<string, any> ? Pick<B, Exclude<keyof B, keyof A>> : never)\n\nexport type DeepMergeAll<T extends any[]> = T extends [\n infer Left,\n ...infer Rest,\n]\n ? Rest extends any[]\n ? DeepMerge<Left, DeepMergeAll<Rest>>\n : Left\n : {}\n\nexport type Values<O> = O[ValueKeys<O>]\nexport type ValueKeys<O> = Extract<keyof O, PropertyKey>\n\nexport type DeepAwaited<T> = T extends Promise<infer A>\n ? DeepAwaited<A>\n : T extends Record<infer A, Promise<infer B>>\n ? { [K in A]: DeepAwaited<B> }\n : T\n\nexport type PathParamMask<TRoutePath extends string> =\n TRoutePath extends `${infer L}/$${infer C}/${infer R}`\n ? PathParamMask<`${L}/${string}/${R}`>\n : TRoutePath extends `${infer L}/$${infer C}`\n ? PathParamMask<`${L}/${string}`>\n : TRoutePath\n\nexport type Timeout = ReturnType<typeof setTimeout>\n\nexport type Updater<TPrevious, TResult = TPrevious> =\n | TResult\n | ((prev?: TPrevious) => TResult)\n\nexport type NonNullableUpdater<TPrevious, TResult = TPrevious> =\n | TResult\n | ((prev: TPrevious) => TResult)\n\nexport type PickExtract<T, U> = {\n [K in keyof T as T[K] extends U ? K : never]: T[K]\n}\n\nexport type PickExclude<T, U> = {\n [K in keyof T as T[K] extends U ? never : K]: T[K]\n}\n\nexport function last<T>(arr: T[]) {\n return arr[arr.length - 1]\n}\n\nfunction isFunction(d: any): d is Function {\n return typeof d === 'function'\n}\n\nexport function functionalUpdate<TResult>(\n updater: Updater<TResult> | NonNullableUpdater<TResult>,\n previous: TResult,\n): TResult {\n if (isFunction(updater)) {\n return updater(previous as TResult)\n }\n\n return updater\n}\n\nexport function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {\n return keys.reduce((obj: any, key: K) => {\n obj[key] = parent[key]\n return obj\n }, {} as any)\n}\n\n/**\n * This function returns `a` if `b` is deeply equal.\n * If not, it will replace any deeply equal children of `b` with those of `a`.\n * This can be used for structural sharing between immutable JSON values for example.\n * Do not use this with signals\n */\nexport function replaceEqualDeep<T>(prev: any, _next: T): T {\n if (prev === _next) {\n return prev\n }\n\n const next = _next as any\n\n const array = Array.isArray(prev) && Array.isArray(next)\n\n if (array || (isPlainObject(prev) && isPlainObject(next))) {\n const prevSize = array ? prev.length : Object.keys(prev).length\n const nextItems = array ? next : Object.keys(next)\n const nextSize = nextItems.length\n const copy: any = array ? [] : {}\n\n let equalItems = 0\n\n for (let i = 0; i < nextSize; i++) {\n const key = array ? i : nextItems[i]\n copy[key] = replaceEqualDeep(prev[key], next[key])\n if (copy[key] === prev[key]) {\n equalItems++\n }\n }\n\n return prevSize === nextSize && equalItems === prevSize ? prev : copy\n }\n\n return next\n}\n\n// Copied from: https://github.com/jonschlinkert/is-plain-object\nexport function isPlainObject(o: any) {\n if (!hasObjectPrototype(o)) {\n return false\n }\n\n // If has modified constructor\n const ctor = o.constructor\n if (typeof ctor === 'undefined') {\n return true\n }\n\n // If has modified prototype\n const prot = ctor.prototype\n if (!hasObjectPrototype(prot)) {\n return false\n }\n\n // If constructor does not have an Object-specific method\n if (!prot.hasOwnProperty('isPrototypeOf')) {\n return false\n }\n\n // Most likely a plain Object\n return true\n}\n\nfunction hasObjectPrototype(o: any) {\n return Object.prototype.toString.call(o) === '[object Object]'\n}\n\nexport function partialDeepEqual(a: any, b: any): boolean {\n if (a === b) {\n return true\n }\n\n if (typeof a !== typeof b) {\n return false\n }\n\n if (isPlainObject(a) && isPlainObject(b)) {\n return !Object.keys(b).some((key) => !partialDeepEqual(a[key], b[key]))\n }\n\n if (Array.isArray(a) && Array.isArray(b)) {\n return (\n a.length === b.length &&\n a.every((item, index) => partialDeepEqual(item, b[index]))\n )\n }\n\n return false\n}\n"],"names":["last","arr","length","isFunction","d","functionalUpdate","updater","previous","pick","parent","keys","reduce","obj","key","replaceEqualDeep","prev","_next","next","array","Array","isArray","isPlainObject","prevSize","Object","nextItems","nextSize","copy","equalItems","i","o","hasObjectPrototype","ctor","constructor","prot","prototype","hasOwnProperty","toString","call","partialDeepEqual","a","b","some","every","item","index"],"mappings":";;;;;;;;;;;;;;AAqBA;;AAaA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwDO,SAASA,IAAIA,CAAIC,GAAQ,EAAE;AAChC,EAAA,OAAOA,GAAG,CAACA,GAAG,CAACC,MAAM,GAAG,CAAC,CAAC,CAAA;AAC5B,CAAA;AAEA,SAASC,UAAUA,CAACC,CAAM,EAAiB;EACzC,OAAO,OAAOA,CAAC,KAAK,UAAU,CAAA;AAChC,CAAA;AAEO,SAASC,gBAAgBA,CAC9BC,OAAuD,EACvDC,QAAiB,EACR;AACT,EAAA,IAAIJ,UAAU,CAACG,OAAO,CAAC,EAAE;IACvB,OAAOA,OAAO,CAACC,QAAmB,CAAC,CAAA;AACrC,GAAA;AAEA,EAAA,OAAOD,OAAO,CAAA;AAChB,CAAA;AAEO,SAASE,IAAIA,CAAuBC,MAAS,EAAEC,IAAS,EAAc;EAC3E,OAAOA,IAAI,CAACC,MAAM,CAAC,CAACC,GAAQ,EAAEC,GAAM,KAAK;AACvCD,IAAAA,GAAG,CAACC,GAAG,CAAC,GAAGJ,MAAM,CAACI,GAAG,CAAC,CAAA;AACtB,IAAA,OAAOD,GAAG,CAAA;GACX,EAAE,EAAS,CAAC,CAAA;AACf,CAAA;;AAEA;AACA;AACA;AACA;AACA;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AAI,CAAC8B,CAAC,CAAC,CAACC,IAAI,CAAE5B,GAAG,IAAK,CAACyB,gBAAgB,CAACC,CAAC,CAAC1B,GAAG,CAAC,EAAE2B,CAAC,CAAC3B,GAAG,CAAC,CAAC,CAAC,CAAA;AACzE,GAAA;AAEA,EAAA,IAAIM,KAAK,CAACC,OAAO,CAACmB,CAAC,CAAC,IAAIpB,KAAK,CAACC,OAAO,CAACoB,CAAC,CAAC,EAAE;IACxC,OACED,CAAC,CAACrC,MAAM,KAAKsC,CAAC,CAACtC,MAAM,IACrBqC,CAAC,CAACG,KAAK,CAAC,CAACC,IAAI,EAAEC,KAAK,KAAKN,gBAAgB,CAACK,IAAI,EAAEH,CAAC,CAACI,KAAK,CAAC,CAAC,CAAC,CAAA;AAE9D,GAAA;AAEA,EAAA,OAAO,KAAK,CAAA;AACd;;;;;;;;;"}
|
package/build/esm/index.js
CHANGED
|
@@ -78,13 +78,13 @@ function createHistory(opts) {
|
|
|
78
78
|
push: (path, state) => {
|
|
79
79
|
assignKey(state);
|
|
80
80
|
queueTask(() => {
|
|
81
|
-
opts.pushState(path, state);
|
|
81
|
+
opts.pushState(path, state, onUpdate);
|
|
82
82
|
});
|
|
83
83
|
},
|
|
84
84
|
replace: (path, state) => {
|
|
85
85
|
assignKey(state);
|
|
86
86
|
queueTask(() => {
|
|
87
|
-
opts.replaceState(path, state);
|
|
87
|
+
opts.replaceState(path, state, onUpdate);
|
|
88
88
|
});
|
|
89
89
|
},
|
|
90
90
|
go: index => {
|
|
@@ -116,7 +116,8 @@ function createHistory(opts) {
|
|
|
116
116
|
stopBlocking();
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
|
-
}
|
|
119
|
+
},
|
|
120
|
+
flush: () => opts.flush?.()
|
|
120
121
|
};
|
|
121
122
|
}
|
|
122
123
|
function assignKey(state) {
|
|
@@ -129,25 +130,100 @@ function assignKey(state) {
|
|
|
129
130
|
// }
|
|
130
131
|
}
|
|
131
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Creates a history object that can be used to interact with the browser's
|
|
135
|
+
* navigation. This is a lightweight API wrapping the browser's native methods.
|
|
136
|
+
* It is designed to work with TanStack Router, but could be used as a standalone API as well.
|
|
137
|
+
* IMPORTANT: This API implements history throttling via a microtask to prevent
|
|
138
|
+
* excessive calls to the history API. In some browsers, calling history.pushState or
|
|
139
|
+
* history.replaceState in quick succession can cause the browser to ignore subsequent
|
|
140
|
+
* calls. This API smooths out those differences and ensures that your application
|
|
141
|
+
* state will *eventually* match the browser state. In most cases, this is not a problem,
|
|
142
|
+
* but if you need to ensure that the browser state is up to date, you can use the
|
|
143
|
+
* `history.flush` method to immediately flush all pending state changes to the browser URL.
|
|
144
|
+
* @param opts
|
|
145
|
+
* @param opts.getHref A function that returns the current href (path + search + hash)
|
|
146
|
+
* @param opts.createHref A function that takes a path and returns a href (path + search + hash)
|
|
147
|
+
* @returns A history instance
|
|
148
|
+
*/
|
|
132
149
|
function createBrowserHistory(opts) {
|
|
133
150
|
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
|
|
134
151
|
const createHref = opts?.createHref ?? (path => path);
|
|
135
|
-
|
|
152
|
+
let currentLocation = parseLocation(getHref(), window.history.state);
|
|
153
|
+
const getLocation = () => currentLocation;
|
|
154
|
+
let next;
|
|
155
|
+
|
|
156
|
+
// Because we are proactively updating the location
|
|
157
|
+
// in memory before actually updating the browser history,
|
|
158
|
+
// we need to track when we are doing this so we don't
|
|
159
|
+
// notify subscribers twice on the last update.
|
|
160
|
+
let tracking = true;
|
|
161
|
+
|
|
162
|
+
// We need to track the current scheduled update to prevent
|
|
163
|
+
// multiple updates from being scheduled at the same time.
|
|
164
|
+
let scheduled;
|
|
165
|
+
|
|
166
|
+
// This function is a wrapper to prevent any of the callback's
|
|
167
|
+
// side effects from causing a subscriber notification
|
|
168
|
+
const untrack = fn => {
|
|
169
|
+
tracking = false;
|
|
170
|
+
fn();
|
|
171
|
+
tracking = true;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// This function flushes the next update to the browser history
|
|
175
|
+
const flush = () => {
|
|
176
|
+
// Do not notify subscribers about this push/replace call
|
|
177
|
+
untrack(() => {
|
|
178
|
+
if (!next) return;
|
|
179
|
+
window.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
|
|
180
|
+
// Reset the nextIsPush flag and clear the scheduled update
|
|
181
|
+
next = undefined;
|
|
182
|
+
scheduled = undefined;
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// This function queues up a call to update the browser history
|
|
187
|
+
const queueHistoryAction = (type, path, state, onUpdate) => {
|
|
188
|
+
const href = createHref(path);
|
|
189
|
+
|
|
190
|
+
// Update the location in memory
|
|
191
|
+
currentLocation = parseLocation(href, state);
|
|
192
|
+
|
|
193
|
+
// Keep track of the next location we need to flush to the URL
|
|
194
|
+
next = {
|
|
195
|
+
href,
|
|
196
|
+
state,
|
|
197
|
+
isPush: next?.isPush || type === 'push'
|
|
198
|
+
};
|
|
199
|
+
// Notify subscribers
|
|
200
|
+
onUpdate();
|
|
201
|
+
if (!scheduled) {
|
|
202
|
+
// Schedule an update to the browser history
|
|
203
|
+
scheduled = Promise.resolve().then(() => flush());
|
|
204
|
+
}
|
|
205
|
+
};
|
|
136
206
|
return createHistory({
|
|
137
207
|
getLocation,
|
|
138
208
|
subscriber: onUpdate => {
|
|
139
|
-
window.addEventListener(pushStateEvent,
|
|
140
|
-
|
|
209
|
+
window.addEventListener(pushStateEvent, () => {
|
|
210
|
+
currentLocation = parseLocation(getHref(), window.history.state);
|
|
211
|
+
onUpdate();
|
|
212
|
+
});
|
|
213
|
+
window.addEventListener(popStateEvent, () => {
|
|
214
|
+
currentLocation = parseLocation(getHref(), window.history.state);
|
|
215
|
+
onUpdate();
|
|
216
|
+
});
|
|
141
217
|
var pushState = window.history.pushState;
|
|
142
218
|
window.history.pushState = function () {
|
|
143
219
|
let res = pushState.apply(history, arguments);
|
|
144
|
-
onUpdate();
|
|
220
|
+
if (tracking) onUpdate();
|
|
145
221
|
return res;
|
|
146
222
|
};
|
|
147
223
|
var replaceState = window.history.replaceState;
|
|
148
224
|
window.history.replaceState = function () {
|
|
149
225
|
let res = replaceState.apply(history, arguments);
|
|
150
|
-
onUpdate();
|
|
226
|
+
if (tracking) onUpdate();
|
|
151
227
|
return res;
|
|
152
228
|
};
|
|
153
229
|
return () => {
|
|
@@ -157,16 +233,13 @@ function createBrowserHistory(opts) {
|
|
|
157
233
|
window.removeEventListener(popStateEvent, onUpdate);
|
|
158
234
|
};
|
|
159
235
|
},
|
|
160
|
-
pushState: (path, state) =>
|
|
161
|
-
|
|
162
|
-
},
|
|
163
|
-
replaceState: (path, state) => {
|
|
164
|
-
window.history.replaceState(state, '', createHref(path));
|
|
165
|
-
},
|
|
236
|
+
pushState: (path, state, onUpdate) => queueHistoryAction('push', path, state, onUpdate),
|
|
237
|
+
replaceState: (path, state, onUpdate) => queueHistoryAction('replace', path, state, onUpdate),
|
|
166
238
|
back: () => window.history.back(),
|
|
167
239
|
forward: () => window.history.forward(),
|
|
168
240
|
go: n => window.history.go(n),
|
|
169
|
-
createHref: path => createHref(path)
|
|
241
|
+
createHref: path => createHref(path),
|
|
242
|
+
flush
|
|
170
243
|
});
|
|
171
244
|
}
|
|
172
245
|
function createHashHistory() {
|
|
@@ -223,6 +296,8 @@ function createRandomKey() {
|
|
|
223
296
|
return (Math.random() + 1).toString(36).substring(7);
|
|
224
297
|
}
|
|
225
298
|
|
|
299
|
+
// export type Expand<T> = T
|
|
300
|
+
|
|
226
301
|
// type Compute<T> = { [K in keyof T]: T[K] } | never
|
|
227
302
|
|
|
228
303
|
// type AllKeys<T> = T extends any ? keyof T : never
|