1 Star 0 Fork 0

jack/protoactor-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
behavior.go 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
Roger Johansson 提交于 2023-11-23 18:19 . cosmetics
package actor
import (
"log/slog"
)
type Behavior []ReceiveFunc
func NewBehavior() Behavior {
return make(Behavior, 0)
}
func (b *Behavior) Become(receive ReceiveFunc) {
b.clear()
b.push(receive)
}
func (b *Behavior) BecomeStacked(receive ReceiveFunc) {
b.push(receive)
}
func (b *Behavior) UnbecomeStacked() {
b.pop()
}
func (b *Behavior) Receive(context Context) {
behavior, ok := b.peek()
if ok {
behavior(context)
} else {
context.Logger().Error("empty behavior called", slog.Any("pid", context.Self()))
}
}
func (b *Behavior) clear() {
if len(*b) == 0 {
return
}
for i := range *b {
(*b)[i] = nil
}
*b = (*b)[:0]
}
func (b *Behavior) peek() (v ReceiveFunc, ok bool) {
if l := b.len(); l > 0 {
ok = true
v = (*b)[l-1]
}
return
}
func (b *Behavior) push(v ReceiveFunc) {
*b = append(*b, v)
}
func (b *Behavior) pop() (v ReceiveFunc, ok bool) {
if l := b.len(); l > 0 {
l--
ok = true
v = (*b)[l]
(*b)[l] = nil
*b = (*b)[:l]
}
return
}
func (b *Behavior) len() int {
return len(*b)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wujianhai/protoactor-go.git
git@gitee.com:wujianhai/protoactor-go.git
wujianhai
protoactor-go
protoactor-go
99a34e4ec9eb

搜索帮助