You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,39 +14,37 @@ This repository contains a **simple, in-process event dispatcher** to be used to
14
14
```go
15
15
// Various event types
16
16
constEventA = 0x01
17
-
constEventB = 0x02
18
17
19
18
// Event type for testing purposes
20
19
type myEvent struct{
21
-
kind uint32
22
20
Datastring
23
21
}
24
22
25
23
// Type returns the event type
26
24
func(evmyEvent) Type() uint32 {
27
-
returnev.kind
25
+
returnEventA
28
26
}
29
27
```
30
28
31
-
When publishing events, you can create a `Dispatcher[T]` which allows to `Publish()` and `Subscribe()` to various event types.
29
+
When publishing events, you can create a `Dispatcher` which is then used as a target of generic `event.Publish[T]()` and `event.Subscribe[T]()`functions to publish and subscribe to various event types respectively.
32
30
33
31
```go
34
-
bus:= event.NewDispatcher[Event]()
32
+
bus:= event.NewDispatcher()
35
33
36
34
// Subcribe to event A, and automatically unsubscribe at the end
37
-
deferbus.Subscribe(EventA, func(e Event) {
35
+
deferevent.Subscribe(bus, func(e Event) {
38
36
println("(consumer 1)", e.Data)
39
37
})()
40
38
41
39
// Subcribe to event A, and automatically unsubscribe at the end
42
-
deferbus.Subscribe(EventA, func(e Event) {
40
+
deferevent.Subscribe(bus, func(e Event) {
43
41
println("(consumer 2)", e.Data)
44
42
})()
45
43
46
44
// Publish few events
47
-
bus.Publish(newEventA("event 1"))
48
-
bus.Publish(newEventA("event 2"))
49
-
bus.Publish(newEventA("event 3"))
45
+
event.Publish(bus, newEventA("event 1"))
46
+
event.Publish(bus, newEventA("event 2"))
47
+
event.Publish(bus, newEventA("event 3"))
50
48
```
51
49
52
50
It should output something along these lines, where order is not guaranteed given that both subscribers are processing messages asyncrhonously.
0 commit comments