dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
157 stars 44 forks source link

[ffigen] Support blocking callbacks #1647

Open liamappelbe opened 1 month ago

liamappelbe commented 1 month ago

Turns out there are a few Apple APIs that require that the completion handler is invoked during the callback. So we should add a variant of listener blocks and protocol methods that uses a mutex to block until the listener callback is complete.

With a little more infrastructure, it would also be possible to support callbacks that return results, but these are even rarer in Apple APIs, so we'll wait until we have a use case for this.

johnmccutchan commented 1 month ago

@liamappelbe Can you point me at an example of this type of API?

liamappelbe commented 1 month ago

The one Brian ran into in cupertino_http was URLSession:downloadTask:didFinishDownloadingToURL:. It's called after a download to a temporary file is complete. The temporary file will be deleted after the callback returns. So if you want to do something with the file, your callback has to be blocking.

@brianquinlan What were the other examples you found that were using the completion handler pattern, but expect the completion handler to be called during the callback?

stuartmorgan commented 1 month ago

With a little more infrastructure, it would also be possible to support callbacks that return results, but these are even rarer in Apple APIs, so we'll wait until we have a use case for this.

We're auto-converting all delegate protocols to callbacks still, right? If so, this is not going to be rare. Having a delegate to provide data or synchronous decisions is fairly common pattern.

brianquinlan commented 1 month ago

@brianquinlan What were the other examples you found that were using the completion handler pattern, but expect the completion handler to be called during the callback?

I didn't look very hard for examples.