From c55375fa09534d226c8eb5319fe648017f13e0a8 Mon Sep 17 00:00:00 2001 From: Matthias Schwarz <70815012+matthiasschwarz@users.noreply.github.com> Date: Sat, 22 Feb 2025 18:28:41 +0100 Subject: [PATCH 1/2] feat(history): access memory history entries --- packages/history/src/index.ts | 57 +++++++++++++------ .../history/tests/createMemoryHistory.test.ts | 26 +++++++++ 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/packages/history/src/index.ts b/packages/history/src/index.ts index 574e6eb531..0328fef187 100644 --- a/packages/history/src/index.ts +++ b/packages/history/src/index.ts @@ -39,6 +39,15 @@ export interface RouterHistory { _ignoreSubscribers?: boolean } +export interface RouterMemoryHistory extends RouterHistory { + entries: () => Array +} + +export interface HistoryEntry { + index: number; + getState: () => ParsedHistoryState +} + export interface HistoryLocation extends ParsedPath { state: ParsedHistoryState } @@ -561,33 +570,43 @@ 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) @@ -599,7 +618,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( diff --git a/packages/history/tests/createMemoryHistory.test.ts b/packages/history/tests/createMemoryHistory.test.ts index caddb96ac0..424f0ddde3 100644 --- a/packages/history/tests/createMemoryHistory.test.ts +++ b/packages/history/tests/createMemoryHistory.test.ts @@ -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) + }) }) From df255073873383677f752e67029ab5544218d627 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 22 Feb 2025 17:47:59 +0000 Subject: [PATCH 2/2] ci: apply automated fixes --- packages/history/src/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/history/src/index.ts b/packages/history/src/index.ts index 0328fef187..41e9f3ea8f 100644 --- a/packages/history/src/index.ts +++ b/packages/history/src/index.ts @@ -44,7 +44,7 @@ export interface RouterMemoryHistory extends RouterHistory { } export interface HistoryEntry { - index: number; + index: number getState: () => ParsedHistoryState } @@ -586,7 +586,8 @@ export function createMemoryHistory( } }) - const getLocation = () => parseHref(entries[index]!.path, entries[index]!.entry.getState()) + const getLocation = () => + parseHref(entries[index]!.path, entries[index]!.entry.getState()) const routerHistory = createHistory({ getLocation, @@ -594,7 +595,7 @@ export function createMemoryHistory( 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).forEach(entry => { + entries.splice(index + 1).forEach((entry) => { entry.entry.index = -1 }) } @@ -605,7 +606,7 @@ export function createMemoryHistory( entries[index]!.entry.index = -1 entries[index] = { path, - entry: { index, getState: () => state } + entry: { index, getState: () => state }, } }, back: () => { @@ -620,7 +621,7 @@ export function createMemoryHistory( createHref: (path) => path, }) as RouterMemoryHistory - routerHistory.entries = () => entries.map(entry => entry.entry) + routerHistory.entries = () => entries.map((entry) => entry.entry) return routerHistory }