summaryrefslogtreecommitdiff
path: root/components.go
blob: 7091ad11279dcbaed544df30da3601ad8d260789 (plain)
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
package main

import (
	"github.com/ewy1/pik/spool"
	"sync"
)

// ComponentList is a list wrapper type which handles operation modes of the program
type ComponentList[T any] []T

// RunAsync checks components one by one and triggers the fire method in a goroutine.
// the method will return when the waitgroup is done (all initializers are finished)
func (c ComponentList[T]) RunAsync(fire func(T) error) {
	wg := sync.WaitGroup{}
	for _, i := range c {
		wg.Go(func() {
			err := fire(i)
			if err != nil {
				_, _ = spool.Warn("%v\n", err)
			}
		})
	}
	wg.Wait()
}

// RunSync checks components one by one and fires them synchronously.
// important when the order of init matters (for example, paths needs to go before cache)
func (c ComponentList[T]) RunSync(fire func(T) error) {
	for _, i := range c {
		err := fire(i)
		if err != nil {
			_, _ = spool.Warn("%v\n", err)
		}
	}
}