Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: ensured that history does not notify twice for certain actions and has index in state #3017

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

tomrehnstrom
Copy link
Contributor

No description provided.

Copy link

nx-cloud bot commented Dec 15, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit c5cf0a9. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


🟥 Failed Commands
nx affected --targets=test:eslint,test:unit,test:e2e,test:types,test:build,build --parallel=3

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Dec 15, 2024

Open in Stackblitz

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@3017

@tanstack/create-router

npm i https://pkg.pr.new/@tanstack/create-router@3017

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@3017

@tanstack/react-cross-context

npm i https://pkg.pr.new/@tanstack/react-cross-context@3017

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@3017

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@3017

@tanstack/react-router-with-query

npm i https://pkg.pr.new/@tanstack/react-router-with-query@3017

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@3017

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@3017

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@3017

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@3017

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@3017

@tanstack/start

npm i https://pkg.pr.new/@tanstack/start@3017

@tanstack/start-vite-plugin

npm i https://pkg.pr.new/@tanstack/start-vite-plugin@3017

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@3017

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@3017

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@3017

commit: c5cf0a9

// This is a fallback for when updating the router and there are history entries without index
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const currentIndex = location.state.index ?? 0
state = assignKey({ ...state, index: currentIndex + 1 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put this logic into assigKey()?

// This is a fallback for when updating the router and there are history entries without index
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const currentIndex = location.state.index ?? 0
state = assignKey({ ...state, index: currentIndex })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, can we put this logic into assigKey()?

@@ -551,6 +568,7 @@ export function createMemoryHistory(
entries[index] = path
},
back: () => {
console.log('back', index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

@@ -51,17 +52,12 @@ export interface ParsedPath {

export interface HistoryState {
key?: string
index: number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be index?: number as the index is not set for the initial entry?

also, i think we should use a more ... obscure name to not collide with user data

e.g. __TSR_index ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always available after we run through parseHref, this is because, if the route has no state (should be valid only for initial route), we default it to { index: 0 }. About the naming, sure, but by that logic, shouldn't the key also be renamed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, yes. but the key has been there since the beginning, but adding an index now could introduce a breaking change

return { router }
}

describe('history: History gives correct notifcations and state', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('history: History gives correct notifcations and state', () => {
describe('history: History gives correct notifications and state', () => {

})

const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should run the same test for all history types

subscriber({ location, action: { type: 'ROLLBACK' } }),
)
const handleIndexChange = (action: SubscriberHistoryAction) => {
if (opts.notifyOnIndexChange ?? true) notify(action)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please always use { } even for one liners?

@@ -375,9 +377,10 @@ export function createBrowserHistory(opts?: {
}
}

const onPushPop = () => {
// NOTE: this function can probably be removed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so can we remove it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants