couchbase/couchbase-lite-java-core — pull request #221 by mjq (2014-06-05 01:53:19+00:00)
Title: Make Database.addChangeListener idempotent

Adding a ChangeListener to a Database should be idempotent: no matter how many times the same listener is added, it should only be called once. This matches the behaviour of Java's `Observable`.

Without this fix, it's possible for the framework to add the same change listener more than once, resulting in changes getting processed more than once, which might have unexpected results.

How I noticed it: Start a continuous push replication, then put your phone in airplane mode, then bring it back and check the Database's `changeListeners` field: it will contain two copies of the same `Pusher`. Every change ends up getting handled twice from then on, and the problem multiplies every time you lose and regain connection. I don't think there is any consequence to it beyond some wasted cycles by the client (`Batcher`'s inbox is a `Set`, so it should ignore the dupes), but I thought it was worth fixing anyway.