Skip to content

Commit

Permalink
refactor: optimized url, createUrl and goto
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Feb 20, 2025
1 parent e9e55e9 commit fdc96ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/runtime/Route/Route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createUrl } from '../helpers/index.js'
import { getCreateUrl } from '../helpers/index.js'
import { populateUrl } from '../utils/index.js'
import { RouteFragment } from './RouteFragment.js'
import { LoadCache } from './utils.js'
Expand Down Expand Up @@ -138,7 +138,7 @@ export class Route {
/** @type { RoutifyLoadContext } */
const ctx = {
route: this,
url: createUrl(fragment, this.router),
url: getCreateUrl(fragment, this.router),
prevRoute,
isNew: !isSameBranch,
fetch,
Expand Down
38 changes: 11 additions & 27 deletions lib/runtime/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,10 @@ export const goto = {
return derived(url, $url =>
/** @type {Goto} */
(pathOrNode, userParams, options) => {
const path =
typeof pathOrNode === 'string' ? pathOrNode : pathOrNode?.path

/** @type {options} */
const defaults = { mode: 'push', state: {} }
options = { ...defaults, ...options }
const newUrl = $url(path, userParams, options)
const newUrl = $url(pathOrNode, userParams, options)
router.url[options.mode](newUrl, options.state)
return ''
},
Expand All @@ -91,7 +88,7 @@ export const goto = {
}

/**
* @typedef {(<T extends string | HTMLAnchorElement>(
* @typedef {(<T extends string | RNodeRuntime | HTMLAnchorElement>(
* inputPath: T,
* userParams?: { [x: string]: string; },
* options?: Partial<$UrlOptions>
Expand All @@ -105,29 +102,16 @@ export const url = {
subscribe: (run, invalidate) => {
const { fragment, router } = contexts

let InitialElem, initialParams, initialPath

const createUrlString = createUrl(fragment.fragment, router)
const getInitialUrl = () => createUrlString(initialPath, ...initialParams)

return derived(fragment.params, $params => {
if (InitialElem) {
// if we're dealing with an already set anchor element, set the href
InitialElem.setAttribute('href', getInitialUrl())
}

// todo for some reason we always need to return this function, but it really shouldn't be necessary when an elem has been set
return (pathOrElem, ...params) => {
return (pathElemOrNode, params, options) => {
const createUrl = getCreateUrl(fragment.fragment, router)
// if we're dealing with a string, return the rendered url
if (typeof pathOrElem != 'object') {
return createUrlString(pathOrElem, ...params)
}

// if we're dealing with an anchor element, store it
InitialElem = pathOrElem
initialParams = params
initialPath = InitialElem.getAttribute('href')
InitialElem.setAttribute('href', getInitialUrl())
if (!(pathElemOrNode instanceof HTMLAnchorElement))
return createUrl(pathElemOrNode, params, options)

// if we're dealing with an anchor element, update it
const path = pathElemOrNode.getAttribute('href')
pathElemOrNode.setAttribute('href', createUrl(path, $params, options))
}
}).subscribe(run, invalidate)
},
Expand All @@ -146,7 +130,7 @@ export const url = {
* @param {Router} router
* @returns {UrlFromString}
*/
export const createUrl =
export const getCreateUrl =
(fragment, router) =>
/** @type {UrlFromString} */
(pathOrNode, userParams = {}, options = {}) => {
Expand Down

0 comments on commit fdc96ac

Please sign in to comment.