File: update_waiter.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (48 lines) | stat: -rw-r--r-- 867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package imap

import (
	"context"
)

type Waiter interface {
	// Wait waits until the update has been marked as done.
	Wait() (error, bool)

	// WaitContext waits until the update has been marked as done or the context is cancelled.
	WaitContext(context.Context) (error, bool)

	// Done marks the update as done and report an error (if any).
	Done(error)
}

type updateWaiter struct {
	waitCh chan error
}

func newUpdateWaiter() *updateWaiter {
	return &updateWaiter{
		waitCh: make(chan error, 1),
	}
}

func (w *updateWaiter) Wait() (error, bool) {
	err, ok := <-w.waitCh
	return err, ok
}

func (w *updateWaiter) WaitContext(ctx context.Context) (error, bool) {
	select {
	case <-ctx.Done():
		return nil, false
	case err, ok := <-w.waitCh:
		return err, ok
	}
}

func (w *updateWaiter) Done(err error) {
	if err != nil {
		w.waitCh <- err
	}

	close(w.waitCh)
}