New returns a Notifier that calls cond once every time it polls. - Duplicate values are removed from countdown, and it is sorted in descending order.
(cond Condition, interval time.Duration, countdown []time.Duration, opts ...Option)
| 53 | // - Duplicate values are removed from countdown, and it is sorted in |
| 54 | // descending order. |
| 55 | func New(cond Condition, interval time.Duration, countdown []time.Duration, opts ...Option) *Notifier { |
| 56 | // Ensure countdown is sorted in descending order and contains no duplicates. |
| 57 | ct := unique(countdown) |
| 58 | sort.Slice(ct, func(i, j int) bool { |
| 59 | return ct[i] < ct[j] |
| 60 | }) |
| 61 | |
| 62 | ctx, cancel := context.WithCancel(context.Background()) |
| 63 | n := &Notifier{ |
| 64 | ctx: ctx, |
| 65 | cancel: cancel, |
| 66 | pollDone: make(chan struct{}), |
| 67 | countdown: ct, |
| 68 | condition: cond, |
| 69 | notifiedAt: make(map[time.Duration]bool), |
| 70 | clock: quartz.NewReal(), |
| 71 | } |
| 72 | for _, opt := range opts { |
| 73 | opt(n) |
| 74 | } |
| 75 | go n.poll(interval) |
| 76 | |
| 77 | return n |
| 78 | } |
| 79 | |
| 80 | // poll polls once immediately, and then periodically according to the interval. |
| 81 | // Poll exits when ticker is closed. |