Context returns a context that is a copy of the provided parent context with a replaced Done channel that is closed when either the tomb is dying or the parent is cancelled. If parent is nil, it defaults to the parent provided via WithContext, or an empty background parent if the tomb wasn't create
(parent context.Context)
| 33 | // If parent is nil, it defaults to the parent provided via WithContext, or an |
| 34 | // empty background parent if the tomb wasn't created via WithContext. |
| 35 | func (t *Tomb) Context(parent context.Context) context.Context { |
| 36 | t.init() |
| 37 | t.m.Lock() |
| 38 | defer t.m.Unlock() |
| 39 | |
| 40 | if parent == nil { |
| 41 | panic("parent context is empty") |
| 42 | } |
| 43 | |
| 44 | if child, ok := t.child[parent]; ok { |
| 45 | return child.context.(context.Context) |
| 46 | } |
| 47 | |
| 48 | child, cancel := context.WithCancel(parent) |
| 49 | t.addChild(parent, child, cancel) |
| 50 | return child |
| 51 | } |
| 52 | |
| 53 | func (t *Tomb) addChild(parent context.Context, child context.Context, cancel func()) { |
| 54 | if t.reason != ErrStillAlive { |
no test coverage detected