Watch starts watching ctx. If ctx is canceled then the onCancel function passed to NewContextWatcher will be called.
(ctx context.Context)
| 30 | |
| 31 | // Watch starts watching ctx. If ctx is canceled then the onCancel function passed to NewContextWatcher will be called. |
| 32 | func (cw *ContextWatcher) Watch(ctx context.Context) { |
| 33 | cw.lock.Lock() |
| 34 | defer cw.lock.Unlock() |
| 35 | |
| 36 | if cw.stop != nil { |
| 37 | panic("watch already in progress") |
| 38 | } |
| 39 | |
| 40 | if ctx.Done() != nil { |
| 41 | cw.done = make(chan struct{}) |
| 42 | cw.stop = context.AfterFunc(ctx, func() { |
| 43 | cw.handler.HandleCancel(ctx) |
| 44 | close(cw.done) |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Unwatch stops watching the previously watched context. If the onCancel function passed to NewContextWatcher was |
| 50 | // called then onUnwatchAfterCancel will also be called. |