github.com/blend/go-sdk@v1.20220411.3/async/interceptor.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package async 9 10 // Interceptors chains calls to interceptors as a single interceptor. 11 func Interceptors(interceptors ...Interceptor) Interceptor { 12 if len(interceptors) == 0 { 13 return nil 14 } 15 if len(interceptors) == 1 { 16 return interceptors[0] 17 } 18 19 var curry = func(a, b Interceptor) Interceptor { 20 if a == nil && b == nil { 21 return nil 22 } 23 if a == nil { 24 return b 25 } 26 if b == nil { 27 return a 28 } 29 return InterceptorFunc(func(i Actioner) Actioner { 30 return b.Intercept(a.Intercept(i)) 31 }) 32 } 33 interceptor := interceptors[0] 34 for _, next := range interceptors[1:] { 35 interceptor = curry(interceptor, next) 36 } 37 return interceptor 38 } 39 40 // Interceptor returns an actioner for a given actioner. 41 type Interceptor interface { 42 Intercept(action Actioner) Actioner 43 } 44 45 var ( 46 _ Interceptor = (*InterceptorFunc)(nil) 47 ) 48 49 // InterceptorFunc is a function that implements action. 50 type InterceptorFunc func(Actioner) Actioner 51 52 // Intercept implements Interceptor for the function. 53 func (fn InterceptorFunc) Intercept(action Actioner) Actioner { 54 return fn(action) 55 }