aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/clock.go
blob: fa944b33ea0c82d6df635d476d88523bcf85a9a0 (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 dag

import (
	"fmt"

	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/repository"
)

// ClockLoader is the repository.ClockLoader for Entity
func ClockLoader(defs ...Definition) repository.ClockLoader {
	clocks := make([]string, len(defs)*2)
	for _, def := range defs {
		clocks = append(clocks, fmt.Sprintf(creationClockPattern, def.namespace))
		clocks = append(clocks, fmt.Sprintf(editClockPattern, def.namespace))
	}

	return repository.ClockLoader{
		Clocks: clocks,
		Witnesser: func(repo repository.ClockedRepo) error {
			// We don't care about the actual identity so an IdentityStub will do
			resolver := identity.NewStubResolver()

			for _, def := range defs {
				// override the resolver
				def := def
				def.identityResolver = resolver

				// we actually just need to read all entities,
				// as that will create and update the clocks
				// TODO: concurrent loading to be faster?
				for b := range ReadAll(def, repo) {
					if b.Err != nil {
						return b.Err
					}
				}
			}
			return nil
		},
	}
}