File size: 652 Bytes
6380833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package syncx

import (
	"context"
	"sync"
)

// OnceValues executes a function at most once and returns the cached result.
// This function behaves like sync.OnceValues, including caching both return
// values from the first call, but with a context argument to the function.
//
// Can be used to do lazy database lookups that may be needed by multiple callbacks but should execute at most once.
func OnceValues[T1, T2 any](fn func(context.Context) (T1, T2)) func(context.Context) (T1, T2) {
	var (
		once sync.Once
		v1   T1
		v2   T2
	)

	return func(ctx context.Context) (T1, T2) {
		once.Do(func() {
			v1, v2 = fn(ctx)
		})

		return v1, v2
	}
}