private void Upsert(DictionaryModification <TKey, TValue> modification)
            {
                TValue existingValue;

                if (_dictionary._state.TryGetValue(modification.Key, out existingValue))
                {
                    try
                    {
                        var newValue = _dictionary._updateFunction(modification.Key, existingValue, modification.Value);

                        _dictionary._state[modification.Key] = newValue;
                        _dictionary._subject.OnNext(DictionaryNotification.Updated(modification.Key, newValue,
                                                                                   modification.Value, existingValue));
                    }
                    catch (Exception ex)
                    {
                        _dictionary._error = ex;
                        _dictionary._subject.OnError(
                            new ObservableDictionaryUpdateException("Exception thrown when updating " + modification, ex));
                    }
                }
                else
                {
                    _dictionary._state[modification.Key] = modification.Value;
                    _dictionary._subject.OnNext(DictionaryNotification.Inserted(modification.Key, modification.Value));
                }
            }
            public void OnNext(DictionaryModification <TKey, TValue> value)
            {
                if (DictionaryModification <TKey, TValue> .Initialised == value)
                {
                    _dictionary._subject.OnNext(DictionaryNotification.Initialised <TKey, TValue>());
                }
                else
                {
                    switch (value.Type)
                    {
                    case DictionaryModificationType.Upsert:
                        Upsert(value);
                        break;

                    case DictionaryModificationType.Replace:
                        Replace(value);
                        break;

                    case DictionaryModificationType.Remove:
                        Remove(value);
                        break;

                    case DictionaryModificationType.Clear:
                        Clear();
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
示例#3
0
            private void Upsert(DictionaryModification <TKey, TValue> modification)
            {
                TValue existingValue;

                if (_dictionary._state.TryGetValue(modification.Key, out existingValue))
                {
                    try
                    {
                        // TODO Check order is correct here.
                        var newValue = _dictionary._updateFunction.Invoke(modification.Key, existingValue,
                                                                          modification.Value);

                        _dictionary._state[modification.Key] = newValue;
                        _dictionary._subject.OnNext(DictionaryNotification.Updated(modification.Key, newValue,
                                                                                   modification.Value, existingValue));
                    }
                    catch (Exception ex)
                    {
                        // TODO Check that this errors the whole stream.
                        _dictionary._error = ex;
                        _dictionary._subject.OnError(
                            new ObservableDictionaryUpdateException("Exception thrown when updating " + modification, ex));
                    }
                }
                else
                {
                    _dictionary._state[modification.Key] = modification.Value;
                    _dictionary._subject.OnNext(DictionaryNotification.Inserted(modification.Key, modification.Value));
                }
            }
            private void Remove(DictionaryModification <TKey, TValue> modification)
            {
                TValue removedValue;

                if (_dictionary._state.TryGetValue(modification.Key, out removedValue))
                {
                    _dictionary._state.Remove(modification.Key);
                    _dictionary._subject.OnNext(DictionaryNotification.Removed(modification.Key, removedValue));
                }
                // Design decision: We don't signal 'removed' for a value that didn't exist.
            }
示例#5
0
            private void Remove(DictionaryModification <TKey, TValue> modification)
            {
                TValue removedValue;

                if (_dictionary._state.TryGetValue(modification.Key, out removedValue))
                {
                    _dictionary._state.Remove(modification.Key);
                    // TODO Should we signal 'removed' for a value that didn't exist..?
                    _dictionary._subject.OnNext(DictionaryNotification.Removed(modification.Key, removedValue));
                }
            }
示例#6
0
            private void Replace(DictionaryModification <TKey, TValue> modification)
            {
                TValue existingValue;

                if (_dictionary._state.TryGetValue(modification.Key, out existingValue))
                {
                    _dictionary._subject.OnNext(DictionaryNotification.Replaced(modification.Key, modification.Value,
                                                                                existingValue));
                }
                else
                {
                    // TODO Should we do an upsert when the item to be replaced doesn't exist, or signal replaced with a missing value?
                    Upsert(modification);
                }
            }
            private void Replace(DictionaryModification <TKey, TValue> modification)
            {
                TValue existingValue;

                if (_dictionary._state.TryGetValue(modification.Key, out existingValue))
                {
                    _dictionary._state[modification.Key] = modification.Value;
                    _dictionary._subject.OnNext(DictionaryNotification.Replaced(modification.Key, modification.Value,
                                                                                existingValue));
                }
                else
                {
                    // Design decision: we do an upsert when the item to be replaced doesn't exist, rather than signal replaced with a missing value.
                    Upsert(modification);
                }
            }