[feat]: 支持自定义添加 context 用于控制 bot 存活 (#220)
This commit is contained in:
parent
76bd0a5648
commit
6629e77fd5
22
bot_login.go
22
bot_login.go
@ -1,6 +1,7 @@
|
|||||||
package openwechat
|
package openwechat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -132,16 +133,16 @@ func NewSyncReloadDataLoginOption(duration time.Duration) BotLoginOption {
|
|||||||
return &SyncReloadDataLoginOption{SyncLoopDuration: duration}
|
return &SyncReloadDataLoginOption{SyncLoopDuration: duration}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithModeOption 指定使用哪种客户端模式
|
// withModeOption 指定使用哪种客户端模式
|
||||||
type WithModeOption struct {
|
type withModeOption struct {
|
||||||
mode Mode
|
mode Mode
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare 实现了 BotLoginOption 接口
|
// Prepare 实现了 BotLoginOption 接口
|
||||||
func (w WithModeOption) Prepare(b *Bot) { b.Caller.Client.SetMode(w.mode) }
|
func (w withModeOption) Prepare(b *Bot) { b.Caller.Client.SetMode(w.mode) }
|
||||||
|
|
||||||
func withMode(mode Mode) BotPreparer {
|
func withMode(mode Mode) BotPreparer {
|
||||||
return WithModeOption{mode: mode}
|
return withModeOption{mode: mode}
|
||||||
}
|
}
|
||||||
|
|
||||||
// btw, 这两个变量已经变了4回了, 但是为了兼容以前的代码, 还是得想着法儿让用户无感知的更新
|
// btw, 这两个变量已经变了4回了, 但是为了兼容以前的代码, 还是得想着法儿让用户无感知的更新
|
||||||
@ -153,6 +154,19 @@ var (
|
|||||||
Desktop = withMode(desktop)
|
Desktop = withMode(desktop)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WithContextOption 指定一个 context.Context 用于Bot的生命周期
|
||||||
|
type WithContextOption struct {
|
||||||
|
Ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare 实现了 BotLoginOption 接口
|
||||||
|
func (w WithContextOption) Prepare(b *Bot) {
|
||||||
|
if w.Ctx == nil {
|
||||||
|
panic("context is nil")
|
||||||
|
}
|
||||||
|
b.context, b.cancel = context.WithCancel(w.Ctx)
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultHotStorageSyncDuration = time.Minute * 5
|
defaultHotStorageSyncDuration = time.Minute * 5
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user