Implements a simple MRU cache that can be used to cache page components used by route handlers.
class PageCache {
constructor(options: {
max: number;
});
get(key: any, factory: (key: any) => any): any;
}
Constructs a new page cache.
constructor(options: {
max: number;
});
options
Options controlling the cache
options.max
The maximum number of cache entries to keep
Get an object from the cache, or if no matches found invoke a callback to create a new instance.
get(key: any, factory: (key: any) => any): any;
key
The key for the page.
factory
A callback to create the item when not found in the cache.
Route objects store information about the current navigation, including the URL, the matched handler and anything else the handler wants to associate with the route.
type Route = {
url: URL;
state: any;
current: boolean;
handler: RouteHandler;
viewState?: any;
page?: any;
title?: string;
};
true
when this is the current route.
There will only ever be one current route.
current: boolean;
The RouteHandler
associated with this route.
handler: RouteHandler;
The page component for this route.
CodeOnly nevers sets or uses this property, but it is included here because
by convention, most applications will set a page
property.
page?: any;
State associated with the route.
The router stores important information in the state object so the clients
should never edit settings in the state object. An application can however
store additional information in the state object, by setting properties on
it and then calling the replace
method.
state: any;
The route's page title
CodeOnly nevers sets or uses this property, but it is included here because
by convention, most applications will set a title
property.
title?: string;
The route's internalized URL.
url: URL;
The route's view state.
This information will be available on the Route object once
the mayEnter
event has been fired by the Router.
By default the web history router driver will save and restore the current document scroll position but applications can save and restore additional custom information as necessary. For more information see View State Restoration.
viewState?: any;
A route handler is an object that handles the navigation to and from a particular URL.
Route handlers are registered with the router during app startup and are called by the router when a URL is loaded and needs to be matched to a particular handler.
When a route handler matches a URL it will usually store additional information on the
Route
object that describes the component or page to be displayed for that
URL along with any other information the handler or the application might find useful.
See Route Handlers for more information.
type RouteHandler = {
pattern?: string | RegExp;
match?: (route: Route) => Promise<boolean>;
mayEnter?: (from: Route, to: Route) => Promise<boolean>;
mayLeave?: (from: Route, to: Route) => Promise<boolean>;
didEnter?: (from: Route, to: Route) => boolean;
didLeave?: (from: Route, to: Route) => boolean;
cancelEnter?: (from: Route, to: Route) => boolean;
cancelLeave?: (from: Route, to: Route) => boolean;
order?: number;
captureViewState?: (route: Route) => object;
restoreViewState?: (route: Route, state: object) => void;
};
Notifies that a route that may have been entered was cancelled.
cancelEnter?: (from: Route, to: Route) => boolean;
Notifies that a route that may have been left was cancelled.
cancelLeave?: (from: Route, to: Route) => boolean;
A callback to capture the view state for this route handler's routes.
captureViewState?: (route: Route) => object;
Notifies that a route for this handler has been entered.
didEnter?: (from: Route, to: Route) => boolean;
Notifies that a route for this handler has been left.
didLeave?: (from: Route, to: Route) => boolean;
A callback to confirm the URL match. If not specified all URL's matching the pattern will be considered matches.
match?: (route: Route) => Promise<boolean>;
Notifies that a route for this handler may be entered.
mayEnter?: (from: Route, to: Route) => Promise<boolean>;
Notifies that a route for this handler may be left.
mayLeave?: (from: Route, to: Route) => Promise<boolean>;
Order of this route handler in relation to all others (default = 0, lowest first).
order?: number;
A string pattern (see urlPattern
) or regular expression to match URL pathnames to this route handler. If not specified, all URL's will match.
pattern?: string | RegExp;
A callback to restore the view state for this route handler's routes.
restoreViewState?: (route: Route, state: object) => void;
Default Router instance.
Nearly all applications only ever need a single router instance and can use this pre-created instance.
let router: Router;
A Router handles URL load requests, by creating route objects matching them to route handlers and firing associated events.
class Router {
constructor(handlers: RouteHandler[]);
start(driver: object | null): Promise<any>;
navigate: (url: URL | string) => Promise<Route>;
replace: (url: URL | string) => void;
back: () => void;
urlMapper: UrlMapper;
internalize(url: URL | string): URL | string;
externalize(url: URL | string): URL | string;
get current(): Route;
get pending(): Route;
addEventListener(event: string, handler: RouterEventAsync | RouterEventSync): void;
removeEventListener(event: string, handler: RouterEventAsync | RouterEventSync): void;
register(handlers: RouteHandler | RouteHandler[]): void;
revoke(predicate: (handler: RouteHandler) => boolean): void;
captureViewState: (route: Route) => object;
restoreViewState: (route: Route, state: object) => void;
}
Adds an event listener.
Available events are:
mayEnter
, mayLeave
async, cancellabledidEnter
, didLeave
sync, non-cancellablecancel
- sync, notification onlyThe async cancellable events should return Promise<boolean>
where a
resolved value of false
cancels the navigation.
All event handlers receive two arguments a from
and to
route object. For the
initial page load, the from
parameter will be null
.
addEventListener(event: string, handler: RouterEventAsync | RouterEventSync): void;
event
The event to listen to
handler
The event handler function
Navigates back one step in the history, or if there is no previous history navigates to the root URL.
back: () => void;
A callback to capture the view state for a route.
captureViewState: (route: Route) => object;
Constructs a new Router instance
constructor(handlers: RouteHandler[]);
handlers
*
An array of router handlers to initially register, however usually
handlers are registered using the register method.The current route object.
get current(): Route;
Externalizes a URL.
externalize(url: URL | string): URL | string;
url
The URL to internalizeInternalizes a URL.
internalize(url: URL | string): URL | string;
url
The URL to internalizeNavigates to a new URL.
navigate: (url: URL | string) => Promise<Route>;
The route currently being navigated to, but not yet committed.
get pending(): Route;
Registers one or more route handlers.
register(handlers: RouteHandler | RouteHandler[]): void;
handlers
The handler or handlers to registerRemoves a previously registered event handler.
removeEventListener(event: string, handler: RouterEventAsync | RouterEventSync): void;
event
The event to remove the listener for
handler
The event handler function to remove
Replaces the current URL, without performing a navigation.
replace: (url: URL | string) => void;
A callback to restore the view state for a route.
restoreViewState: (route: Route, state: object) => void;
Revoke previously registered handlers that match a predicate callback.
revoke(predicate: (handler: RouteHandler) => boolean): void;
predicate
Callback passed each route handler, return true
to removeStarts the router, using the specified driver
start(driver: object | null): Promise<any>;
driver
The router driver to use, or null
to use the default Web History router driver.An optional URL mapper to be used for URL internalization and externalization.
urlMapper: UrlMapper;
Provides URL internalization and externalization
class UrlMapper {
constructor(options: {
base: string;
hash: boolean;
});
internalize(url: URL): URL;
externalize(url: URL, asset?: boolean): URL;
}
Constructs a new Url Mapper
constructor(options: {
base: string;
hash: boolean;
});
options
Options for how to map URLs
options.base
The base URL of the external URL
options.hash
True to use hashed URLs
Externalizes a URL
externalize(url: URL, asset?: boolean): URL;
url
The URL to externalize
asset
If true, ignores the hash option (used to externalize asset URLs with base only)
Internalizes a URL
internalize(url: URL): URL;
url
The URL to internalize