Skip to content

feat(history): access memory history entries #3524

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
58 changes: 41 additions & 17 deletions packages/history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export interface RouterHistory {
_ignoreSubscribers?: boolean
}

export interface RouterMemoryHistory extends RouterHistory {
entries: () => Array<HistoryEntry>
}

export interface HistoryEntry {
index: number
getState: () => ParsedHistoryState
}

export interface HistoryLocation extends ParsedPath {
state: ParsedHistoryState
}
Expand Down Expand Up @@ -561,33 +570,44 @@ export function createMemoryHistory(
} = {
initialEntries: ['/'],
},
): RouterHistory {
const entries = opts.initialEntries
): RouterMemoryHistory {
let index = opts.initialIndex
? Math.min(Math.max(opts.initialIndex, 0), entries.length - 1)
: entries.length - 1
const states = entries.map((_entry, index) =>
assignKeyAndIndex(index, undefined),
)
? Math.min(Math.max(opts.initialIndex, 0), opts.initialEntries.length - 1)
: opts.initialEntries.length - 1
const entries = opts.initialEntries.map((path, index) => {
const state = assignKeyAndIndex(index, undefined)
const entry: HistoryEntry = {
index,
getState: () => state,
}
return {
path,
entry,
}
})

const getLocation = () => parseHref(entries[index]!, states[index])
const getLocation = () =>
parseHref(entries[index]!.path, entries[index]!.entry.getState())

return createHistory({
const routerHistory = createHistory({
getLocation,
getLength: () => entries.length,
pushState: (path, state) => {
// Removes all subsequent entries after the current index to start a new branch
if (index < entries.length - 1) {
entries.splice(index + 1)
states.splice(index + 1)
entries.splice(index + 1).forEach((entry) => {
entry.entry.index = -1
})
}
states.push(state)
entries.push(path)
index = Math.max(entries.length - 1, 0)
index = Math.max(entries.length, 0)
entries.push({ path, entry: { index, getState: () => state } })
},
replaceState: (path, state) => {
states[index] = state
entries[index] = path
entries[index]!.entry.index = -1
entries[index] = {
path,
entry: { index, getState: () => state },
}
},
back: () => {
index = Math.max(index - 1, 0)
Expand All @@ -599,7 +619,11 @@ export function createMemoryHistory(
index = Math.min(Math.max(index + n, 0), entries.length - 1)
},
createHref: (path) => path,
})
}) as RouterMemoryHistory

routerHistory.entries = () => entries.map((entry) => entry.entry)

return routerHistory
}

export function parseHref(
Expand Down
26 changes: 26 additions & 0 deletions packages/history/tests/createMemoryHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,30 @@ describe('createMemoryHistory', () => {
history.push('/c', { i: 3 })
expect((history.location.state as any).i).toBe(3)
})

test('entries', () => {
const history = createMemoryHistory()
const initialEntry = history.entries()[0]
expect(initialEntry.index).toBe(0)
history.push('/a', { i: 1 })
const entryA = history.entries()[1]
expect(entryA.index).toBe(1)
expect(entryA.getState()['i']).toBe(1)
history.replace('/b', { i: 2 })
const entryB = history.entries()[1]
expect(entryB.index).toBe(1)
expect(entryB.getState()['i']).toBe(2)
expect(entryA.index).toBe(-1)
history.back()
history.push('/c', { i: 3 })
const entryC = history.entries()[1]
expect(entryC.index).toBe(1)
expect(entryC.getState()['i']).toBe(3)
expect(entryB.index).toBe(-1)
history.push('/d', { i: 4 })
const entryD = history.entries()[2]
expect(entryD.index).toBe(2)
expect(entryD.getState()['i']).toBe(4)
expect(entryC.index).toBe(1)
})
})