Skip to content

Commit

Permalink
using createRouterNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
stackoverfloweth committed Jan 5, 2024
1 parent acf74bd commit bd84858
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/utilities/createRouter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { readonly } from 'vue'
import { Resolved, Route, RouteMethods, Routes } from '@/types'
import { createRouteMethods, resolveRoutes } from '@/utilities'
import { createRouteMethods, createRouterNavigation, resolveRoutes } from '@/utilities'
import { resolveRoutesRegex } from '@/utilities/resolveRoutesRegex'
import { routeMatch } from '@/utilities/routeMatch'
import { updateBrowserUrl } from '@/utilities/updateBrowserUrl'

type RouterPush = (url: string, options?: { replace: boolean }) => Promise<void>
type RouterReplace = (url: string) => Promise<void>
type RouterNavigation = (number: number) => Promise<void>
type RouterBackForward = () => Promise<void>
type RouterGo = (delta: number) => Promise<void>

export type Router<
TRoutes extends Routes
Expand All @@ -16,14 +17,15 @@ export type Router<
route: Readonly<Resolved<Route>>,
push: RouterPush,
replace: RouterReplace,
back: RouterNavigation,
forward: RouterNavigation,
go: RouterNavigation,
back: RouterBackForward,
forward: RouterBackForward,
go: RouterGo,
}

export function createRouter<T extends Routes>(routes: T): Router<T> {
const resolved = resolveRoutes(routes)
const resolvedWithRegex = resolveRoutesRegex(resolved)
const routerNavigation = createRouterNavigation()

// todo: implement this
const route: Router<T>['route'] = readonly({} as any)
Expand All @@ -35,23 +37,23 @@ export function createRouter<T extends Routes>(routes: T): Router<T> {
return updateBrowserUrl(url, options)
}

throw 'not implemented'
await routerNavigation.update(url, options)
}

const replace: RouterReplace = (url) => {
return push(url, { replace: true })
}

const forward: RouterNavigation = (number = 1) => {
return go(number)
const forward: RouterBackForward = async () => {
await routerNavigation.forward()
}

const back: RouterNavigation = (number = -1) => {
return go(number)
const back: RouterBackForward = async () => {
await routerNavigation.back()
}

const go: RouterNavigation = (_number) => {
throw 'not implemented'
const go: RouterGo = async (delta) => {
await routerNavigation.go(delta)
}

const router = {
Expand Down

0 comments on commit bd84858

Please sign in to comment.