github.com/haraldrudell/parl@v0.4.176/on-cancel.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parl
     7  
     8  import "context"
     9  
    10  // OnCancel invokes fn when work done on behalf of context ctx
    11  // should be canceled
    12  func OnCancel(fn func(), ctx context.Context) {
    13  	go onCancelThread(fn, ctx.Done())
    14  }
    15  
    16  func onCancelThread(fn func(), done <-chan struct{}) {
    17  	Recover(func() DA { return A() }, nil, Infallible)
    18  	<-done
    19  	fn()
    20  }