vgrouter

package module
v0.0.0-...-eeb478c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2020 License: MIT Imports: 9 Imported by: 6

README

vgrouter

A URL router for Vugu. As of April 2020 this is functional. See test cases https://github.com/vugu/vgrouter/blob/master/rgen/rgen_test.go and https://github.com/vugu/vugu/tree/master/wasm-test-suite/test-012-router as examples.

Travis CI

More documention will follow.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindParam

type BindParam interface {
	BindParamRead() []string
	BindParamWrite(v []string)
}

BindParam is implemented by something that can be read and written as a URL param.

type ErrMissingPrefix

type ErrMissingPrefix struct {
	Message string // error message
	Path    string // path which is missing the prefix
}

ErrMissingPrefix is returned when a prefix was expected but not found.

func (ErrMissingPrefix) Error

func (e ErrMissingPrefix) Error() string

Error implements error.

type EventEnv

type EventEnv interface {
	Lock()         // acquire write lock
	UnlockOnly()   // release write lock
	UnlockRender() // release write lock and request re-render

}

EventEnv is our view of a Vugu EventEnv

type Navigator interface {

	// MustNavigate is like Navigate but panics upon error.
	MustNavigate(path string, query url.Values, opts ...NavigatorOpt)

	// Navigate will go the specified path and query.
	Navigate(path string, query url.Values, opts ...NavigatorOpt) error

	// Push will take any bound parameters and put them into the URL in the appropriate place.
	// Only works in wasm environment otherwise has no effect.
	Push(opts ...NavigatorOpt) error
}

Navigator interface has the methods commonly needed throughout the application to go to different pages and deal with routing.

type NavigatorOpt interface {
	IsNavigatorOpt()
}

NavigatorOpt is a marker interface to ensure that options to Navigator are passed intentionally.

var (
	// NavReplace will cause this navigation to replace the
	// current history entry rather than pushing to the stack.
	// Implemented using window.history.replaceState()
	NavReplace NavigatorOpt = intNavigatorOpt(1)

	// NavSkipRender will cause this navigation to not re-render
	// the current component state.  It can be used when a component
	// has already accounted for the render in some other way and
	// just wants to inform the Navigator of the current logical path and query.
	NavSkipRender NavigatorOpt = intNavigatorOpt(2)
)
type NavigatorRef struct {
	Navigator
}

NavigatorRef embeds a reference to a Navigator and provides a NavigatorSet method.

func (nr *NavigatorRef) NavigatorSet(v Navigator)

NavigatorSet implements NavigatorSetter interface.

type NavigatorSetter interface {
	NavigatorSet(v Navigator)
}

NavigatorSetter is implemented by things that can accept a Navigator.

type RouteHandler

type RouteHandler interface {
	RouteHandle(rm *RouteMatch)
}

RouteHandler implementations are called in response to a route matching (being navigated to).

type RouteHandlerFunc

type RouteHandlerFunc func(rm *RouteMatch)

RouteHandlerFunc implements RouteHandler as a function.

func (RouteHandlerFunc) RouteHandle

func (f RouteHandlerFunc) RouteHandle(rm *RouteMatch)

RouteHandle implements the RouteHandler interface.

type RouteMatch

type RouteMatch struct {
	Path      string     // path input (with any params interpolated)
	RoutePath string     // route path pattern with params as :param
	Params    url.Values // parameters (combined query and route params)
	Exact     bool       // true if the path is an exact match or false if just the prefix

	Request *http.Request // if ProcessRequest is used, this will be set to Request instance passed to it; server-side only
	// contains filtered or unexported fields
}

RouteMatch describes a request to navigate to a route.

func (*RouteMatch) Bind

func (r *RouteMatch) Bind(name string, param BindParam)

Bind adds a BindParam to the list of bound parameters. Later calls to Bind with the same name will replace the bind from earlier calls.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router handles URL routing.

func New

func New(eventEnv EventEnv) *Router

New returns a new Router.

func (*Router) AddRoute

func (r *Router) AddRoute(path string, rh RouteHandler) error

AddRoute adds a route to the list.

func (*Router) AddRouteExact

func (r *Router) AddRouteExact(path string, rh RouteHandler) error

AddRouteExact adds a route but only calls the handler if the path provided matches exactly. E.g. an exact route for "/a" will not fire when "/a/b" is navigated to (whereas AddRoute would do this).

func (*Router) BrowserAvail

func (r *Router) BrowserAvail() bool

BrowserAvail returns true if in browser mode.

func (*Router) GetNotFound

func (r *Router) GetNotFound() RouteHandler

GetNotFound returns what was set by SetNotFound. Provided to facilitate code that needs to wrap an existing not found behavior with another one.

func (*Router) ListenForPopState

func (r *Router) ListenForPopState() error

ListenForPopState registers an event listener so the user navigating with forward/back/history or fragment changes will be detected and handled by this router. Any call to SetUseFragment or SetPathPrefix should occur before calling ListenForPopState.

Only works in wasm environment and if called outside it will have no effect and return error.

func (*Router) MustAddRoute

func (r *Router) MustAddRoute(path string, rh RouteHandler)

MustAddRoute is like AddRoute but panics upon error.

func (*Router) MustAddRouteExact

func (r *Router) MustAddRouteExact(path string, rh RouteHandler)

MustAddRouteExact is like AddRouteExact but panic's upon error.

func (*Router) MustNavigate

func (r *Router) MustNavigate(path string, query url.Values, opts ...NavigatorOpt)

MustNavigate is like Navigate but panics upon error.

func (*Router) Navigate

func (r *Router) Navigate(path string, query url.Values, opts ...NavigatorOpt) error

Navigate will go the specified path and query.

func (*Router) ProcessRequest

func (r *Router) ProcessRequest(req *http.Request)

ProcessRequest processes the route contained in request. This is meant for server-side use with static rendering.

func (*Router) Pull

func (r *Router) Pull() error

Pull will read the current browser URL and navigate to it. This is generally called once at application startup. Only works in wasm environment otherwise has no effect and will return error. If a path prefix has been set and the path read does not start with prefix then an error of type *ErrMissingPrefix will be returned.

func (*Router) Push

func (r *Router) Push(opts ...NavigatorOpt) error

Push will take any bound parameters and put them into the URL in the appropriate place. Only works in wasm environment otherwise has no effect.

func (*Router) SetNotFound

func (r *Router) SetNotFound(rh RouteHandler)

SetNotFound assigns the handler for the case of no exact match reoute.

func (*Router) SetPathPrefix

func (r *Router) SetPathPrefix(pfx string)

SetPathPrefix sets the path prefix to use prepend or stripe when iteracting with the browser or external requests. Internally paths do not use this prefix. For example, calling `r.SetPrefix("/pfx"); r.Navigate("/a", nil)` will result in /pfx/a in the browser, but the path will be treated as just /a during processing. The prefix is stripped from the URL when calling Pull() and also when http.Requests are processed server-side.

func (*Router) SetUseFragment

func (r *Router) SetUseFragment(v bool)

SetUseFragment sets the fragment flag which if set means the fragment part of the URL (after the "#") is used as the path and query string. This can be useful for compatibility in applications which are served statically and do not have the ability to handle URL routing on the server side. This option is disabled by default. If used it should be set immediately after creation. Changing it after navigation may have undefined results.

func (*Router) UnbindParams

func (r *Router) UnbindParams()

UnbindParams will remove any previous parameter bindings. Note that this is called implicitly when navigiation occurs since that involves re-binding newly based on the path being navigated to.

func (*Router) UnlistenForPopState

func (r *Router) UnlistenForPopState() error

UnlistenForPopState removes the listener created by ListenForPopState.

type StringParam

type StringParam string

StringParam implements BindParam on a string.

func (*StringParam) BindParamRead

func (s *StringParam) BindParamRead() []string

BindParamRead implements BindParam.

func (*StringParam) BindParamWrite

func (s *StringParam) BindParamWrite(v []string)

BindParamWrite implements BindParam.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL