WithCancel sets up cancellation in a [TContext.Cleanup] callback and constructs a new TContext where [TContext.Cancel] cancels only the new context.
()
| 27 | // constructs a new TContext where [TContext.Cancel] cancels only the new |
| 28 | // context. |
| 29 | func (tCtx TContext) WithCancel() TContext { |
| 30 | ctx, cancel := context.WithCancelCause(tCtx) |
| 31 | |
| 32 | tCtx.Context = ctx |
| 33 | tCtx.cancel = func(cause string) { |
| 34 | var cancelCause error |
| 35 | if cause != "" { |
| 36 | cancelCause = canceledError(cause) |
| 37 | } |
| 38 | cancel(cancelCause) |
| 39 | } |
| 40 | return tCtx |
| 41 | } |
| 42 | |
| 43 | // WithoutCancel causes the returned context to ignore cancellation of its parent. |
| 44 | // Calling Cancel will only cancel the new context. |