summaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
authorewy <ewy0@protonmail.com>2026-04-17 00:41:31 +0200
committerewy <ewy0@protonmail.com>2026-04-17 00:41:31 +0200
commitc00cba98075ecddc82de4690574e06a9fa687ca3 (patch)
tree81e6ef8c7203479189c61e3abe7dbd20f7d0156d /model
parentb175e1541b56c0b17ca604128b6ea017ad7548b9 (diff)
make state creation asynchronous
Diffstat (limited to 'model')
-rw-r--r--model/new.go60
1 files changed, 34 insertions, 26 deletions
diff --git a/model/new.go b/model/new.go
index c2d5e37..e23bbf6 100644
--- a/model/new.go
+++ b/model/new.go
@@ -6,44 +6,52 @@ import (
"path/filepath"
"pik/identity"
"strings"
+ "sync"
)
func NewState(f fs.FS, locations []string, indexers []Indexer, runners []Runner) (*State, []error) {
var errs []error
st := &State{}
+ wg := sync.WaitGroup{}
for _, loc := range locations {
- _, dirName := filepath.Split(loc)
- src := &Source{
- Path: loc,
- Identity: identity.New(dirName),
- }
- loc = strings.TrimSuffix(loc, "/")
- loc = strings.TrimPrefix(loc, "/")
-
- if loc == "" {
- continue
- }
-
- for _, indexer := range indexers {
+ wg.Go(func() {
+ _, dirName := filepath.Split(loc)
+ src := &Source{
+ Path: loc,
+ Identity: identity.New(dirName),
+ }
+ loc = strings.TrimSuffix(loc, "/")
+ loc = strings.TrimPrefix(loc, "/")
- s, err := fs.Sub(f, loc)
- if err != nil && !errors.Is(err, fs.ErrNotExist) {
- errs = append(errs, err)
- continue
+ if loc == "" {
+ return
}
- targets, err := indexer.Index("/"+loc, s, runners)
- if err != nil && !errors.Is(err, fs.ErrNotExist) {
- errs = append(errs, err)
- continue
+
+ myWg := sync.WaitGroup{}
+ for _, indexer := range indexers {
+ myWg.Go(func() {
+ s, err := fs.Sub(f, loc)
+ if err != nil && !errors.Is(err, fs.ErrNotExist) {
+ errs = append(errs, err)
+ return
+ }
+ targets, err := indexer.Index("/"+loc, s, runners)
+ if err != nil && !errors.Is(err, fs.ErrNotExist) {
+ errs = append(errs, err)
+ return
+ }
+ src.Targets = append(src.Targets, targets...)
+ })
}
- src.Targets = append(src.Targets, targets...)
- }
+ myWg.Wait()
- if src.Targets != nil {
- st.Sources = append(st.Sources, src)
- }
+ if src.Targets != nil {
+ st.Sources = append(st.Sources, src)
+ }
+ })
}
+ wg.Wait()
return st, errs
}