Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
According to The Big O of Code Reviews, this is a O(n) change.
Fixes #73
Fixes #74
Background
Task cancellation is cooperative in both Rust and Javascript. i.e. they rely on cooperation of the task itself to perform cancellation.
In Javascript, there is API for the canceller: the
AbortController
. TheAbortController
has anAbortSignal
object which can be given to the task.For example:
This might be used like so:
Cancelling Rust tasks
Uniffi's machinery provides a
cancelFunc
. As ofv0.28.0
, this causes theFuture
to be dropped. The Rust should be written in such a way as to do any cleanup for the task when this happens.This
cancelFunc
can be called indirectly by passing an{ signal: AbortSignal; }
when calling any async function.There is no way for uniffi to know which Futures can be cancelled, so all async functions have an optional argument of
asyncOpts_?: { signal: AbortSignal; }
appended to their argument list.Thus:
may also be called with an
AbortSignal
.Since these are optional arguments, it is up to the Typescript caller whether or not to include them.
Cancelling async Javascript callbacks
The
futures_util
crate provides structures similar toAbortController
andAbortSignal
. In this example,obj
is a JS callback interface.The
obj.delay(delay_ms)
call translates to a call to a Javascript function.When
abort_handle.abort()
is called, the Abortablefuture
is dropped. TheAbortSignal
in Javascript is told to abort when it is being cleaned up, and hasn't yet settled.Because uniffi can't tell which Javascript callbacks support an
AbortSignal
, all async functions have an optional argument ofasyncOpts_?: { signal: AbortSignal; }
appended to their argument list.Since these are optional arguments, it is up to the Typescript implementer whether or not to include them.
Caveat emptor
Because of the different APIs across languages and the co-operative nature of task cancellation in Rust, there is a diversity of API support for task cancellation across the various backend languages that uniffi supports. This PR brings uniffi-bindgen-react-native to parity with the Mozilla supported languages.
However, the uniffi docs currently suggest more modest support:
I would expect this to change over time.