• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / arche / 10612783370

29 Aug 2024 09:52AM CUT coverage: 99.984% (-0.02%) from 100.0%
10612783370

push

github

web-flow
Fix Dispatch.AddListener (#424)

I noticed that my listeners which were added to an initially empty Dispatch were not being notified. This fixes it.

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

6379 of 6380 relevant lines covered (99.98%)

77075.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.62
/listener/dispatch.go
1
package listener
2

3
import (
4
        "github.com/mlange-42/arche/ecs"
5
        "github.com/mlange-42/arche/ecs/event"
6
)
7

8
// Dispatch event listener.
9
//
10
// Dispatches events to sub-listeners and manages subscription automatically, based on their settings.
11
// Sub-listeners should not alter their subscriptions or components after being added.
12
//
13
// To make it possible for systems to add listeners, Dispatch can be added to the [ecs.World] as a resource.
14
type Dispatch struct {
15
        listeners     []ecs.Listener
16
        events        event.Subscription
17
        components    ecs.Mask
18
        hasComponents bool
19
}
20

21
// NewDispatch returns a new [Dispatch] listener with the given sub-listeners.
22
func NewDispatch(listeners ...ecs.Listener) Dispatch {
4✔
23
        var events event.Subscription = 0
4✔
24
        var components ecs.Mask
4✔
25
        hasComponents := true
4✔
26
        for _, l := range listeners {
9✔
27
                events |= l.Subscriptions()
5✔
28
                cmp := l.Components()
5✔
29
                if cmp == nil {
7✔
30
                        hasComponents = false
2✔
31
                } else {
5✔
32
                        components = components.Or(cmp)
3✔
33
                }
3✔
34
        }
35
        return Dispatch{
4✔
36
                listeners:     listeners,
4✔
37
                events:        events,
4✔
38
                components:    components,
4✔
39
                hasComponents: hasComponents,
4✔
40
        }
4✔
41
}
42

43
// AddListener adds a sub-listener to this listener.
44
func (l *Dispatch) AddListener(ls ecs.Listener) {
3✔
45
        l.listeners = append(l.listeners, ls)
3✔
46
        l.events |= ls.Subscriptions()
3✔
47

3✔
48
        cmp := ls.Components()
3✔
49
        if cmp == nil {
3✔
UNCOV
50
                l.hasComponents = false
×
51
        } else {
3✔
52
                l.components = l.components.Or(cmp)
3✔
53
        }
3✔
54
}
55

56
// Notify the listener.
57
func (l *Dispatch) Notify(world *ecs.World, evt ecs.EntityEvent) {
68✔
58
        for _, ls := range l.listeners {
204✔
59
                trigger := ls.Subscriptions() & evt.EventTypes
136✔
60
                if trigger != 0 && subscribes(trigger, &evt.Added, &evt.Removed, ls.Components(), evt.OldRelation, evt.NewRelation) {
247✔
61
                        ls.Notify(world, evt)
111✔
62
                }
111✔
63
        }
64
}
65

66
// Subscriptions of the listener.
67
func (l *Dispatch) Subscriptions() event.Subscription {
23✔
68
        return l.events
23✔
69
}
23✔
70

71
// Components the listener subscribes to.
72
func (l *Dispatch) Components() *ecs.Mask {
19✔
73
        if l.hasComponents {
32✔
74
                return &l.components
13✔
75
        }
13✔
76
        return nil
6✔
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc