Skip to content

Commit e1070ef

Browse files
authored
Add Signal to context provider interface (#4368)
1 parent bbabf2d commit e1070ef

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

internal/pkg/composable/controller.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ type contextProviderState struct {
300300
signal chan bool
301301
}
302302

303+
// Signal signals that something has changed in the provider.
304+
//
305+
// Note: This should only be used by fetch context providers, standard context
306+
// providers should use Set to update the overall state.
307+
func (c *contextProviderState) Signal() {
308+
// Notify the controller Run loop that a state has changed. The notification
309+
// channel has buffer size 1 so this ensures that an update will always
310+
// happen after this change, while coalescing multiple simultaneous changes
311+
// into a single controller update.
312+
select {
313+
case c.signal <- true:
314+
default:
315+
}
316+
}
317+
303318
// Set sets the current mapping.
304319
func (c *contextProviderState) Set(mapping map[string]interface{}) error {
305320
var err error
@@ -321,15 +336,7 @@ func (c *contextProviderState) Set(mapping map[string]interface{}) error {
321336
return nil
322337
}
323338
c.mapping = mapping
324-
325-
// Notify the controller Run loop that a state has changed. The notification
326-
// channel has buffer size 1 so this ensures that an update will always
327-
// happen after this change, while coalescing multiple simultaneous changes
328-
// into a single controller update.
329-
select {
330-
case c.signal <- true:
331-
default:
332-
}
339+
c.Signal()
333340
return nil
334341
}
335342

internal/pkg/composable/testing/context.go

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type ContextComm struct {
1616
lock sync.Mutex
1717
previous map[string]interface{}
1818
current map[string]interface{}
19+
// Signal calls onSignal.
20+
onSignal func()
1921
// Set calls onSet with the new value, so tests can
2022
// verify data from the provider.
2123
onSet func(map[string]interface{})
@@ -28,6 +30,16 @@ func NewContextComm(ctx context.Context) *ContextComm {
2830
}
2931
}
3032

33+
// Signal signals that something has changed in the provider.
34+
func (t *ContextComm) Signal() {
35+
t.lock.Lock()
36+
onSignal := t.onSignal
37+
t.lock.Unlock()
38+
if onSignal != nil {
39+
onSignal()
40+
}
41+
}
42+
3143
// Set sets the current mapping for the context.
3244
func (t *ContextComm) Set(mapping map[string]interface{}) error {
3345
var err error
@@ -45,6 +57,7 @@ func (t *ContextComm) Set(mapping map[string]interface{}) error {
4557
if onSet != nil {
4658
onSet(mapping)
4759
}
60+
t.Signal()
4861

4962
return nil
5063
}
@@ -63,6 +76,13 @@ func (t *ContextComm) Current() map[string]interface{} {
6376
return t.current
6477
}
6578

79+
// CallOnSignal sets the OnSignal callback.
80+
func (t *ContextComm) CallOnSignal(f func()) {
81+
t.lock.Lock()
82+
defer t.lock.Unlock()
83+
t.onSignal = f
84+
}
85+
6686
// CallOnSet sets the OnSet callback.
6787
func (t *ContextComm) CallOnSet(f func(map[string]interface{})) {
6888
t.lock.Lock()

internal/pkg/core/composable/providers.go

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ type FetchContextProvider interface {
1919
type ContextProviderComm interface {
2020
context.Context
2121

22+
// Signal signals that something has changed in the provider.
23+
//
24+
// Note: This should only be used by fetch context providers, standard context
25+
// providers should use Set to update the overall state.
26+
Signal()
27+
2228
// Set sets the current mapping for this context.
2329
Set(map[string]interface{}) error
2430
}

0 commit comments

Comments
 (0)