Aberdeen - v1.1.0
    Preparing search index...

    Class Dispatcher

    Simple route matcher and dispatcher.

    Example usage:

    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", matchRest, (terms: string[]) => {
    console.log("Search terms:", terms);
    });

    dispatcher.dispatch(["search", "classical", "piano"]);
    // Logs: Search terms: [ 'classical', 'piano' ]
    Index

    Constructors

    Methods

    Constructors

    Methods

    • Add a route with matchers and a handler function.

      Type Parameters

      • T extends Matcher[]

        Array of matcher types.

      • H extends (...args: ParamsFromMatchers<T>) => void

        Handler function type, inferred from the matchers.

      Parameters

      • ...args: [...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 matchFailed if it doesn't match. The return value (if not matchFailed and not NaN) is passed as a parameter to the handler function. The built-in functions Number and String can be used to match numeric and string segments respectively.
        • The special matchRest symbol: matches the rest of the segments as an array of strings. Only one matchRest is allowed, and it must be the last matcher.

      Returns void

    • Dispatches the given segments to the first route handler that matches.

      Parameters

      • 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 boolean

      True if a matching route was found and handled, false otherwise.