Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Refactor delay timer into wait
Browse files Browse the repository at this point in the history
Resolves: #326 (comment)
  • Loading branch information
ankur22 committed Aug 2, 2022
1 parent 8b68487 commit 20908e3
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,9 @@ func (k *Keyboard) modifierBitFromKeyName(key string) int64 {
}

func (k *Keyboard) comboPress(keys string, opts *KeyboardOptions) error {
if opts.Delay != 0 {
t := time.NewTimer(time.Duration(opts.Delay) * time.Millisecond)
select {
case <-k.ctx.Done():
if !t.Stop() {
<-t.C
}
return context.Canceled
case <-t.C:
if opts.Delay > 0 {
if err := wait(k.ctx, opts.Delay); err != nil {
return err
}
}

Expand Down Expand Up @@ -312,12 +306,9 @@ func split(keys string) []string {
}

func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
if opts.Delay != 0 {
t := time.NewTimer(time.Duration(opts.Delay) * time.Millisecond)
select {
case <-k.ctx.Done():
t.Stop()
case <-t.C:
if opts.Delay > 0 {
if err := wait(k.ctx, opts.Delay); err != nil {
return err
}
}
if err := k.down(key); err != nil {
Expand All @@ -329,12 +320,9 @@ func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
for _, c := range text {
if opts.Delay != 0 {
t := time.NewTimer(time.Duration(opts.Delay) * time.Millisecond)
select {
case <-k.ctx.Done():
t.Stop()
case <-t.C:
if opts.Delay > 0 {
if err := wait(k.ctx, opts.Delay); err != nil {
return err
}
}
keyInput := keyboardlayout.KeyInput(c)
Expand All @@ -350,3 +338,17 @@ func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
}
return nil
}

func wait(ctx context.Context, delay int64) error {

This comment has been minimized.

Copy link
@imiric

imiric Aug 2, 2022

Contributor

This function is generic enough to go in common/helpers.go.

Ugh, made the comment on the commit. And now can't delete it for some reason... 🤦

t := time.NewTimer(time.Duration(delay) * time.Millisecond)
select {
case <-ctx.Done():
if !t.Stop() {
<-t.C
}
return ctx.Err()
case <-t.C:
}

return nil
}

0 comments on commit 20908e3

Please sign in to comment.