-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
52 lines (44 loc) · 1.18 KB
/
container.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
// Copyright 2024 Outreach Corporation. All Rights Reserved.
// Description: This file contains root dependency container
package plumber
import (
"context"
"errors"
)
// Container represents a root dependency container
type Container struct {
cleanup []func() error
}
// Cleanup registers a cleanup function
func (c *Container) Cleanup(fn func()) {
c.cleanup = append(c.cleanup, func() error {
fn()
return nil
})
}
// CleanupError registers a cleanup function returning an error
func (c *Container) CleanupError(fn func() error) {
c.cleanup = append(c.cleanup, fn)
}
// Close calls all cleanup functions
func (c *Container) Close() error {
errs := []error{}
for _, cleanup := range c.cleanup {
if err := cleanup(); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// DefineContainers defines supplied containers using given config and root container
func DefineContainers[C, CF any](ctx context.Context, cfg CF, definers []func(context.Context, CF, C), root C, containers ...interface {
Define(context.Context, CF, C)
}) C {
for _, d := range definers {
d(ctx, cfg, root)
}
for _, d := range containers {
d.Define(ctx, cfg, root)
}
return root
}