Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-core
Commits
786c4f4e
Commit
786c4f4e
authored
Jun 21, 2019
by
Łukasz Magiera
Committed by
Raúl Kripalani
Jun 21, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
eventbus interface changes (#22)
parent
5f4de346
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
12 deletions
+38
-12
event/bus.go
event/bus.go
+38
-12
No files found.
event/bus.go
View file @
786c4f4e
...
@@ -22,19 +22,45 @@ type Emitter interface {
...
@@ -22,19 +22,45 @@ type Emitter interface {
Emit
(
evt
interface
{})
Emit
(
evt
interface
{})
}
}
// Subscription represents a subscription to one or multiple event types.
type
Subscription
interface
{
io
.
Closer
// Out returns the channel from which to consume events.
Out
()
<-
chan
interface
{}
}
// Bus is an interface for a type-based event delivery system.
// Bus is an interface for a type-based event delivery system.
type
Bus
interface
{
type
Bus
interface
{
// Subscribe creates a new
s
ubscription.
// Subscribe creates a new
S
ubscription.
//
//
//
Failing to drain the channel may cause publish
er
s
to
block. CancelFunc must return after
//
eventType can be either a point
er to
a single event type, or a slice of pointers to
//
last send to the
channel.
//
subscribe to multiple event types at once, under a single subscription (and
channel
)
.
//
//
// Example:
// Failing to drain the channel may cause publishers to block.
// ch := make(chan EventT, 10)
//
// defer close(ch)
// Simple example
// cancel, err := eventbus.Subscribe(ch)
//
// defer cancel()
// sub, err := eventbus.Subscribe(new(EventType))
Subscribe
(
typedChan
interface
{},
opts
...
SubscriptionOpt
)
(
CancelFunc
,
error
)
// defer sub.Close()
// for e := range sub.Out() {
// event := e.(EventType) // guaranteed safe
// [...]
// }
//
// Multi-type example
//
// sub, err := eventbus.Subscribe([]interface{}{new(EventA), new(EventB)})
// defer sub.Close()
// for e := range sub.Out() {
// select e.(type):
// case EventA:
// [...]
// case EventB:
// [...]
// }
// }
Subscribe
(
eventType
interface
{},
opts
...
SubscriptionOpt
)
(
Subscription
,
error
)
// Emitter creates a new event emitter.
// Emitter creates a new event emitter.
//
//
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment