From b756a1f248605a946ff7068d22dc49b10dd21d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Mainu=C5=A1?= Date: Fri, 5 Jun 2015 12:22:53 +0200 Subject: [PATCH] Javascript callback not call Android callback #9 Fixed bug when javascript callback didnt call Android callback Patch: Callback wraper is now not registered as WeakReference. Javascript Callback wrapper has optional attribute keepAlive that mean: "Keep this callback because javascript can call him again". Default behaviour (Standard callback) is remove callback after use. --- .../com/flipboard/goldengate/BridgeInterface.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/compiler/src/main/java/com/flipboard/goldengate/BridgeInterface.java b/compiler/src/main/java/com/flipboard/goldengate/BridgeInterface.java index 679d5dc..32bb5fd 100644 --- a/compiler/src/main/java/com/flipboard/goldengate/BridgeInterface.java +++ b/compiler/src/main/java/com/flipboard/goldengate/BridgeInterface.java @@ -62,7 +62,7 @@ public void writeToFiler(Filer filer) throws IOException { .addField(ClassName.get(packageName, name + "Bridge", "ResultBridge"), "resultBridge", Modifier.PRIVATE) .addField(AtomicLong.class, "receiverIds", Modifier.PRIVATE); - Type callbacksMapType = new TypeToken>>>(){}.getType(); + Type callbacksMapType = new TypeToken>>(){}.getType(); Type callbackType = new TypeToken>(){}.getType(); // Generate the result bridge @@ -74,7 +74,7 @@ public void writeToFiler(Filer filer) throws IOException { .addMethod(MethodSpec.methodBuilder("registerCallback") .addParameter(long.class, "receiver") .addParameter(callbackType, "cb") - .addCode(CodeBlock.builder().addStatement("callbacks.put($N, new $T($N))", "receiver", WeakReference.class, "cb").build()) + .addCode(CodeBlock.builder().addStatement("callbacks.put($N, $N)", "receiver", "cb").build()) .build()) .addMethod(MethodSpec.methodBuilder("onResult") .addModifiers(Modifier.PUBLIC) @@ -85,9 +85,16 @@ public void writeToFiler(Filer filer) throws IOException { .addStatement("$T $N = new $T($N)", ClassName.get("org.json", "JSONObject"), "json", ClassName.get("org.json", "JSONObject"), "result") .addStatement("$T $N = $N.getLong($S)", long.class, "receiver", "json", "receiver") .addStatement("$T $N = $N.get($S).toString()", String.class, "realResult", "json", "result") - .addStatement("$T $N = $N.get($N).get()", callbackType, "callback", "callbacks", "receiver") + .addStatement("$T $N = $N.get($N)", callbackType, "callback", "callbacks", "receiver") .beginControlFlow("if ($N != null) ", "callback") .addStatement("$N.onResult($N)", "callback", "realResult") + .addStatement("$T $N = false", boolean.class, "keepAlive") + .beginControlFlow("if ($N.has($S))", "json", "keepAlive") + .addStatement("$N = $N.getBoolean($S)", "keepAlive", "json", "keepAlive") + .endControlFlow() + .beginControlFlow("if (!$N)", "keepAlive") + .addStatement("$N.remove($N)", "callbacks", "receiver") + .endControlFlow() .endControlFlow() .nextControlFlow("catch (org.json.JSONException e)") .addStatement("$N.printStackTrace()", "e") @@ -127,7 +134,7 @@ public void writeToFiler(Filer filer) throws IOException { .addCode("evaluateJavascript(\n" + " \"function GoldenGate$$$$CreateCallback(receiver) {\" +\n" + " \" return function(result) {\" +\n" + - " \" $N.onResult(JSON.stringify({receiver: receiver, result: JSON.stringify(result)}))\" +\n" + + " \" $N.onResult(JSON.stringify({receiver: receiver, keepAlive: true, result: JSON.stringify(result)}))\" +\n" + " \" }\" +\n" + " \"}\");", name) .build()