From a4e7db1bde8812ca003809a6d323c0069266a864 Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Thu, 12 Feb 2015 11:40:22 +0100
Subject: [PATCH] [Account] Fixes for addAccountListener() and
removeAccountListener().
Change-Id: I8b6f4d9c5901605040c14937da5d722d626f9761
---
src/account/account_api.js | 39 ++++++++++++++++-----------------
src/account/account_instance.cc | 8 +++++++
2 files changed, 27 insertions(+), 20 deletions(-)
diff --git a/src/account/account_api.js b/src/account/account_api.js
index f2bf1124..712a952a 100644
--- a/src/account/account_api.js
+++ b/src/account/account_api.js
@@ -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;
diff --git a/src/account/account_instance.cc b/src/account/account_instance.cc
index d748fc91..1ccae978 100644
--- a/src/account/account_instance.cc
+++ b/src/account/account_instance.cc
@@ -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);
--
2.34.1