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.
nnet/core/lifetime.go

43 lines
721 B
Go

package core
2 years ago
import "git.noahlan.cn/noahlan/nnet/entity"
type (
2 years ago
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)
}
2 years ago
func (lt *lifetime) Open(entity entity.NetworkEntity) {
if len(lt.onOpen) <= 0 {
return
}
for _, handler := range lt.onOpen {
2 years ago
handler(entity)
}
}
2 years ago
func (lt *lifetime) Close(entity entity.NetworkEntity) {
if len(lt.onClosed) <= 0 {
return
}
for _, handler := range lt.onClosed {
2 years ago
handler(entity)
}
}