Broadcast events after committing transactions.

This commit is contained in:
akwizgran
2016-02-11 14:49:41 +00:00
parent de8cc50fb4
commit 146dac056d
6 changed files with 88 additions and 85 deletions

View File

@@ -9,9 +9,6 @@ import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.event.ContactAddedEvent;
import org.briarproject.api.event.ContactRemovedEvent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.IdentityManager.RemoveIdentityHook;
@@ -37,14 +34,12 @@ class ContactManagerImpl implements ContactManager, Service,
Logger.getLogger(ContactManagerImpl.class.getName());
private final DatabaseComponent db;
private final EventBus eventBus;
private final List<AddContactHook> addHooks;
private final List<RemoveContactHook> removeHooks;
@Inject
ContactManagerImpl(DatabaseComponent db, EventBus eventBus) {
ContactManagerImpl(DatabaseComponent db) {
this.db = db;
this.eventBus = eventBus;
addHooks = new CopyOnWriteArrayList<AddContactHook>();
removeHooks = new CopyOnWriteArrayList<RemoveContactHook>();
}
@@ -53,8 +48,6 @@ class ContactManagerImpl implements ContactManager, Service,
public boolean start() {
// Finish adding/removing any partly added/removed contacts
try {
List<ContactId> added = new ArrayList<ContactId>();
List<ContactId> removed = new ArrayList<ContactId>();
Transaction txn = db.startTransaction();
try {
for (Contact c : db.getContacts(txn)) {
@@ -62,22 +55,16 @@ class ContactManagerImpl implements ContactManager, Service,
for (AddContactHook hook : addHooks)
hook.addingContact(txn, c);
db.setContactStatus(txn, c.getId(), ACTIVE);
added.add(c.getId());
} else if (c.getStatus().equals(REMOVING)) {
for (RemoveContactHook hook : removeHooks)
hook.removingContact(txn, c);
db.removeContact(txn, c.getId());
removed.add(c.getId());
}
}
txn.setComplete();
} finally {
db.endTransaction(txn);
}
for (ContactId c : added)
eventBus.broadcast(new ContactAddedEvent(c));
for (ContactId c : removed)
eventBus.broadcast(new ContactRemovedEvent(c));
return true;
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -115,7 +102,6 @@ class ContactManagerImpl implements ContactManager, Service,
} finally {
db.endTransaction(txn);
}
eventBus.broadcast(new ContactAddedEvent(c));
return c;
}
@@ -159,7 +145,6 @@ class ContactManagerImpl implements ContactManager, Service,
} finally {
db.endTransaction(txn);
}
eventBus.broadcast(new ContactRemovedEvent(c));
}
private void removeContact(Transaction txn, ContactId c)