Dispatcher
Defined in: dispatcher.ts:61
Simple route matcher and dispatcher.
Example usage:
import * as route from 'aberdeen/route';import { Dispatcher, MATCH_REST } from 'aberdeen/dispatcher';
const dispatcher = new Dispatcher();
dispatcher.addRoute("user", Number, "stream", String, (id, stream) => { console.log(`User ${id}, stream ${stream}`);});
dispatcher.dispatch(["user", "42", "stream", "music"]);// Logs: User 42, stream music
dispatcher.addRoute("search", MATCH_REST, (terms: string[]) => { console.log("Search terms:", terms);});
dispatcher.dispatch(["search", "classical", "piano"]);// Logs: Search terms: [ 'classical', 'piano' ]Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Dispatcher():
Dispatcher
Returns
Section titled “Returns”Dispatcher
Methods
Section titled “Methods”addRoute()
Section titled “addRoute()”addRoute<
T,H>(…args):void
Defined in: dispatcher.ts:73
Add a route with matchers and a handler function.
Type Parameters
Section titled “Type Parameters”T extends Matcher[]
Array of matcher types.
H extends (…args) => void
Handler function type, inferred from the matchers.
Parameters
Section titled “Parameters”…[...T[], H]
An array of matchers followed by a handler function. Each matcher can be:
- A string: matches exactly that string.
- A function: takes a string segment and returns a value (of any type) if it matches, or MATCH_FAILED if it doesn’t match. The return value (if not
MATCH_FAILEDand notNaN) is passed as a parameter to the handler function. The standard JavaScript functionsNumberandStringcan be used to match numeric and string segments respectively. - The special MATCH_REST symbol: matches the rest of the segments as an array of strings. Only one
MATCH_RESTis allowed.
Returns
Section titled “Returns”void
dispatch()
Section titled “dispatch()”dispatch(
segments):boolean
Defined in: dispatcher.ts:94
Dispatches the given segments to the first route handler that matches.
Parameters
Section titled “Parameters”segments
Section titled “segments”string[]
Array of string segments to match against the added routes. When using this class with the Aberdeen route module, one would typically pass route.current.p.
Returns
Section titled “Returns”boolean
True if a matching route was found and handled, false otherwise.