Skip to content

Commit 480116a

Browse files
authored
Merge pull request #81 from JohnMaguire/context-documentation
Add some notes about the context parameter
2 parents 09657e2 + 0e6fbb3 commit 480116a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This is an implementation of [Facebook's DataLoader](https://github.com/facebook
99

1010
## Usage
1111
```go
12-
// setup batch function
12+
// setup batch function - the first Context passed to the Loader's Load
13+
// function will be provided when the batch function is called.
1314
batchFn := func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
1415
var results []*dataloader.Result
1516
// do some async work to get data for specified keys
@@ -26,6 +27,10 @@ loader := dataloader.NewBatchedLoader(batchFn)
2627
* A thunk is a function returned from a function that is a
2728
* closure over a value (in this case an interface value and error).
2829
* When called, it will block until the value is resolved.
30+
*
31+
* loader.Load() may be called multiple times for a given batch window.
32+
* The first context passed to Load is the object that will be passed
33+
* to the batch function.
2934
*/
3035
thunk := loader.Load(context.TODO(), dataloader.StringKey("key1")) // StringKey is a convenience method that make wraps string to implement `Key` interface
3136
result, err := thunk()

dataloader.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ func NewBatchedLoader(batchFn BatchFunc, opts ...Option) *Loader {
190190
return loader
191191
}
192192

193-
// Load load/resolves the given key, returning a channel that will contain the value and error
193+
// Load load/resolves the given key, returning a channel that will contain the value and error.
194+
// The first context passed to this function within a given batch window will be provided to
195+
// the registered BatchFunc.
194196
func (l *Loader) Load(originalContext context.Context, key Key) Thunk {
195197
ctx, finish := l.tracer.TraceLoad(originalContext, key)
196198

0 commit comments

Comments
 (0)