You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

set_void_future.go 818 B

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package future
  2. import (
  3. "context"
  4. "sync"
  5. )
  6. type SetVoidFuture struct {
  7. isCompleted bool
  8. ch chan error
  9. completeOnce sync.Once
  10. }
  11. func NewSetVoid() *SetVoidFuture {
  12. return &SetVoidFuture{
  13. ch: make(chan error, 1),
  14. }
  15. }
  16. func (f *SetVoidFuture) SetVoid() {
  17. f.completeOnce.Do(func() {
  18. f.ch <- nil
  19. close(f.ch)
  20. f.isCompleted = true
  21. })
  22. }
  23. func (f *SetVoidFuture) SetError(err error) {
  24. f.completeOnce.Do(func() {
  25. f.ch <- err
  26. close(f.ch)
  27. f.isCompleted = true
  28. })
  29. }
  30. func (f *SetVoidFuture) IsComplete() bool {
  31. return f.isCompleted
  32. }
  33. func (f *SetVoidFuture) Wait(ctx context.Context) error {
  34. select {
  35. case v, ok := <-f.ch:
  36. if !ok {
  37. return ErrCompleted
  38. }
  39. return v
  40. case <-ctx.Done():
  41. return ErrContextCancelled
  42. }
  43. }
  44. func (f *SetVoidFuture) Chan() <-chan error {
  45. return f.ch
  46. }