Router

open class Router

An object that routes paths to a set of connected routers. Child router are added via the add(child:priority:) function. When the router is requested to handle a route it will query the children by priority order. If 2 children have identical priorities they will be queried in the order they were added.

  • A closure that will be notified when a route is handled.

    Declaration

    Swift

    public typealias CompletionHandler = (_ routeHandler: Router?) -> Void

    Parameters

    routeHandler

    The route handler that handled the route.

  • The immediate parent. This will be set automatically by the parent. The parent has a priority of Priority.parent, making it the last router to be queried.

    Declaration

    Swift

    public weak var parent: Router?
  • An array of children that have been added to the router, sorted in decending priority.

    Declaration

    Swift

    public var children: [Router] { get }
  • Create an empty router.

    Declaration

    Swift

    public init()
  • Attempt to handle the provided path. The child routers will be sorted by their priorities using the standard library sorted function; the sorting algorithm is not guaranteed to be stable, although this is unlikely to change in the future. See https://forums.swift.org/t/is-sort-stable-in-swift-5/21297/11.

    The completion closure will be called with the router that handled the path, or nil if the path was not handled.

    Declaration

    Swift

    open func handle<Path>(
        path: Path,
        ignoring: [Router] = [],
        completionHandler: ((Router?) -> Void)? = nil
    )

    Parameters

    path

    The path to attempt to handle.

    ignoring

    An array of routers to ignore when traversing the tree of routers.

    completionHandler

    A closure that will be called with the router that handled the path, or nil if the path was not handled.

  • Add the provided router as a child of this router. The added child will be queried when attempting to handle a path via handle(path:ignoring:completionHandler:).

    If the provided router is an existing child of this router the priority will be updated.

    Declaration

    Swift

    open func add(child router: Router, priority: Priority = .medium)

    Parameters

    router

    The router to add as a child.

    priority

    The priority to assign to the child. Defaults to .medium.

  • Remove the provided router from the set of router that will be queried by this router. If the router is a child of this router the parent will be set to nil. If it is not a child this function does nothing.

    Declaration

    Swift

    open func remove(child router: Router)

    Parameters

    router

    The router to remove.

  • A closure that can be queried when attempting to handle a path.

    Declaration

    Swift

    public typealias AsynchronousClosurePathHandler<Path> = (_ path: Path, _ completionHandler: @escaping (_ didHandle: Bool) -> Void) -> Void

    Parameters

    path

    The path to attempt to handle.

    completionHandler

    A closure to be called when the path has been handled, or it is determined the path cannot be handled.

  • Add the provided closure to be a child of the router, handling routes of type Path.

    When the closure is called it should attempt to handle the path. If the path is handled the closure should be called with true, otherwise the closure should be called with false.

    Declaration

    Swift

    @discardableResult
    public func addPathHandler<Path>(
        priority: Priority = .medium,
        pathHandler: @escaping AsynchronousClosurePathHandler<Path>
    ) -> Router

    Parameters

    priority

    The priority of the handler. Defaults to medium.

    pathHandler

    A closure that will be queried when handling a path of type Path.

  • Add the provided closure to be a child of the router, handling routes of type Path.

    When the closure is called it should attempt to handle the path. If the path is handled the closure should be called with true, otherwise the closure should be called with false.

    Declaration

    Swift

    @discardableResult
    public func addHandlerForPaths<Path>(
        ofType pathType: Path.Type,
        priority: Priority = .medium,
        pathHandler: @escaping AsynchronousClosurePathHandler<Path>
    ) -> Router

    Parameters

    pathType

    The path type to handle. Pass this to aid with type checking.

    priority

    The priority of the handler. Defaults to medium.

    pathHandler

    A closure that will be queried when handling a path of type Path

  • The decendents of the router, in an unspecified order.

    Declaration

    Swift

    public var decendents: [Router] { get }
  • The parents of the router, ordered from the closest to furthest.

    Declaration

    Swift

    public var parents: [Router] { get }
  • A closure that can be queried when attempting to handle a path.

    Declaration

    Swift

    public typealias SynchronousClosurePathHandler<Path> = (_ path: Path) -> Bool

    Parameters

    path

    The path to attempt to handle.

    completionHandler

    A closure to be called when the path has been handled, or it is determined the path cannot be handled.

  • Add the provided closure to be a child of the router, handling routes of type Path.

    When the closure is called it should attempt to handle the path. If the path is handled the closure should be called with true, otherwise the closure should be called with false.

    Declaration

    Swift

    @discardableResult
    public func addPathHandler<Path>(
        priority: Priority = .medium,
        pathHandler: @escaping SynchronousClosurePathHandler<Path>
    ) -> Router

    Parameters

    priority

    The priority of the handler. Defaults to medium.

    pathHandler

    A closure that will be queried when handling a path of type Path.

  • Add the provided closure to be a child of the router, handling routes of type Path.

    When the closure is called it should attempt to handle the path. If the path is handled the closure should be called with true, otherwise the closure should be called with false.

    Declaration

    Swift

    @discardableResult
    public func addHandlerForPaths<Path>(
        ofType pathType: Path.Type,
        priority: Priority = .medium,
        pathHandler: @escaping SynchronousClosurePathHandler<Path>
    ) -> Router

    Parameters

    pathType

    The path type to handle. Pass this to aid with type checking.

    priority

    The priority of the handler. Defaults to medium.

    pathHandler

    A closure that will be queried when handling a path of type Path