jhugman / uniffi-bindgen-react-native

A uniffi bindings generator for calling Rust from react-native
Other
0 stars 0 forks source link

CancelablePromises: Add task cancellation to Typescript to request cancellation in Rust #73

Open jhugman opened 3 weeks ago

jhugman commented 3 weeks ago

Speaking to @zzorba we have decided that requesting cancellation from JS should be done by adding requestCancellation or cancel to each promise returned.

async performTask(): Promise<string>

becomes:

async performTask(): CancelablePromise<string>

where CancelablePromise is:

type CancelablePromise<T> = Promise<T> & { cancel: () => void };

In the event of a cancel method being called:

a) an Rust call to the cancellation request method is called. b) if Rust respects that request, the promise is rejected with a cancellation error. c) If Rust does not respect the request, and continues until completion, then the promise is resolved as usual.