-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[chore] Router refactor #2655
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
Closed
Closed
[chore] Router refactor #2655
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78ac3de
Refactor out separate Prefetcher class
benmccann ea17363
Remove routing / prefetching dependency on renderer
benmccann 491114e
format
benmccann 9118644
fix tests
benmccann c936052
add comments to types
benmccann 8415838
add comment
benmccann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { get_anchor, get_href } from './router'; | ||
|
||
export class Prefetcher { | ||
/** | ||
* @param {{ | ||
* router: import('./router').Router | ||
* handle_prefetch: import('./types').PrefetchHandler | ||
* }} opts | ||
*/ | ||
constructor({ router, handle_prefetch }) { | ||
this.router = router; | ||
this.handle_prefetch = handle_prefetch; | ||
} | ||
|
||
init_listeners() { | ||
/** @param {MouseEvent|TouchEvent} event */ | ||
const trigger_prefetch = (event) => { | ||
const a = get_anchor(/** @type {Node} */ (event.target)); | ||
if (a && a.href && a.hasAttribute('sveltekit:prefetch')) { | ||
this.prefetch(get_href(a)); | ||
} | ||
}; | ||
|
||
/** @type {NodeJS.Timeout} */ | ||
let mousemove_timeout; | ||
|
||
/** @param {MouseEvent|TouchEvent} event */ | ||
const handle_mousemove = (event) => { | ||
clearTimeout(mousemove_timeout); | ||
mousemove_timeout = setTimeout(() => { | ||
trigger_prefetch(event); | ||
}, 20); | ||
}; | ||
|
||
addEventListener('touchstart', trigger_prefetch); | ||
addEventListener('mousemove', handle_mousemove); | ||
} | ||
|
||
/** | ||
* @param {URL} url | ||
* @returns {Promise<import('./types').NavigationResult>} | ||
*/ | ||
async prefetch(url) { | ||
const info = this.router.parse(url); | ||
|
||
if (!info) { | ||
throw new Error('Attempted to prefetch a URL that does not belong to this app'); | ||
} | ||
|
||
return this.handle_prefetch(info); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
/** @type {import('./renderer').Renderer} */ | ||
export let renderer; | ||
|
||
/** @type {import('./router').Router?} */ | ||
export let router; | ||
|
||
/** @param {import('./router').Router?} _ */ | ||
export function init(_) { | ||
router = _; | ||
/** @type {import('./prefetcher').Prefetcher?} */ | ||
export let prefetcher; | ||
|
||
/** | ||
* @param {import('./renderer').Renderer} render | ||
* @param {import('./router').Router?} route | ||
* @param {import('./prefetcher').Prefetcher?} prefetch | ||
*/ | ||
export function init(render, route, prefetch) { | ||
renderer = render; | ||
router = route; | ||
prefetcher = prefetch; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible later on to resolve this somewhat cyclic dependency? I read this as "renderer depends on router, which depends on renderer"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it's a good question. I'm not quite sure and don't have any ideas on how it would be done
the cycle isn't a new one. In
master
, the router takes the render as a constructor arg and then did this inside the constructor. I just moved where it's occurring. I agree with you that I don't love it, but am not sure how to change it. I'd be all ears if you have suggestionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dug into this a bit and added a comment explaining options for cleaning it up. It'd be too big of a change to try to tackle in this PR, but might make a nice follow up if I get some time 😄