registry.go 744 B

123456789101112131415161718192021222324252627282930313233
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package interceptor
  4. // Registry is a collector for interceptors.
  5. type Registry struct {
  6. factories []Factory
  7. }
  8. // Add adds a new Interceptor to the registry.
  9. func (r *Registry) Add(f Factory) {
  10. r.factories = append(r.factories, f)
  11. }
  12. // Build constructs a single Interceptor from a InterceptorRegistry
  13. func (r *Registry) Build(id string) (Interceptor, error) {
  14. if len(r.factories) == 0 {
  15. return &NoOp{}, nil
  16. }
  17. interceptors := []Interceptor{}
  18. for _, f := range r.factories {
  19. i, err := f.NewInterceptor(id)
  20. if err != nil {
  21. return nil, err
  22. }
  23. interceptors = append(interceptors, i)
  24. }
  25. return NewChain(interceptors), nil
  26. }