private void NotifyChangeListeners(ReplicationStateTransition transition = null)
        {
            Log.V(TAG, "NotifyChangeListeners ({0}/{1}, state={2} (batch={3}, net={4}))",
                CompletedChangesCount, ChangesCount,
                _stateMachine.State, Batcher == null ? 0 : Batcher.Count(), _requests.Count);

            var evt = _changed;
            if (evt == null) {
                return;
            }

            var args = new ReplicationChangeEventArgs(this, transition);

            // Ensure callback runs on captured context, which should be the UI thread.
            var stackTrace = Environment.StackTrace;
            LocalDatabase.Manager.CapturedContext.StartNew(() =>
            {
                evt(this, args);
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="Couchbase.Lite.ReplicationChangeEventArgs"/> class.
 /// </summary>
 /// <param name="sender">The <see cref="Couchbase.Lite.Replication"/> that raised the event.</param>
 /// <param name="transition">The transition that caused the state in the replication, if applicable</param>
 public ReplicationChangeEventArgs (Replication sender, ReplicationStateTransition transition)
 {
     _source = sender;
     _transition = transition;
 }
 private void NotifyChangeListenersStateTransition(StateMachine<ReplicationState, ReplicationTrigger>.Transition transition)
 {
     var stateTransition = new ReplicationStateTransition(transition);
     NotifyChangeListeners(stateTransition);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="Couchbase.Lite.ReplicationChangeEventArgs"/> class.
 /// </summary>
 /// <param name="sender">The <see cref="Couchbase.Lite.Replication"/> that raised the event.</param>
 /// <param name="transition">The transition that caused the state in the replication, if applicable</param>
 public ReplicationChangeEventArgs (Replication sender, ReplicationStateTransition transition)
 {
     _source = sender;
     _transition = transition;
     _changesCount = sender.ChangesCount;
     _completedChangesCount = sender.CompletedChangesCount;
     _status = sender.Status;
 }
        private void NotifyChangeListeners(ReplicationStateTransition transition = null)
        {
            Log.V(TAG, "NotifyChangeListeners ({0}/{1}, state={2} (batch={3}, net={4}))",
                CompletedChangesCount, ChangesCount,
                _stateMachine.State, Batcher == null ? 0 : Batcher.Count(), _requests.Count);

            _pendingDocumentIDs = null;
            var evt = _changed;
            if (evt == null) {
                return;
            }

            var args = new ReplicationChangeEventArgs(this, transition);

            // Ensure callback runs on captured context, which should be the UI thread.
            var stackTrace = Environment.StackTrace;

            lock(_eventQueue) {
                _eventQueue.Enqueue(args);
            }

            if (_eventContext != null) {
                _eventContext.StartNew(() =>
                {
                    lock (_eventQueue) { 
                        while (_eventQueue.Count > 0) {
                            evt(this, _eventQueue.Dequeue());
                        }
                    }
                });
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Couchbase.Lite.ReplicationChangeEventArgs"/> class.
        /// </summary>
        /// <param name="sender">The <see cref="Couchbase.Lite.Replication"/> that raised the event.</param>
        /// <param name="transition">The transition that caused the state in the replication, if applicable</param>
        public ReplicationChangeEventArgs (Replication sender, ReplicationStateTransition transition)
        {
            if (sender == null) {
                Log.To.Sync.E(Tag, "sender null in ctor, throwing...");
                throw new ArgumentNullException("sender");
            }

            Source = sender;
            ReplicationStateTransition = transition;
            ChangesCount = sender.ChangesCount;
            CompletedChangesCount = sender.CompletedChangesCount;
            Status = sender.Status;
            LastError = sender.LastError;
            Username = sender.Username;

            if (Status == ReplicationStatus.Offline && transition != null && transition.Destination == ReplicationState.Running) {
                Status = ReplicationStatus.Active;
            }

        }
        private void NotifyChangeListeners(ReplicationStateTransition transition = null) 
        {
            Log.To.Sync.I(Tag, "NotifyChangeListeners ({0}/{1}, state={2} (batch={3}, net={4}))",
                CompletedChangesCount, ChangesCount,
                _stateMachine.State, Batcher == null ? 0 : Batcher.Count(), _remoteSession.RequestCount);

            _pendingDocumentIDs = null;
            Username = (Authenticator as IAuthorizer)?.Username;

            var evt = _changed;
            if (evt == null) {
                return;
            }

            var args = new ReplicationChangeEventArgs(this, transition);

            // Ensure callback runs on captured context, which should be the UI thread.
            var stackTrace = Environment.StackTrace;

            lock(_eventQueue) {
                _eventQueue.Enqueue(args);
            }

            Log.To.TaskScheduling.V(Tag, "Scheduling Changed callback...");
            if (_eventContext != null) {
                _eventContext.StartNew(() =>
                {
                    lock (_eventQueue) { 
                        if(_eventQueue.Count > 0) {
                            Log.To.TaskScheduling.V(Tag, "Firing {0} queued callback(s)", _eventQueue.Count);
                        } else {
                            Log.To.TaskScheduling.V(Tag, "No callback scheduled, not firing");
                        }

                        while (_eventQueue.Count > 0) {
                            try {
                                evt (this, _eventQueue.Dequeue ());
                            } catch (Exception e) {
                                Log.To.Sync.E (Tag, "Exception in Changed callback, " +
                                               "this will cause instability unless corrected!", e);
                            }
                        }
                    }
                });
            }
        }