You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
721 B
Go
43 lines
721 B
Go
package core
|
|
|
|
import "git.noahlan.cn/noahlan/nnet/entity"
|
|
|
|
type (
|
|
LifetimeHandler func(entity entity.NetworkEntity)
|
|
|
|
lifetime struct {
|
|
onOpen []LifetimeHandler
|
|
onClosed []LifetimeHandler
|
|
}
|
|
)
|
|
|
|
var Lifetime = &lifetime{}
|
|
|
|
func (lt *lifetime) OnClosed(h LifetimeHandler) {
|
|
lt.onClosed = append(lt.onClosed, h)
|
|
}
|
|
|
|
func (lt *lifetime) OnOpen(h LifetimeHandler) {
|
|
lt.onOpen = append(lt.onOpen, h)
|
|
}
|
|
|
|
func (lt *lifetime) Open(entity entity.NetworkEntity) {
|
|
if len(lt.onOpen) <= 0 {
|
|
return
|
|
}
|
|
|
|
for _, handler := range lt.onOpen {
|
|
handler(entity)
|
|
}
|
|
}
|
|
|
|
func (lt *lifetime) Close(entity entity.NetworkEntity) {
|
|
if len(lt.onClosed) <= 0 {
|
|
return
|
|
}
|
|
|
|
for _, handler := range lt.onClosed {
|
|
handler(entity)
|
|
}
|
|
}
|