forked from lotusirous/go-concurrency-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (39 loc) · 969 Bytes
/
main.go
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
42
43
44
45
46
47
48
49
package main
import "time"
type Item struct{ Tile, Channel, GUID string }
// Fetcher fetches Items and returns the time when the next fetch
// should be attempted. On failure, fetch returns non-nil error.
type Fetcher interface {
Fetch() (item []Item, next time.Time, err error)
}
// A Subscription delivers Items over a channel. Close cancels the
// subscription, closes the Updates channel, and returns the last fetch error,
// if any.
type Subscription interface {
Updates() <-chan Item
Close() error
}
func Subscribe(fetcher Fetcher) Subscription {
s := &sub{
fetcher: fetcher,
updates: make(chan Item),
closing: make(chan chan error),
}
return s
}
type sub struct {
fetcher Fetcher
updates chan Item
closing chan chan error
}
func (s *sub) Updates() <-chan Item {
return s.updates
}
func (s *sub) Close() error {
// STOPCLOSESIG OMIT
errc := make(chan error)
s.closing <- errc // HLchan
return <-errc // HLchan
}
func main() {
}