Return-Path: X-Original-To: apmail-cordova-issues-archive@minotaur.apache.org Delivered-To: apmail-cordova-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4655AF852 for ; Tue, 9 Apr 2013 18:42:17 +0000 (UTC) Received: (qmail 76408 invoked by uid 500); 9 Apr 2013 18:42:17 -0000 Delivered-To: apmail-cordova-issues-archive@cordova.apache.org Received: (qmail 76391 invoked by uid 500); 9 Apr 2013 18:42:17 -0000 Mailing-List: contact issues-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list issues@cordova.apache.org Received: (qmail 76319 invoked by uid 99); 9 Apr 2013 18:42:17 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Apr 2013 18:42:17 +0000 Date: Tue, 9 Apr 2013 18:42:17 +0000 (UTC) From: "Joe Bowser (JIRA)" To: issues@cordova.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (CB-2963) Javascript callback functions are not executed in the correct order MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/CB-2963?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13626931#comment-13626931 ] Joe Bowser commented on CB-2963: -------------------------------- [~davide] Do you have a test case that we can use to check this behaviour? The setTimeout call looks super hacky and I'm not sure this would work across all devices. > Javascript callback functions are not executed in the correct order > ------------------------------------------------------------------- > > Key: CB-2963 > URL: https://issues.apache.org/jira/browse/CB-2963 > Project: Apache Cordova > Issue Type: Bug > Components: Android > Affects Versions: 2.5.0 > Reporter: Davide Maestroni > Assignee: Joe Bowser > Priority: Critical > > I created an application which sends asynchronous notifications from the native to the Javascript side. I noticed that in some cases the Javascript code of the callbacks is not executed in the expected order. > Let me show an example: > # a native event trigger a notification to the Javascript side where a callback function (callback1 - part 1) gets correctly called > # in the while, on the native side, a different notification is triggered > # inside the Javascript code of the first callback the cordova.exec() API is issued > # at this point the Javascript code of the second callback (callback2) is executed sequentially on the same thread > # when the second callback completes, the final part of the code in the first callback (callback1 - part 2) is executed > So that, if the callback functions are defined as below: > {noformat} > function callback1() { > > cordova.exec(...); > > } > function callback2() { > > } > {noformat} > the Javascript code gets executed in the following order: > {noformat} > > cordova.exec(...); > > > {noformat} > which is not the expected behavior. > I believe the problem is related to the following lines of code in the _cordova.android.js_ file: > {noformat} > var messages = nativeApiProvider.get().exec(service, action, callbackId, argsJson); > androidExec.processMessages(messages); > {noformat} > which get called if _jsToNativeBridgeMode == jsToNativeModes.JS_OBJECT_. > That causes enqueued messages to get executed sequentially before the execution of the first callback is complete. > An easy fix would be to change the above code into: > {noformat} > var messages = nativeApiProvider.get().exec(service, action, callbackId, argsJson); > setTimeout(function() { > androidExec.processMessages(messages); > }, 0); > {noformat} > so that the enqueued messages are processed in the next Javascript loop. -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira