DataMap class
A representation for a single unit of structured data.
class DataMap extends DataMapView implements Map { //Track subscriptions and remove /** * Creates an empty data object. */ DataMap(); /** * Creates a new data object from key-value pairs [data]. */ factory DataMap.from(dynamic data) { var dataObj = new DataMap(); dataObj._initAddAll(data); return dataObj; } /** * Assigns the [value] to the [key] field. */ void add(String key, value, {author: null}) { _addAll({key: value}, author: author); } /** * Adds all key-value pairs of [other] to this data. */ void addAll(Map other, {author: null}) { _addAll(other, author:author); } void _initAddAll(Map other){ other.forEach((key, value) { if (value is List || value is Set || value is Map) { value = cleanify(value); } if(value is ChangeNotificationsMixin) _addOnDataChangeListener(key, value); _fields[key] = value; }); } void _addAll(Map other, {author: null}) { other.forEach((key, value) { if (value is List || value is Set || value is Map) { value = cleanify(value); } if (_fields.containsKey(key)) { if(_fields[key] is DataReference) _fields[key].changeValue(value, author: author); else { _markChanged(key, new Change(_fields[key], value)); _removeOnDataChangeListener(key); if(value is ChangeNotificationsMixin) _addOnDataChangeListener(key, value); _fields[key] = value; } } else { _markAdded(key, value); if(value is ChangeNotificationsMixin) _addOnDataChangeListener(key, value); _fields[key] = value; } }); _notify(author: author); } /** * Assigns the [value] to the [key] field. */ void operator[]=(String key, value) { _addAll({key: value}); } /** * Removes [key] from the data object. */ void remove(String key, {author: null}) { _removeAll([key], author: author); } /** * Remove all [keys] from the data object. */ void removeAll(List<String> keys, {author: null}) { _removeAll(keys, author:author); } void _removeAll(List<String> keys, {author: null}) { for (var key in keys) { if(_fields.containsKey(key)){ _markRemoved(key, this[key]); _fields.remove(key); } _removeOnDataChangeListener(key); } _notify(author: author); } void clear({author: null}) { _removeAll(keys.toList(), author: author); } void forEach(void f(key, value)) { _fields.forEach((K, V) => f(K, V is DataReference ? V.value : V)); } DataReference ref(String key) { if(!_fields.containsKey(key)) return null; if(_fields[key] is! DataReference) { _removeOnDataChangeListener(key); _fields[key] = new DataReference(_fields[key]); _addOnDataChangeListener(key, _fields[key]); } return _fields[key]; } putIfAbsent(key, ifAbsent()) { if (!containsKey(key)) { _addAll({key: ifAbsent()}); } } }
Extends
DataMapView > DataMap
Implements
Constructors
new DataMap() #
Creates an empty data object.
DataMap();
factory DataMap.from(data) #
Creates a new data object from key-value pairs data.
factory DataMap.from(dynamic data) { var dataObj = new DataMap(); dataObj._initAddAll(data); return dataObj; }
Properties
final bool isEmpty #
Returns true if there is no {key, value} pair in the data object.
bool get isEmpty { return _fields.isEmpty; }
final bool isNotEmpty #
Returns true if there is at least one {key, value} pair in the data object.
bool get isNotEmpty { return _fields.isNotEmpty; }
final Iterable keys #
The keys of data object.
Iterable get keys { return _fields.keys; }
final int length #
The number of {key, value} pairs in the DataMap.
int get length { return _fields.length; }
final Stream<dynamic> onBeforeAdd #
Stream populated with DataMapView events before any data object is added.
Stream<dynamic> get onBeforeAdd { if(_onBeforeAddedController == null) { _onBeforeAddedController = new StreamController.broadcast(sync: true); } return _onBeforeAddedController.stream; }
final Stream<dynamic> onBeforeRemove #
Stream populated with DataMapView events before any data object is removed.
Stream<dynamic> get onBeforeRemove { if(_onBeforeRemovedController == null) { _onBeforeRemovedController = new StreamController.broadcast(sync: true); } return _onBeforeRemovedController.stream; }
final Stream<ChangeSet> onChange #
Stream populated with ChangeSet events whenever the collection or any of data object contained gets changed.
Stream<ChangeSet> get onChange { if(_onChangeController == null) { _onChangeController = new StreamController.broadcast(); } return _onChangeController.stream; }
final Stream<Map> onChangeSync #
Stream populated with {'change': ChangeSet, 'author': dynamic
} events
synchronously at the moment when the collection or any data object contained
gets changed.
Stream<Map> get onChangeSync => _onChangeSyncController.stream;
final Iterable values #
The values of DataMap.
Iterable get values { return _fields.values.map((elem) => elem is DataReference ? elem.value : elem); }
Operators
dynamic operator [](key) #
Returns the value for the given key or null if key is not in the data object. Because null values are supported, one should use containsKey to distinguish between an absent key and a null value.
dynamic operator[](key) => _fields[key] is DataReference ? _fields[key].value : _fields[key];
Methods
void add(String key, value, {author: null}) #
Assigns the value to the key field.
void add(String key, value, {author: null}) { _addAll({key: value}, author: author); }
void addAll(Map other, {author: null}) #
Adds all key-value pairs of other to this data.
void addAll(Map other, {author: null}) { _addAll(other, author:author); }
void clear({author: null}) #
Removes all pairs from the map.
void clear({author: null}) { _removeAll(keys.toList(), author: author); }
bool containsKey(String key) #
Returns whether this data object contains the given key.
bool containsKey(String key) { return _fields.containsKey(key); }
bool containsValue(Object value) #
bool containsValue(Object value) { if(_fields.containsValue(value)) return true; bool contains = false; _fields.forEach((K, elem) { if(elem is DataReference && elem.value == value) contains = true;}); return contains; }
void dispose() #
Should release all allocated (referenced) resources as subscribtions.
void dispose() { _dispose(); }
void forEach(void f(key, value)) #
Applies f to each {key, value} pair of the map.
It is an error to add or remove keys from the map during iteration.
void forEach(void f(key, value)) { _fields.forEach((K, V) => f(K, V is DataReference ? V.value : V)); }
dynamic putIfAbsent(key, ifAbsent()) #
If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.
Map<String, int> scores = {'Bob': 36};
for (var key in ['Bob', 'Rohan', 'Sophena']) {
scores.putIfAbsent(key, () => key.length);
}
scores['Bob']; // 36
scores['Rohan']; // 5
scores['Sophena']; // 7
The code that ifAbsent executes must not add or remove keys.
putIfAbsent(key, ifAbsent()) { if (!containsKey(key)) { _addAll({key: ifAbsent()}); } }
DataReference ref(String key) #
DataReference ref(String key) { if(!_fields.containsKey(key)) return null; if(_fields[key] is! DataReference) { _removeOnDataChangeListener(key); _fields[key] = new DataReference(_fields[key]); _addOnDataChangeListener(key, _fields[key]); } return _fields[key]; }
void remove(String key, {author: null}) #
Removes key from the data object.
void remove(String key, {author: null}) { _removeAll([key], author: author); }
void removeAll(List<String> keys, {author: null}) #
Remove all keys from the data object.
void removeAll(List<String> keys, {author: null}) { _removeAll(keys, author:author); }
Map toJson() #
Converts to Map.
Map toJson() => new Map.fromIterables(this.keys, this.values);
String toString() #
Returns Json representation of the object.
String toString() => toJson().toString();