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

Added Router BeforeAll Hook #228

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,16 @@ declare module '@lightningjs/blits' {
watch?: W & ComponentContext<P, S, M, C>
}

export interface ApplicationConfig<P extends Props, S, M, C, W> extends ComponentConfig<P, S, M, C, W> {
export interface RouterHooks {
beforeAll?: (to: Route, from: Route) => string | Route | Promise<string | Route>;
}

export interface RouterConfig {
/**
* Register hooks for the router
*/
hooks?: RouterHooks,

/**
* Routes definition
*
Expand All @@ -390,6 +399,27 @@ declare module '@lightningjs/blits' {
routes?: Route[]
}

export interface ApplicationConfig<P extends Props, S, M, C, W> extends ComponentConfig<P, S, M, C, W> {
/**
* Router configuration
*/
router?: RouterConfig

/**
* Routes definition
*
* @example
*
* ```js
* routes: [
* { path: '/', component: Home },
* { path: '/details', component: Details },
* { path: '/account', component: Account },
* ]
* ```
*/
routes?: Route[]
}

export interface Transition {
/**
Expand Down
6 changes: 3 additions & 3 deletions src/component/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import setupMethods from './methods.js'
import setupState from './state.js'
import setupComputed from './computed.js'
import setupInput from './input.js'
import setupRoutes from './routes.js'
import setupRouter from './routes.js'
import setupWatch from './watch.js'

import { registerHooks } from '../../lib/hooks.js'
Expand Down Expand Up @@ -53,8 +53,8 @@ export default function (component, config) {
// // setup watchers
if (config.watch) setupWatch(component, config.watch)

// // setup routes
if (config.routes) setupRoutes(component, config.routes)
// // setup router
if (config.router || config.routes) setupRouter(component, config.router || config.routes)

// // setup input
if (config.input) setupInput(component, config.input)
Expand Down
7 changes: 6 additions & 1 deletion src/component/setup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

import symbols from '../../lib/symbols.js'

export default (component, routes) => {
export default (component, data) => {
let routes = data
if (Array.isArray(data) === false) {
component[symbols.routerHooks] = data.hooks
routes = data.routes
}
component[symbols.routes] = []
Object.keys(routes).forEach((key) => {
// todo: validate routes[key] for expected format etc.
Expand Down
1 change: 1 addition & 0 deletions src/lib/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default {
ready: Symbol('ready'),
renderer: Symbol('renderer'),
routes: Symbol('routes'),
routerHooks: Symbol('routerHooks'),
settings: Symbol('settings'),
state: Symbol('state'),
stateKeys: Symbol('stateKeys'),
Expand Down
16 changes: 15 additions & 1 deletion src/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,22 @@ export const navigate = async function () {
const previousRoute = currentRoute
const hash = getHash()
let route = matchHash(hash, this.parent[symbols.routes])
let beforeHookOutput
if (route) {
let beforeAllResult
if (this.parent[symbols.routerHooks]) {
const hooks = this.parent[symbols.routerHooks]
if (hooks.beforeAll) {
beforeAllResult = await hooks.beforeAll(route, previousRoute)
if (isString(beforeAllResult)) {
to(beforeAllResult)
return
}
}
}
// If the resolved result is an object, assign it to the target route object
route = isObject(beforeAllResult) ? beforeAllResult : route

let beforeHookOutput
if (route.hooks) {
if (route.hooks.before) {
beforeHookOutput = await route.hooks.before.call(this.parent, route, previousRoute)
Expand Down