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 809 B

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