1package cache
2
3import (
4 "fmt"
5 "io"
6 "strconv"
7
8 "github.com/git-bug/git-bug/entity"
9)
10
11type BuildEventType int
12
13const (
14 _ BuildEventType = iota
15 // BuildEventCacheIsBuilt signal that the cache is being built (aka, not skipped)
16 BuildEventCacheIsBuilt
17 // BuildEventRemoveLock signal that an old repo lock has been cleaned
18 BuildEventRemoveLock
19 // BuildEventStarted signal the beginning of a cache build for an entity
20 BuildEventStarted
21 // BuildEventProgress signal progress in the cache building for an entity
22 BuildEventProgress
23 // BuildEventFinished signal the end of a cache build for an entity
24 BuildEventFinished
25)
26
27// BuildEvent carry an event happening during the cache build process.
28type BuildEvent struct {
29 // Err carry an error if the build process failed. If set, no other field matters.
30 Err error
31 // Typename is the name of the entity of which the event relate to. Can be empty if no particular entity is involved.
32 Typename string
33 // Event is the type of the event.
34 Event BuildEventType
35 // Total is the total number of elements being built. Set if Event is BuildEventStarted.
36 Total int64
37 // Progress is the current count of processed elements. Set if Event is BuildEventProgress.
38 Progress int64
39}
40
41type EntityEventType int
42
43const (
44 _ EntityEventType = iota
45 EntityEventCreated
46 EntityEventUpdated
47 EntityEventRemoved
48)
49
50// Observer gets notified of changes in entities in the cache
51type Observer interface {
52 EntityEvent(event EntityEventType, repoName string, typename string, id entity.Id)
53}
54
55func (e EntityEventType) MarshalGQL(w io.Writer) {
56 switch e {
57 case EntityEventCreated:
58 _, _ = w.Write([]byte(strconv.Quote("CREATED")))
59 case EntityEventUpdated:
60 _, _ = w.Write([]byte(strconv.Quote("UPDATED")))
61 case EntityEventRemoved:
62 _, _ = w.Write([]byte(strconv.Quote("REMOVED")))
63 default:
64 panic("missing case")
65 }
66}
67
68func (e *EntityEventType) UnmarshalGQL(v interface{}) error {
69 str, ok := v.(string)
70 if !ok {
71 return fmt.Errorf("enums must be strings")
72 }
73 switch str {
74 case "CREATED":
75 *e = EntityEventCreated
76 case "UPDATED":
77 *e = EntityEventUpdated
78 case "REMOVED":
79 *e = EntityEventRemoved
80 default:
81 return fmt.Errorf("%s is not a valid EntityEventType", str)
82 }
83 return nil
84}