/
context.go
31 lines (25 loc) · 866 Bytes
/
context.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
package reactors
import "context"
// WithReactor creates a child context with the specified reactor for contextual use
func WithReactor[S any](underlying context.Context, reactor Boundary[S]) context.Context {
return context.WithValue(underlying, ContextKey, reactor)
}
// For retrieves a reactor Boundary from the given context
func For[S any](ctx context.Context) Boundary[S] {
return ctx.Value(ContextKey).(Boundary[S])
}
func Maybe[S any](ctx context.Context) (boundary Boundary[S], has bool) {
if ctx == nil {
return nil, false
}
b, ok := ctx.Value(ContextKey).(Boundary[S])
if !ok || b == nil {
return nil, false
}
return b, true
}
// ScheduleFunc schedules an op on the contextual reactor Boundary.
func ScheduleFunc(ctx context.Context, op TickEventFunc) {
For[any](ctx).ScheduleFunc(ctx, op)
}
const ContextKey = "meschbach.junk.reactor"