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) } }