File size: 3,554 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package dataloader

import (
	"context"
	"sync"
	"time"
)

// FetcherParam provides batched loading of data with parameters. Unlike the basic Fetcher,
// this allows you to pass additional parameters that modify the fetch behavior.
// It maintains separate Fetcher instances for each unique parameter combination.
//
// Type parameters:
//   - K: The type of the unique identifier (key) for items being fetched
//   - P: The type of additional parameters that can be passed to modify fetch behavior
//   - V: The type of values being fetched
//
// Example usage:
//
//	type UserParams struct { Active bool }
//	paramLoader := dataloader.NewStoreLoaderParam(
//	  ctx,
//	  func(ctx context.Context, param UserParams, ids []string) ([]User, error) {
//	    return userStore.FindManyFiltered(ctx, ids, param.Active)
//	  },
//	  func(u User) string { return u.ID },
//	)
//	activeUser, err := paramLoader.FetchOneParam(ctx, "user-id", UserParams{Active: true})
type FetcherParam[K, P comparable, V any] struct {
	mx        sync.Mutex              // Protects the map of fetchers
	m         map[P]*Fetcher[K, V]    // Map of parameters to their corresponding fetchers
	FetchFunc FetchParamFunc[K, P, V] // Function called to fetch data with parameters
	IDFunc    IDFunc[K, V]            // Function to extract ID from values
	MaxBatch  int                     // Maximum batch size for each parameter's fetcher
	Delay     time.Duration           // Delay before executing batches
}

// FetchParamFunc defines the signature for functions that fetch data with parameters.
// The function receives a context, parameter value, and slice of IDs to fetch.
type FetchParamFunc[K, P comparable, V any] func(context.Context, P, []K) ([]V, error)

// NewStoreLoaderParam creates a new FetcherParam for loading data from a store with parameters.
// This allows you to batch requests not just by ID, but also by parameter values.
//
// The fetchMany function should handle the parameter and return values in any order.
// The FetcherParam will create separate Fetcher instances for each unique parameter combination.
func NewStoreLoaderParam[K, P comparable, V any](ctx context.Context, fetchMany FetchParamFunc[K, P, V], idFunc IDFunc[K, V]) *FetcherParam[K, P, V] {
	return &FetcherParam[K, P, V]{
		m:         make(map[P]*Fetcher[K, V]),
		FetchFunc: fetchMany,
		IDFunc:    idFunc,
		MaxBatch:  100,
		Delay:     5 * time.Millisecond,
	}
}

// FetchOneParam retrieves a single value by its ID using the specified parameters.
// Requests with the same parameter values will be batched together, while requests
// with different parameters will use separate batches.
//
// The method lazily creates new Fetcher instances for each unique parameter combination.
// This allows efficient batching while still supporting parameterized queries.
func (fp *FetcherParam[K, P, V]) FetchOneParam(ctx context.Context, id K, param P) (*V, error) {
	fp.mx.Lock()
	defer fp.mx.Unlock()

	if fp.m == nil {
		fp.m = make(map[P]*Fetcher[K, V])
	}

	// Get or create a Fetcher for this parameter combination
	loader, ok := fp.m[param]
	if !ok {
		loader = &Fetcher[K, V]{
			MaxBatch:  fp.MaxBatch,
			Delay:     fp.Delay,
			FetchFunc: func(ctx context.Context, ids []K) ([]V, error) { return fp.FetchFunc(ctx, param, ids) },
			IDFunc:    fp.IDFunc,
		}
		fp.m[param] = loader
	}

	return loader.FetchOne(ctx, id)
}

func (fp *FetcherParam[K, P, V]) Close() {
	fp.mx.Lock()
	defer fp.mx.Unlock()

	// Close all fetchers for each parameter
	for _, f := range fp.m {
		f.Close()
	}
}