mirror of
https://github.com/harshabose/socket-comm.git
synced 2025-10-06 08:06:59 +08:00
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package process
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/harshabose/socket-comm/pkg/middleware/chat/errors"
|
|
"github.com/harshabose/socket-comm/pkg/middleware/chat/interfaces"
|
|
"github.com/harshabose/socket-comm/pkg/middleware/chat/state"
|
|
)
|
|
|
|
type IdentWaiterOptions func(*IdentWaiter)
|
|
|
|
func NewIdentWaiter(ctx context.Context, options ...IdentWaiterOptions) *IdentWaiter {
|
|
p := &IdentWaiter{
|
|
ctx: ctx,
|
|
duration: 500 * time.Millisecond,
|
|
}
|
|
|
|
for _, option := range options {
|
|
option(p)
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
type IdentWaiter struct {
|
|
ctx context.Context
|
|
duration time.Duration
|
|
}
|
|
|
|
func (p *IdentWaiter) Process(_ interfaces.Processor, s *state.State) error {
|
|
ticker := time.NewTicker(p.duration)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-p.ctx.Done():
|
|
return errors.ErrContextCancelled
|
|
case <-ticker.C:
|
|
if err := p.process(nil, s); err == nil {
|
|
return nil
|
|
}
|
|
fmt.Println("waiting for ident...")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *IdentWaiter) process(_ interfaces.Processor, s *state.State) error {
|
|
_, err := s.GetClientID()
|
|
return err
|
|
}
|