aboutsummaryrefslogtreecommitdiff
path: root/mixins.go
blob: 65309e458f466c44b9821f213287b77320ee5f6e (plain) (blame)
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
package rutina

import (
	"context"
	"log"
	"os"
)

type Mixin interface {
	apply(*Rutina)
}

type MixinContext struct {
	Context context.Context
}

func WithContext(context context.Context) *MixinContext {
	return &MixinContext{Context: context}
}

func (o MixinContext) apply(r *Rutina) {
	ctx, cancel := context.WithCancel(o.Context)
	r.ctx = ctx
	r.Cancel = cancel
}

type MixinLogger struct {
	Logger *log.Logger
}

func WithLogger(logger *log.Logger) *MixinLogger {
	return &MixinLogger{Logger: logger}
}

func WithStdLogger() *MixinLogger {
	return &MixinLogger{Logger: log.New(os.Stdout, "rutina", log.LstdFlags)}
}

func (o MixinLogger) apply(r *Rutina) {
	r.logger = o.Logger
}