Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: promise with callback plugin method #5622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions android/capacitor/src/main/assets/native-bridge.js
Expand Up @@ -405,7 +405,23 @@ const nativeBridge = (function (exports) {
return err;
}, new cap.Exception(''));
}
if (typeof storedCall.callback === 'function') {
if (typeof storedCall.callback === 'function' &&
typeof storedCall.resolve === 'function') {
// promise with callback
if (result.success) {
if (result.save) {
storedCall.callback(result.data);
}
else {
storedCall.resolve(result.data);
}
}
else {
storedCall.reject(null, result.error);
callbacks.delete(result.callbackId);
}
}
else if (typeof storedCall.callback === 'function') {
// callback
if (result.success) {
storedCall.callback(result.data);
Expand Down Expand Up @@ -451,9 +467,10 @@ const nativeBridge = (function (exports) {
}
return cap.toNative(pluginName, methodName, options, { callback });
};
cap.nativePromise = (pluginName, methodName, options) => {
cap.nativePromise = (pluginName, methodName, options, callback) => {
return new Promise((resolve, reject) => {
cap.toNative(pluginName, methodName, options, {
callback: callback,
resolve: resolve,
reject: reject,
});
Expand Down
12 changes: 10 additions & 2 deletions android/capacitor/src/main/java/com/getcapacitor/JSExport.java
Expand Up @@ -145,7 +145,7 @@ private static String generateMethodJS(PluginHandle plugin, PluginMethodHandle m
args.add(CATCHALL_OPTIONS_PARAM);

String returnType = method.getReturnType();
if (returnType.equals(PluginMethod.RETURN_CALLBACK)) {
if (!returnType.equals(PluginMethod.RETURN_NONE)) {
args.add(CALLBACK_PARAM);
}

Expand All @@ -166,7 +166,15 @@ private static String generateMethodJS(PluginHandle plugin, PluginMethodHandle m
break;
case PluginMethod.RETURN_PROMISE:
lines.add(
"return w.Capacitor.nativePromise('" + plugin.getId() + "', '" + method.getName() + "', " + CATCHALL_OPTIONS_PARAM + ")"
"return w.Capacitor.nativePromise('" +
plugin.getId() +
"', '" +
method.getName() +
"', " +
CATCHALL_OPTIONS_PARAM +
", " +
CALLBACK_PARAM +
")"
);
break;
case PluginMethod.RETURN_CALLBACK:
Expand Down
20 changes: 18 additions & 2 deletions core/native-bridge.ts
Expand Up @@ -481,7 +481,22 @@ const initBridge = (w: any): void => {
}, new cap.Exception(''));
}

if (typeof storedCall.callback === 'function') {
if (
typeof storedCall.callback === 'function' &&
typeof storedCall.resolve === 'function'
) {
// promise with callback
if (result.success) {
if (result.save) {
storedCall.callback(result.data);
} else {
storedCall.resolve(result.data);
}
} else {
storedCall.reject(null, result.error);
callbacks.delete(result.callbackId);
}
} else if (typeof storedCall.callback === 'function') {
// callback
if (result.success) {
storedCall.callback(result.data);
Expand Down Expand Up @@ -531,9 +546,10 @@ const initBridge = (w: any): void => {
return cap.toNative(pluginName, methodName, options, { callback });
};

cap.nativePromise = (pluginName, methodName, options) => {
cap.nativePromise = (pluginName, methodName, options, callback) => {
return new Promise((resolve, reject) => {
cap.toNative(pluginName, methodName, options, {
callback: callback,
resolve: resolve,
reject: reject,
});
Expand Down
3 changes: 2 additions & 1 deletion core/src/definitions-internal.ts
@@ -1,4 +1,4 @@
import type {
import type {
CapacitorGlobal,
PluginCallback,
PluginResultData,
Expand Down Expand Up @@ -74,6 +74,7 @@ export interface CapacitorInstance extends CapacitorGlobal {
pluginName: string,
methodName: string,
options?: O,
callback?: PluginCallback,
) => Promise<R>;

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/runtime.ts
Expand Up @@ -121,8 +121,8 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => {
const methodHeader = pluginHeader?.methods.find(m => prop === m.name);
if (methodHeader) {
if (methodHeader.rtype === 'promise') {
return (options: any) =>
cap.nativePromise(pluginName, prop.toString(), options);
return (options: any, callback: any) =>
cap.nativePromise(pluginName, prop.toString(), options, callback);
} else {
return (options: any, callback: any) =>
cap.nativeCallback(
Expand Down
4 changes: 2 additions & 2 deletions ios/Capacitor/Capacitor/JSExport.swift
Expand Up @@ -126,7 +126,7 @@ internal class JSExport {
paramList.append(catchallOptionsParameter)

// Automatically add the _callback param if returning data through a callback
if returnType == CAPPluginReturnCallback {
if returnType != CAPPluginReturnNone {
paramList.append(callbackParameter)
}

Expand All @@ -151,7 +151,7 @@ internal class JSExport {

// ...using a promise
lines.append("""
return w.Capacitor.nativePromise('\(pluginClassName)', '\(methodName)', \(argObjectString));
return w.Capacitor.nativePromise('\(pluginClassName)', '\(methodName)', \(argObjectString), \(callbackParameter));
""")
} else if returnType == CAPPluginReturnCallback {
// ...using a callback
Expand Down
21 changes: 19 additions & 2 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Expand Up @@ -405,7 +405,23 @@ const nativeBridge = (function (exports) {
return err;
}, new cap.Exception(''));
}
if (typeof storedCall.callback === 'function') {
if (typeof storedCall.callback === 'function' &&
typeof storedCall.resolve === 'function') {
// promise with callback
if (result.success) {
if (result.save) {
storedCall.callback(result.data);
}
else {
storedCall.resolve(result.data);
}
}
else {
storedCall.reject(null, result.error);
callbacks.delete(result.callbackId);
}
}
else if (typeof storedCall.callback === 'function') {
// callback
if (result.success) {
storedCall.callback(result.data);
Expand Down Expand Up @@ -451,9 +467,10 @@ const nativeBridge = (function (exports) {
}
return cap.toNative(pluginName, methodName, options, { callback });
};
cap.nativePromise = (pluginName, methodName, options) => {
cap.nativePromise = (pluginName, methodName, options, callback) => {
return new Promise((resolve, reject) => {
cap.toNative(pluginName, methodName, options, {
callback: callback,
resolve: resolve,
reject: reject,
});
Expand Down