Browse Source

Added more signal emit points. And comments.

signals-integration
Benjamin Shelton 4 weeks ago
parent
commit
4eb3faf1d7
2 changed files with 59 additions and 0 deletions
  1. +27
    -0
      router_bind.go
  2. +32
    -0
      signals.go

+ 27
- 0
router_bind.go View File

@ -277,6 +277,12 @@ func (r *Router) Bind(endpoint Controller) error {
// reflect their final state. (Note: bindRoute() also corrects slash
// termination based on trailing syntax.)
r.urls.Map(rt)
signals.EmitFunc(SigRouteComplete, func(fn interface{}) {
if f, ok := fn.(OnRouteComplete); ok {
f(r, rt)
}
})
}
return nil
@ -344,12 +350,33 @@ func (r *Router) ManualBind(method, path, suffix string, controller Controller,
route.Middleware = append(route.Middleware, middleware...)
}
// Signal: OnManualRouteSetup.
signals.EmitFunc(SigManualRouteSetup, func(fn interface{}) {
if f, ok := fn.(OnManualRouteSetup); ok {
f(r, route)
}
})
// Signal: OnEachRoute.
signals.EmitFunc(SigEachRoute, func(fn interface{}) {
if f, ok := fn.(OnEachRoute); ok {
f(r, route)
}
})
bindRoute(r, route)
// Map the route after being bound to the muxer. Guarantees route values
// reflect their final state. (Note: bindRoute() also corrects slash
// termination based on trailing syntax.)
r.urls.Map(route)
// Signal: OnRouteComplete.
signals.EmitFunc(SigRouteComplete, func(fn interface{}) {
if f, ok := fn.(OnRouteComplete); ok {
f(r, route)
}
})
}
// Static binds the path described by `urlroot` to the filesystem `path` as a


+ 32
- 0
signals.go View File

@ -55,8 +55,24 @@ type OnRouteWriteError func(*Route, Context, error) signals.Context
// SigRouteSetup emits after the route has been setup and method handlers have been configured.
var SigRouteSetup = signals.New((OnRouteSetup)(nil))
// OnRouteSetup accepts the router context and a slice of all Route structs
// derived from the controller as it is processed. Routes will be generated for
// each HTTP method, each special struct method, and any bind routes that were
// configured at runtime.
type OnRouteSetup func(*Router, []*Route)
// SigManulRouteSetup emits when routes are manually bound using ManualBind().
//
// This is an uncommon trigger since most routes will be configured via their
// controllers but some users may have a need to manually configure a route.
//
// There is no way to differentiate between routes configured via a controller
// and routes configured via ManualBind; use this signal when catching the
// latter.
var SigManualRouteSetup = signals.New((OnManualRouteSetup)(nil))
type OnManualRouteSetup func(*Router, *Route)
// SigEachRoute emits for each method handler bound per route.
//
// In Capstan's semantics, routes are defined individually by path *and* by
@ -66,11 +82,27 @@ type OnRouteSetup func(*Router, []*Route)
//
// Capstan uses this signal internally to expose the renderer's convenience
// function for attaching template functions and definitions.
//
// This signal emits for routes configured with ManualBind.
var SigEachRoute = signals.New((OnEachRoute)(nil))
// OnEachRoute accepts the router context and the Route currently being
// processed. This is triggered before bindRoute is called (meaning the Route is
// not yet attached to any muxer) and before the route is completely configured.
type OnEachRoute func(*Router, *Route)
// SigRouteComplete emits for each route after the setup, bindRoute, and URL
// mapping has been finalized.
//
// This signal is also emitted when manually bound routes are completed.
var SigRouteComplete = signals.New((OnRouteComplete)(nil))
// OnRouteComplete accepts the router context and the Route currently processed.
type OnRouteComplete func(*Router, *Route)
// SigBindRoute emits for each invocation of bindRoute (see funcs.go). This provides preliminary access to the router and muxer state prior to configuration.
var SigBindRoute = signals.New((OnBindRoute)(nil))
// OnBindRoute accepts as its arguments the router context, the route currently
// being processed, and the assigned muxer.
type OnBindRoute func(*Router, *Route, chi.Router) signals.Context

Loading…
Cancel
Save