capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

feat: promise with callback plugin method

Open matthiasschwarz opened this issue 3 years ago • 0 comments

Description

Fixes #5618 Allows to add a callback function to a plugin method which returns a promise. Calling call.resolve() on keep alive passes the object to the callback - when a callback was provided. Setting keep alive to false passes the object to the promise and resolves it. call.reject() rejects the promise and deletes the saved callback.

This should not be a breaking change to the value return method type when no callback function is provided to the method or keep alive is never set to true.

Android example usage

@CapacitorPlugin(name = "Example")
public class ExamplePlugin extends Plugin {
    @PluginMethod()
    public void example(PluginCall call) {
        String v = call.getString("v");
        call.setKeepAlive(true);
        call.resolve(new JSObject().put("n", 1));
        call.resolve(new JSObject().put("n", 2));
        call.resolve(new JSObject().put("n", 3));
        call.setKeepAlive(false);
        call.resolve(new JSObject().put("i", 42).put("r", v));
    }
}

Platform(s)

  • Android
  • iOS (untested)

matthiasschwarz avatar May 16 '22 14:05 matthiasschwarz