File size: 787 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
package dataloader

import (
	"context"
)

// batch represents a collection of requests that will be executed together.
// It maintains both the IDs to fetch and the result channels to notify when complete.
type batch[K comparable, V any] struct {
	ids     []K              // IDs to fetch in this batch
	results []*result[K, V]  // Result channels for each request
}

// Add appends a new request to the batch and returns a result channel.
// The caller can wait on the result channel to receive the fetched value.
func (b *batch[K, V]) Add(ctx context.Context, id K) *result[K, V] {
	if b.results == nil {
		b.results = make([]*result[K, V], 0, 1)
	}
	res := &result[K, V]{id: id, done: make(chan struct{})}
	b.results = append(b.results, res)
	b.ids = append(b.ids, id)
	return res
}