[Account] Fixes for addAccountListener() and removeAccountListener().
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Thu, 12 Feb 2015 10:40:22 +0000 (11:40 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 13 Feb 2015 12:01:22 +0000 (21:01 +0900)
Change-Id: I8b6f4d9c5901605040c14937da5d722d626f9761

src/account/account_api.js
src/account/account_instance.cc

index f2bf1124c32d129fd279eb9671fde5d4e9e67d19..712a952a9fc6625d54e661b36b23e157227d0fd4 100644 (file)
@@ -340,13 +340,23 @@ function AccountListeners() {
 
 
 AccountListeners.prototype.instances = {};
+AccountListeners.prototype.nextID = 0;
 
 
-AccountListeners.prototype.addListener = function(accountListenerId, callback) {
+AccountListeners.prototype.addListener = function(callback) {
+    var id = ++this.nextID;
+
     if (T_.isEmptyObject(this.instances)) {
+        var result = native_.callSync('AccountManager_addAccountListener');
+        if (native_.isFailure(result)) {
+            throw native_.getErrorObject(result);
+        }
+
         native_.addListener(ACCOUNT_LISTENER, this.appCallback);
     }
-    this.instances[accountListenerId] = callback;
+    this.instances[id] = callback;
+
+    return id;
 };
 
 
@@ -354,6 +364,12 @@ AccountListeners.prototype.removeListener = function(accountListenerId) {
     delete this.instances[accountListenerId];
     if (T_.isEmptyObject(this.instances)) {
         native_.removeListener(ACCOUNT_LISTENER, this.appCallback);
+
+        var result = native_.callSync('AccountManager_removeListener');
+
+        if (native_.isFailure(result)) {
+            throw native_.getErrorObject(result);
+        }
     }
 };
 
@@ -366,14 +382,7 @@ AccountManager.prototype.addAccountListener = function() {
         { name: 'callback', type: types_.LISTENER, values: ['onadded', 'onremoved', 'onupdated'] }
     ]);
 
-    var result = native_.callSync('AccountManager_addAccountListener');
-    if (native_.isFailure(result)) {
-        throw native_.getErrorObject(result);
-    }
-
-    var accountListenerId = native_.getResultObject(result);
-    _accountListeners.addListener(accountListenerId, args.callback);
-    return accountListenerId;
+    return _accountListeners.addListener(args.callback);
 }
 
 
@@ -383,16 +392,6 @@ AccountManager.prototype.removeAccountListener = function() {
     ]);
 
     _accountListeners.removeListener(args.accountListenerId);
-
-    if (T_.isEmptyObject(_accountListeners.instances)) {
-        var result = native_.callSync('AccountManager_removeListener');
-
-        if (native_.isFailure(result)) {
-            throw native_.getErrorObject(result);
-        }
-    }
-
-    return;
 }
 
 tizen.Account = Account;
index d748fc91d7fe15fbbe8e088fc3f1369a8d14c70f..1ccae9783cb2590598afc06168d30f32d0a7b422 100644 (file)
@@ -325,6 +325,7 @@ void AccountInstance::AccountManagerAddAccountListener(
 
   int ret = 0;
   if (!subscribe_) {
+    LoggerD("Creating subscription");
     ret = account_subscribe_create(&subscribe_);
     if (ret != ACCOUNT_ERROR_NONE) {
       LoggerE("Failed to create account subscribe");
@@ -332,12 +333,15 @@ void AccountInstance::AccountManagerAddAccountListener(
       return;
     }
 
+    LoggerD("Subscribing for notification");
     ret = account_subscribe_notification(subscribe_, AccountEventCb, this);
     if (ret != ACCOUNT_ERROR_NONE) {
       LoggerE("Failed to subscribe notification");
       ReportError(UnknownException(manager_->GetErrorMsg(ret)), out);
       return;
     }
+
+    LoggerD("Success");
   }
 
   ReportSuccess(out);
@@ -350,9 +354,13 @@ void AccountInstance::AccountManagerRemoveAccountListener(
   CheckAccess(kPrivilegeAccountRead, &out);
 
   if (subscribe_) {
+    LoggerD("Removing subscription");
+
     if (account_unsubscribe_notification(subscribe_) != ACCOUNT_ERROR_NONE) {
       LoggerE("Failed to unsubscribe notification");
     }
+
+    subscribe_ = nullptr;
   }
 
   ReportSuccess(out);