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(android): Unifying saving plugin calls #4254

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public void callPluginMethod(String pluginId, final String methodName, final Plu
try {
plugin.invoke(methodName, call);

if (call.isSaved()) {
if (call.isKeptAlive()) {
saveCall(call);
}
} catch (PluginLoadException | InvalidPluginMethodException ex) {
Expand Down Expand Up @@ -646,10 +646,18 @@ PluginCall getPluginCallForLastActivity() {

/**
* Release a retained call
* @param call
* @param call a call to release
*/
public void releaseCall(PluginCall call) {
this.savedCalls.remove(call.getCallbackId());
releaseCall(call.getCallbackId());
}

/**
* Release a retained call by its ID
* @param callbackId an ID of a callback to release
*/
public void releaseCall(String callbackId) {
this.savedCalls.remove(callbackId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void postMessage(String jsonStr) {
public void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult) {
try {
PluginResult data = new PluginResult();
data.put("save", call.isSaved());
data.put("save", call.isKeptAlive());
data.put("callbackId", call.getCallbackId());
data.put("pluginId", call.getPluginId());
data.put("methodName", call.getMethodName());
Expand Down
10 changes: 4 additions & 6 deletions android/capacitor/src/main/java/com/getcapacitor/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void triggerPermissionCallback(Method method, Map<String, Boolean> permi
e.printStackTrace();
}

if (!savedCall.isReleased() && !savedCall.isSaved()) {
if (!savedCall.isKeptAlive()) {
savedCall.release(bridge);
}
}
Expand All @@ -167,7 +167,7 @@ private void triggerActivityCallback(Method method, ActivityResult result) {
e.printStackTrace();
}

if (!savedCall.isReleased() && !savedCall.isSaved()) {
if (!savedCall.isKeptAlive()) {
savedCall.release(bridge);
}
}
Expand Down Expand Up @@ -285,9 +285,7 @@ public void saveCall(PluginCall lastCall) {
*/
@Deprecated
public void freeSavedCall() {
if (!this.savedLastCall.isReleased()) {
this.savedLastCall.release(bridge);
}
this.savedLastCall.release(bridge);
this.savedLastCall = null;
}

Expand Down Expand Up @@ -735,7 +733,7 @@ private void sendRetainedArgumentsForEvent(String eventName) {
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
public void addListener(PluginCall call) {
String eventName = call.getString("eventName");
call.save();
call.setKeepAlive(true);
addEventListener(eventName, call);
}

Expand Down
36 changes: 32 additions & 4 deletions android/capacitor/src/main/java/com/getcapacitor/PluginCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public class PluginCall {
private final String methodName;
private final JSObject data;

private boolean shouldSave = false;
private boolean keepAlive = false;

/**
* Indicates that this PluginCall was released, and should no longer be used
*/
@Deprecated
private boolean isReleased = false;

public PluginCall(MessageHandler msgHandler, String pluginId, String callbackId, String methodName, JSObject data) {
Expand Down Expand Up @@ -340,21 +341,48 @@ public boolean hasOption(String name) {
* Indicate that the Bridge should cache this call in order to call
* it again later. For example, the addListener system uses this to
* continuously call the call's callback (😆).
* @deprecated use {@link #setKeepAlive(Boolean)} instead
*/
@Deprecated
public void save() {
this.shouldSave = true;
setKeepAlive(true);
}

/**
* Indicate that the Bridge should cache this call in order to call
* it again later. For example, the addListener system uses this to
* continuously call the call's callback.
*
* @param keepAlive whether to keep the callback saved
*/
public void setKeepAlive(Boolean keepAlive) {
this.keepAlive = keepAlive;
}

public void release(Bridge bridge) {
this.shouldSave = false;
this.keepAlive = false;
bridge.releaseCall(this);
this.isReleased = true;
}

/**
* @deprecated use {@link #isKeptAlive()}
* @return true if the plugin call is kept alive
*/
@Deprecated
public boolean isSaved() {
return shouldSave;
return isKeptAlive();
}

/**
* Gets the keepAlive value of the plugin call
* @return true if the plugin call is kept alive
*/
public boolean isKeptAlive() {
return keepAlive;
}

@Deprecated
public boolean isReleased() {
return isReleased;
}
Expand Down