Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface NavigateOptions {
}
export interface RouterHistory {
location: HistoryLocation
length: number
subscribers: Set<() => void>
subscribe: (cb: () => void) => () => void
push: (path: string, state?: any, navigateOpts?: NavigateOptions) => void
Expand Down Expand Up @@ -61,6 +62,7 @@ const stopBlocking = () => {

export function createHistory(opts: {
getLocation: () => HistoryLocation
getLength: () => number
pushState: (path: string, state: any) => void
replaceState: (path: string, state: any) => void
go: (n: number) => void
Expand Down Expand Up @@ -102,6 +104,9 @@ export function createHistory(opts: {
get location() {
return location
},
get length() {
return opts.getLength()
},
subscribers,
subscribe: (cb: () => void) => {
subscribers.add(cb)
Expand Down Expand Up @@ -293,6 +298,7 @@ export function createBrowserHistory(opts?: {

const history = createHistory({
getLocation,
getLength: () => win.history.length,
pushState: (href, state) => queueHistoryAction('push', href, state),
replaceState: (href, state) => queueHistoryAction('replace', href, state),
back: () => win.history.back(),
Expand Down Expand Up @@ -368,7 +374,7 @@ export function createMemoryHistory(

return createHistory({
getLocation,

getLength: () => entries.length,
pushState: (path, state) => {
currentState = state
// Removes all subsequent entries after the current index to start a new branch
Expand Down
13 changes: 13 additions & 0 deletions packages/history/tests/createMemoryHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,17 @@ describe('createMemoryHistory', () => {
history.back()
expect(history.location.pathname).toBe('/b')
})

test('length', () => {
const history = createMemoryHistory()
expect(history.length).toBe(1)
history.push('/a')
expect(history.length).toBe(2)
history.replace('/b')
expect(history.length).toBe(2)
history.back()
expect(history.length).toBe(2)
history.push('/c')
expect(history.length).toBe(2)
})
})