-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeyed-refcount-resolver.go
70 lines (60 loc) · 1.71 KB
/
keyed-refcount-resolver.go
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
package directive
import (
"context"
"github.com/aperturerobotics/util/keyed"
)
// KeyedRefCountResolver resolves a directive with a Keyed RefCount container.
//
// Adds a reference, waits for a value, and adds it to the handler.
// Removes the value and waits for a new one when the value is released.
// Calls MarkIdle when the value has been added.
// buildValue can be nil
// If buildValue is set, will be called with the values.
// if buildValue returns nil, nil, ignores the value.
type KeyedRefCountResolver[K, V comparable] struct {
rc *keyed.KeyedRefCount[K, V]
key K
buildValue func(ctx context.Context, val V) (Value, error)
}
// NewKeyedRefCountResolver constructs a new KeyedRefCountResolver.
func NewKeyedRefCountResolver[K, V comparable](
rc *keyed.KeyedRefCount[K, V],
key K,
buildValue func(ctx context.Context, val V) (Value, error),
) *KeyedRefCountResolver[K, V] {
return &KeyedRefCountResolver[K, V]{
rc: rc,
key: key,
buildValue: buildValue,
}
}
// Resolve resolves the values, emitting them to the handler.
func (r *KeyedRefCountResolver[K, V]) Resolve(ctx context.Context, handler ResolverHandler) error {
ref, data, _ := r.rc.AddKeyRef(r.key)
defer ref.Release()
var val Value
var empty V
if r.buildValue != nil {
var err error
val, err = r.buildValue(ctx, data)
if err != nil {
return err
}
} else if data != empty {
val = data
}
if val == nil {
return nil
}
_, accepted := handler.AddValue(val)
if !accepted {
return nil
}
// mark idle & wait for ctx to be canceled
handler.MarkIdle(true)
<-ctx.Done()
handler.ClearValues()
return context.Canceled
}
// _ is a type assertion
var _ Resolver = ((*KeyedRefCountResolver[string, int])(nil))