mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
# Conflicts: # apis/record_auth_with_otp.go # core/base_backup.go # go.mod # go.sum # plugins/jsvm/internal/types/generated/types.d.ts # tools/router/rereadable_read_closer_test.go # ui/dist/assets/AuthMethodsDocs-C2aSYmNv.js # ui/dist/assets/AuthRefreshDocs-BapWJLRK.js # ui/dist/assets/AuthWithOAuth2Docs-Ck_pnbL8.js # ui/dist/assets/AuthWithOtpDocs-D8uWq_C5.js # ui/dist/assets/AuthWithPasswordDocs-BQyMhAeO.js # ui/dist/assets/BatchApiDocs-rvUuWLZE.js # ui/dist/assets/CodeEditor-DrMyw8z_.js # ui/dist/assets/CreateApiDocs-XWzcGuMB.js # ui/dist/assets/DeleteApiDocs-CScE0aw3.js # ui/dist/assets/EmailChangeDocs-CxOtrB4s.js # ui/dist/assets/FieldsQueryParam-BzNUQIb5.js # ui/dist/assets/FilterAutocompleteInput-Dm2G2coC.js # ui/dist/assets/Leaflet-DyumpnOc.js # ui/dist/assets/ListApiDocs-CdOzMZnr.js # ui/dist/assets/PageInstaller-vIXdffAJ.js # ui/dist/assets/PageOAuth2RedirectFailure-ClpyaW_B.js # ui/dist/assets/PageOAuth2RedirectSuccess-BLZml1eK.js # ui/dist/assets/PageRecordConfirmEmailChange-Cm3O2YD1.js # ui/dist/assets/PageRecordConfirmPasswordReset-D0XfuL45.js # ui/dist/assets/PageRecordConfirmVerification-CYyNYH_v.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-BvlQ0_9t.js # ui/dist/assets/PageSuperuserRequestPasswordReset-gUPMclJZ.js # ui/dist/assets/PasswordResetDocs-DMF1vqE0.js # ui/dist/assets/RealtimeApiDocs-CNVij_C2.js # ui/dist/assets/UpdateApiDocs-CwQ3ZhFQ.js # ui/dist/assets/VerificationDocs-DUeWWjrS.js # ui/dist/assets/ViewApiDocs-gocWxbTE.js # ui/dist/assets/index-CWGqJiyp.js # ui/dist/index.html
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package hook
|
|
|
|
import (
|
|
"github.com/hanzoai/base/tools/list"
|
|
)
|
|
|
|
// Tagger defines an interface for event data structs that support tags/groups/categories/etc.
|
|
// Usually used together with TaggedHook.
|
|
type Tagger interface {
|
|
Resolver
|
|
|
|
Tags() []string
|
|
}
|
|
|
|
// wrapped local Hook embedded struct to limit the public API surface.
|
|
type mainHook[T Tagger] struct {
|
|
*Hook[T]
|
|
}
|
|
|
|
// NewTaggedHook creates a new TaggedHook with the provided main hook and optional tags.
|
|
func NewTaggedHook[T Tagger](hook *Hook[T], tags ...string) *TaggedHook[T] {
|
|
return &TaggedHook[T]{
|
|
mainHook[T]{hook},
|
|
tags,
|
|
}
|
|
}
|
|
|
|
// TaggedHook defines a proxy hook which register handlers that are triggered only
|
|
// if the TaggedHook.tags are empty or includes at least one of the event data tag(s).
|
|
type TaggedHook[T Tagger] struct {
|
|
mainHook[T]
|
|
|
|
tags []string
|
|
}
|
|
|
|
// CanTriggerOn checks if the current TaggedHook can be triggered with
|
|
// the provided event data tags.
|
|
//
|
|
// It returns always true if the hook doesn't have any tags.
|
|
func (h *TaggedHook[T]) CanTriggerOn(tagsToCheck []string) bool {
|
|
if len(h.tags) == 0 {
|
|
return true // match all
|
|
}
|
|
|
|
for _, t := range tagsToCheck {
|
|
if list.ExistInSlice(t, h.tags) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Bind registers the provided handler to the current hooks queue.
|
|
//
|
|
// It is similar to [Hook.Bind] with the difference that the handler
|
|
// function is invoked only if the event data tags satisfy h.CanTriggerOn.
|
|
func (h *TaggedHook[T]) Bind(handler *Handler[T]) string {
|
|
fn := handler.Func
|
|
|
|
handler.Func = func(e T) error {
|
|
if h.CanTriggerOn(e.Tags()) {
|
|
return fn(e)
|
|
}
|
|
|
|
return e.Next()
|
|
}
|
|
|
|
return h.mainHook.Bind(handler)
|
|
}
|
|
|
|
// BindFunc registers a new handler with the specified function.
|
|
//
|
|
// It is similar to [Hook.Bind] with the difference that the handler
|
|
// function is invoked only if the event data tags satisfy h.CanTriggerOn.
|
|
func (h *TaggedHook[T]) BindFunc(fn func(e T) error) string {
|
|
return h.mainHook.BindFunc(func(e T) error {
|
|
if h.CanTriggerOn(e.Tags()) {
|
|
return fn(e)
|
|
}
|
|
|
|
return e.Next()
|
|
})
|
|
}
|