Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Better support map.keys/map.values in bindings #67

Open
DartBot opened this issue Jun 5, 2015 · 0 comments
Open

Better support map.keys/map.values in bindings #67

DartBot opened this issue Jun 5, 2015 · 0 comments

Comments

@DartBot
Copy link

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/2049220?v=3" align="left" width="96" height="96"hspace="10"> Issue by sigmundch
Originally opened as dart-lang/sdk#20211


Right now map.keys/map.values return a new list every time they are requested. This can bring up issues in bindings where we might be comparing object's identities.

One idea is to cache the value and only return a new value when there are changes in the underlying Map. For example, doing something like the following in ObservableMap:

      /// Cache of keys so that only a new collection is created when we have reason
      /// to believe the keys have changed.
      Iterable<K> _keys;
      @­reflectable Iterable<K> get keys {
        if (_keys == null) _keys = _map.keys;
        return _keys;
      }
    
      /// Cache of keys so that only a new collection is created when we have reason
      /// to believe the values have changed.
      Iterable<V> _values;
      @­reflectable Iterable<V> get values {
        if (_values == null) _values = _map.values;
        return _values;
      }
    
      ...
    
      // Note: we don't really have a reasonable old/new value to use here.
      // But this should fix "keys" and "values" in templates with minimal overhead.
      void _notifyKeysValuesChanged() {
        _keys = null;
        notifyChange(new PropertyChangeRecord(this, #keys, null, null));
        _notifyValuesChanged();
      }
    
      void _notifyValuesChanged() {
        _values = null;
        notifyChange(new PropertyChangeRecord(this, #values, null, null));
      }
    }
 

This unfortunately departs from the semantics we got from dart:core Map, where keys/values are always a new instance.

Other ideas:
  - do something similar to the above, but also define observable iterables that can provide notifications for changes in the underlying map.

  - add logic to compare these collections more carefully in the binding layer.

Thoughts?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

1 participant