forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
106 lines (91 loc) · 1.93 KB
/
pool.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package disgord
import "sync"
type Pool interface {
Put(x Reseter)
Get() (x Reseter)
}
type pool struct {
pool sync.Pool
// New specifies a function to generate a
// value when Get would otherwise return nil.
// It may not be changed concurrently with calls to Get.
New func() Reseter
}
// Put resets the object before it is put back into the pool. We reset it here
// to quickly detect if there are other owners than the pool as it is inserted.
func (p *pool) Put(x Reseter) {
x.Reset()
p.pool.Put(x)
}
// Get selects an arbitrary item from the Pool, removes it from the
// Pool, and returns it to the caller.
// Get may choose to ignore the pool and treat it as empty.
// Callers should not assume any relation between values passed to Put and
// the values returned by Get.
//
// This assumes that p.New is always set.
func (p *pool) Get() (x Reseter) {
var ok bool
if x, ok = p.pool.Get().(Reseter); x == nil || !ok {
x = p.New()
}
return
}
//////////////////////////////////////////////////////
//
// Resource Pools
//
//////////////////////////////////////////////////////
func newPools() *pools {
p := &pools{}
p.channel = &pool{
New: func() Reseter {
return &Channel{}
},
}
p.user = &pool{
New: func() Reseter {
return &User{}
},
}
p.message = &pool{
New: func() Reseter {
return &Message{
Author: p.user.Get().(*User),
}
},
}
p.emoji = &pool{
New: func() Reseter {
return &Emoji{}
},
}
//p.msgCreate = &pool{
// New: func() Reseter {
// return &MessageCreate{
// Message: p.message.Get().(*Message),
// }
// },
//}
return p
}
type pools struct {
channel Pool
message Pool
user Pool
emoji Pool
// events
//msgCreate Pool // this is actually slower
}
func (p *pools) ChannelPool() Pool {
return p.channel
}
func (p *pools) MessagePool() Pool {
return p.message
}
func (p *pools) UserPool() Pool {
return p.user
}
func (p *pools) EmojiPool() Pool {
return p.emoji
}