WithContext returns a new tomb that is killed when the provided parent context is canceled, and a copy of parent with a replaced Done channel that is closed when either the tomb is dying or the parent is canceled. The returned context may also be obtained via the tomb's Context method.
(parent context.Context)
| 9 | // that is closed when either the tomb is dying or the parent is canceled. |
| 10 | // The returned context may also be obtained via the tomb's Context method. |
| 11 | func WithContext(parent context.Context) (*Tomb, context.Context) { |
| 12 | var t Tomb |
| 13 | t.init() |
| 14 | if parent.Done() != nil { |
| 15 | go func() { |
| 16 | select { |
| 17 | case <-t.Dying(): |
| 18 | case <-parent.Done(): |
| 19 | t.Kill(parent.Err()) |
| 20 | } |
| 21 | }() |
| 22 | } |
| 23 | t.parent = parent |
| 24 | child, cancel := context.WithCancel(parent) |
| 25 | t.addChild(parent, child, cancel) |
| 26 | return &t, child |
| 27 | } |
| 28 | |
| 29 | // Context returns a context that is a copy of the provided parent context with |
| 30 | // a replaced Done channel that is closed when either the tomb is dying or the |