mirror of
https://github.com/xaionaro-go/streamctl.git
synced 2025-09-27 03:45:52 +08:00
Split k.NewChatHandlerOBSOLETE
This commit is contained in:
@@ -60,46 +60,48 @@ func NewChatHandlerOBSOLETE(
|
|||||||
|
|
||||||
ctx, cancelFn := context.WithCancel(ctx)
|
ctx, cancelFn := context.WithCancel(ctx)
|
||||||
h := &ChatHandlerOBSOLETE{
|
h := &ChatHandlerOBSOLETE{
|
||||||
currentCursor: 0,
|
currentCursor: 0,
|
||||||
client: chatClient,
|
client: chatClient,
|
||||||
channelID: channelID,
|
channelID: channelID,
|
||||||
cancelFunc: cancelFn,
|
cancelFunc: cancelFn,
|
||||||
messagesOutChan: make(chan streamcontrol.ChatMessage, 100),
|
onClose: onClose,
|
||||||
onClose: onClose,
|
}
|
||||||
|
h.init(ctx)
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ChatHandlerOBSOLETE) init(ctx context.Context) {
|
||||||
|
h.messagesOutChan = make(chan streamcontrol.ChatMessage, 100)
|
||||||
|
observability.Go(ctx, func(ctx context.Context) {
|
||||||
|
if h.onClose != nil {
|
||||||
|
defer h.onClose(ctx, h)
|
||||||
|
}
|
||||||
|
defer close(h.messagesOutChan)
|
||||||
|
h.loop(ctx)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ChatHandlerOBSOLETE) loop(ctx context.Context) {
|
||||||
|
err := h.iterate(ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf(ctx, "unable to perform an iteration: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
observability.Go(ctx, func(ctx context.Context) {
|
t := time.NewTicker(time.Second)
|
||||||
if onClose != nil {
|
defer t.Stop()
|
||||||
defer func() {
|
for {
|
||||||
onClose(ctx, h)
|
select {
|
||||||
}()
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-t.C:
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
close(h.messagesOutChan)
|
|
||||||
}()
|
|
||||||
err := h.iterate(ctx)
|
err := h.iterate(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf(ctx, "unable to perform an iteration: %v", err)
|
logger.Errorf(ctx, "unable to perform an iteration: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
t := time.NewTicker(time.Second)
|
|
||||||
defer t.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-t.C:
|
|
||||||
}
|
|
||||||
err := h.iterate(ctx)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf(ctx, "unable to perform an iteration: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return h, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ChatHandlerOBSOLETE) iterate(ctx context.Context) error {
|
func (h *ChatHandlerOBSOLETE) iterate(ctx context.Context) error {
|
||||||
|
Reference in New Issue
Block a user