private async void OnCacheFileChangedAsync(object sender, FileSystemEventArgs args)
        {
            // avoid the high cost of computing the added / removed accounts if nobody listens to this
            if (CacheChanged == null)
            {
                return;
            }

            try
            {
                IEnumerable <string> added;
                IEnumerable <string> removed;

                using (CreateCrossPlatLock(_storageCreationProperties))
                {
                    var currentAccountIds = await GetAccountIdentifiersAsync(_storageCreationProperties, _logger).ConfigureAwait(false);

                    var intersect = currentAccountIds.Intersect(_knownAccountIds);
                    removed = _knownAccountIds.Except(intersect);
                    added   = currentAccountIds.Except(intersect);

                    _knownAccountIds = currentAccountIds;
                }

                if (added.Any() || removed.Any())
                {
                    CacheChanged.Invoke(sender, new CacheChangedEventArgs(added, removed));
                }
            }
            catch (Exception e)
            {
                // Never let this throw, just log errors
                _logger.LogWarning($"Exception within File Watcher : {e}");
            }
        }
Ejemplo n.º 2
0
        private async void OnCacheFileChangedAsync(object sender, FileSystemEventArgs args)
        {
            try
            {
                IEnumerable <string> added   = Enumerable.Empty <string>();
                IEnumerable <string> removed = Enumerable.Empty <string>();

                using (CreateCrossPlatLock(_storageCreationProperties))
                {
                    var currentAccountIds = await GetAccountIdentifiersAsync(_storageCreationProperties).ConfigureAwait(false);

                    var intersect = currentAccountIds.Intersect(_knownAccountIds);
                    removed = _knownAccountIds.Except(intersect);
                    added   = currentAccountIds.Except(intersect);

                    _knownAccountIds = currentAccountIds;
                }

                if (added.Any() || removed.Any())
                {
                    CacheChanged?.Invoke(sender, new CacheChangedEventArgs(added, removed));
                }
            }
            catch (Exception e)
            {
                // Never let this throw, just log errors
                _logger.TraceEvent(TraceEventType.Warning, /*id*/ 0, $"Exception within File Watcher : {e}");
            }
        }
Ejemplo n.º 3
0
        public void RefreshCache(string fieldName = null)
        {
            _orderByField = fieldName;
            Context.Refresh(RefreshMode.StoreWins, Context.Contacts);

            if (_contactsCache == null)
            {
                _contactsCache = new SynchronizedObservableCollection <Contact>(new ObservableCollection <Contact>());
            }

            // _contactsCache.Clear();
            _contactsCache = new SynchronizedObservableCollection <Contact>(new ObservableCollection <Contact>(orderContactsByField(fieldName)));

            if (CacheChanged != null)
            {
                CacheChanged.Invoke(this, EventArgs.Empty);
            }

            //foreach (Contact contact in orderContactsByField(fieldName))
            //{
            //    _contactsCache.Add(contact);
            //}
        }
Ejemplo n.º 4
0
 private void OnCacheChanged()
 {
     CacheChanged?.Invoke(this, EventArgs.Empty);
 }
Ejemplo n.º 5
0
        // Called by the timer to make a request for data
        public async void FetchData()
        {
            // Stop the timer so we don't get fired again unless data is requested
            _timer.Stop();
            if (_requestInProgress != null)
            {
                // Verify if an active request is still needed
                if (_request.Intersects(_requestInProgress))
                {
                    return;
                }

                // Cancel the existing request
                _cancelTokenSource.Cancel();
            }

            ItemIndexRange nextRequest = GetFirstRequestBlock(_maxBatchFetchSize);

            if (nextRequest != null)
            {
                _cancelTokenSource = new CancellationTokenSource();
                CancellationToken ct = _cancelTokenSource.Token;
                _requestInProgress = nextRequest;
                T[] data = null;
                try
                {
                    // Use the callback to get the data, passing in a cancellation token
                    data = await _fetchDataCallback(nextRequest, ct);

                    if (!ct.IsCancellationRequested)
                    {
                        for (int i = 0; i < data.Length; i++)
                        {
                            int cacheIndex = nextRequest.FirstIndex + i;

                            T oldItem = this[cacheIndex];
                            T newItem = data[i];

                            if (!newItem.Equals(oldItem))
                            {
                                this[cacheIndex] = newItem;

                                // Fire CacheChanged so that the datasource can fire its INCC event, and do other work based on the item having data
#pragma warning disable SA1118 // Parameter must not span multiple lines
                                CacheChanged?.Invoke(this,
                                                     new CacheChangedEventArgs <T>
                                {
                                    OldItem   = oldItem,
                                    NewItem   = newItem,
                                    ItemIndex = cacheIndex
                                });
#pragma warning restore SA1118 // Parameter must not span multiple lines
                            }
                        }

                        _request.Subtract(new ItemIndexRange(nextRequest.FirstIndex, (uint)data.Length));
                    }
                }

                // Try/Catch is needed as cancellation is via an exception
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    _requestInProgress = null;

                    // Start another request if required
                    FetchData();
                }
            }
        }