PubsubMessage<E> class
@proxy class PubsubMessage<E> extends Object implements Map, Iterable { String _chan; List _arguments; var _objectData; PubsubMessage(String channel, List arguments, Map kwargs) { _objectData = new Map(); this._chan = channel; this._arguments = arguments; kwargs.forEach((Symbol key, dynamic value){ String property = _symbolToString(key); this[property] = value; }); } get channel => _chan; get args => _arguments; /** noSuchMethod() is where the magic happens. * If we try to access a property using dot notation (eg: o.wibble ), then * noSuchMethod will be invoked, and identify the getter or setter name. * It then looks up in the map contained in _objectData (represented using * this (as this class implements [Map], and forwards it's calls to that * class. * If it finds the getter or setter then it either updates the value, or * replaces the value. * * If isExtendable = true, then it will allow the property access * even if the property doesn't yet exist. */ noSuchMethod(Invocation mirror) { int positionalArgs = 0; if (mirror.positionalArguments != null) positionalArgs = mirror.positionalArguments.length; var property = _symbolToString(mirror.memberName); if (mirror.isGetter && (positionalArgs == 0)) { //synthetic getter if (this.containsKey(property)) { return this[property]; } } else if (mirror.isSetter && positionalArgs == 1) { //synthetic setter //if the property doesn't exist, it will only be added property = property.replaceAll("=", ""); this[property] = mirror.positionalArguments[0]; // args[0]; return this[property]; } //if we get here, then we've not found it - throw. _log("Not found: ${property}"); _log("IsGetter: ${mirror.isGetter}"); _log("IsSetter: ${mirror.isGetter}"); _log("isAccessor: ${mirror.isAccessor}"); super.noSuchMethod(mirror); } String _symbolToString(value) { if (value is Symbol) { return mirrors.MirrorSystem.getName(value); } else { return value.toString(); } } /*************************************************************************** * Iterable implementation methods and properties * */ Iterable toIterable() { return _objectData.values; } Iterator<E> get iterator => this.toIterable().iterator; Iterable map(f(E element)) => this.toIterable().map(f); Iterable<E> where(bool f(E element)) => this.toIterable().where(f); Iterable expand(Iterable f(E element)) => this.toIterable().expand(f); bool contains(E element) => this.toIterable().contains(element); dynamic reduce(E combine(E value, E element)) => this.toIterable().reduce(combine); bool every(bool f(E element)) => this.toIterable().every(f); String join([String separator]) => this.toIterable().join(separator); bool any(bool f(E element)) => this.toIterable().any(f); Iterable<E> take(int n) => this.toIterable().take(n); Iterable<E> takeWhile(bool test(E value)) => this.toIterable().takeWhile(test); Iterable<E> skip(int n) => this.toIterable().skip(n); Iterable<E> skipWhile(bool test(E value)) => this.toIterable().skipWhile(test); E get first => this.toIterable().first; E get last => this.toIterable().last; E get single => this.toIterable().single; E fold(initialValue, dynamic combine(a,b)) => this.toIterable().fold(initialValue, combine); @deprecated E firstMatching(bool test(E value), { E orElse() : null }) { if (orElse != null) this.toIterable().firstWhere(test, orElse: orElse); else this.toIterable().firstWhere(test); } @deprecated E lastMatching(bool test(E value), {E orElse() : null}) { if (orElse != null) this.toIterable().lastWhere(test, orElse: orElse); else this.toIterable().lastWhere(test); } @deprecated E singleMatching(bool test(E value)) => this.toIterable().singleWhere(test); E elementAt(int index) => this.toIterable().elementAt(index); List<dynamic> toList({ bool growable: true }) => this.toIterable().toList(growable:growable); Set<dynamic> toSet() => this.toIterable().toSet(); @deprecated E min([int compare(E a, E b)]) { throw "Deprecated in iterable interface"; } @deprecated E max([int compare(E a, E b)]) { throw "Deprecated in iterable interface"; } dynamic firstWhere(test, {orElse}) => this.toIterable().firstWhere(test, orElse:orElse); dynamic lastWhere(test, {orElse}) => this.toIterable().firstWhere(test, orElse:orElse); dynamic singleWhere(test, {orElse}) => this.toIterable().firstWhere(test, orElse:orElse); /*************************************************************************** * Map implementation methods and properties * * */ bool containsValue(value) => _objectData.containsValue(value); bool containsKey(value) { return _objectData.containsKey(_symbolToString(value)); } bool get isNotEmpty => _objectData.isNotEmpty; operator [](key) => _objectData[key]; forEach(func) => _objectData.forEach(func); Iterable get keys => _objectData.keys; Iterable get values => _objectData.values; int get length => _objectData.length; bool get isEmpty => _objectData.isEmpty; addAll(items) => _objectData.addAll(items); operator []=(key,value) { return _objectData[key] = value; } putIfAbsent(key,ifAbsent()) { return _objectData.putIfAbsent(key, ifAbsent); } remove(key) { return _objectData.remove(key); } clear() { _objectData.clear(); } }
Implements
Constructors
new PubsubMessage(String channel, List arguments, Map kwargs) #
Creates a Map instance with the default implementation.
PubsubMessage(String channel, List arguments, Map kwargs) { _objectData = new Map(); this._chan = channel; this._arguments = arguments; kwargs.forEach((Symbol key, dynamic value){ String property = _symbolToString(key); this[property] = value; }); }
Properties
final args #
get args => _arguments;
final channel #
get channel => _chan;
final E first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
E get first => this.toIterable().first;
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty => _objectData.isEmpty;
final bool isNotEmpty #
Returns true if there is at least one element in this collection.
bool get isNotEmpty => _objectData.isNotEmpty;
final Iterator<E> iterator #
Returns an Iterator that iterates over this Iterable object.
Iterator<E> get iterator => this.toIterable().iterator;
final Iterable keys #
The keys of this
.
Iterable get keys => _objectData.keys;
final int length #
Returns the number of elements in this
.
Counting all elements may be involve running through all elements and can therefore be slow.
int get length => _objectData.length;
final E single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError.
E get single => this.toIterable().single;
Operators
dynamic operator [](key) #
Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.
operator [](key) => _objectData[key];
Methods
dynamic addAll(items) #
Adds all key-value pairs of other
to this map.
If a key of other
is already in this map, its value is overwritten.
The operation is equivalent to doing this[key] = value
for each key
and associated value in other. It iterates over other
, which must
therefore not change during the iteration.
addAll(items) => _objectData.addAll(items);
bool any(bool f(E element)) #
Returns true if one element of this collection satisfies the
predicate test
. Returns false otherwise.
bool any(bool f(E element)) => this.toIterable().any(f);
dynamic clear() #
Removes all pairs from the map.
clear() { _objectData.clear(); }
bool contains(E element) #
Returns true if the collection contains an element equal to element.
bool contains(E element) => this.toIterable().contains(element);
bool containsKey(value) #
Returns true if this map contains the given key.
bool containsKey(value) { return _objectData.containsKey(_symbolToString(value)); }
bool containsValue(value) #
Map implementation methods and properties *
bool containsValue(value) => _objectData.containsValue(value);
E elementAt(int index) #
Returns the indexth element.
If this
has fewer than
index elements throws a RangeError.
Note: if this
does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this
.
E elementAt(int index) => this.toIterable().elementAt(index);
bool every(bool f(E element)) #
Returns true if every elements of this collection satisify the
predicate test
. Returns false
otherwise.
bool every(bool f(E element)) => this.toIterable().every(f);
Iterable expand(Iterable f(E element)) #
Expands each element of this Iterable into zero or more elements.
The resulting Iterable runs through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and calls f for each element of this every time it's iterated.
Iterable expand(Iterable f(E element)) => this.toIterable().expand(f);
E firstMatching(bool test(E value), {orElse: null}) #
@deprecated E firstMatching(bool test(E value), { E orElse() : null }) { if (orElse != null) this.toIterable().firstWhere(test, orElse: orElse); else this.toIterable().firstWhere(test); }
dynamic firstWhere(test, {orElse}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
dynamic firstWhere(test, {orElse}) => this.toIterable().firstWhere(test, orElse:orElse);
E fold(initialValue, combine(a, b)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
E fold(initialValue, dynamic combine(a,b)) => this.toIterable().fold(initialValue, combine);
dynamic forEach(func) #
Applies the function f
to each element of this collection.
forEach(func) => _objectData.forEach(func);
String join([String separator]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.
String join([String separator]) => this.toIterable().join(separator);
E lastMatching(bool test(E value), {orElse: null}) #
@deprecated E lastMatching(bool test(E value), {E orElse() : null}) { if (orElse != null) this.toIterable().lastWhere(test, orElse: orElse); else this.toIterable().lastWhere(test); }
dynamic lastWhere(test, {orElse}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
dynamic lastWhere(test, {orElse}) => this.toIterable().firstWhere(test, orElse:orElse);
Iterable map(f(E element)) #
Returns a lazy Iterable where each element e
of this
is replaced
by the result of f(e)
.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
Iterable map(f(E element)) => this.toIterable().map(f);
E max([int compare(E a, E b)]) #
@deprecated E max([int compare(E a, E b)]) { throw "Deprecated in iterable interface"; }
E min([int compare(E a, E b)]) #
@deprecated E min([int compare(E a, E b)]) { throw "Deprecated in iterable interface"; }
dynamic noSuchMethod(Invocation mirror) #
noSuchMethod() is where the magic happens. If we try to access a property using dot notation (eg: o.wibble ), then noSuchMethod will be invoked, and identify the getter or setter name. It then looks up in the map contained in _objectData (represented using this (as this class implements Map, and forwards it's calls to that class. If it finds the getter or setter then it either updates the value, or replaces the value.
If isExtendable = true, then it will allow the property access even if the property doesn't yet exist.
noSuchMethod(Invocation mirror) { int positionalArgs = 0; if (mirror.positionalArguments != null) positionalArgs = mirror.positionalArguments.length; var property = _symbolToString(mirror.memberName); if (mirror.isGetter && (positionalArgs == 0)) { //synthetic getter if (this.containsKey(property)) { return this[property]; } } else if (mirror.isSetter && positionalArgs == 1) { //synthetic setter //if the property doesn't exist, it will only be added property = property.replaceAll("=", ""); this[property] = mirror.positionalArguments[0]; // args[0]; return this[property]; } //if we get here, then we've not found it - throw. _log("Not found: ${property}"); _log("IsGetter: ${mirror.isGetter}"); _log("IsSetter: ${mirror.isGetter}"); _log("isAccessor: ${mirror.isAccessor}"); super.noSuchMethod(mirror); }
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()) { return _objectData.putIfAbsent(key, ifAbsent); }
dynamic reduce(E combine(E value, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
dynamic reduce(E combine(E value, E element)) => this.toIterable().reduce(combine);
dynamic remove(key) #
Removes the association for the given key. Returns the value for key in the map or null if key is not in the map. Note that values can be null and a returned null value does not always imply that the key is absent.
remove(key) { return _objectData.remove(key); }
E singleMatching(bool test(E value)) #
@deprecated E singleMatching(bool test(E value)) => this.toIterable().singleWhere(test);
dynamic singleWhere(test, {orElse}) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
dynamic singleWhere(test, {orElse}) => this.toIterable().firstWhere(test, orElse:orElse);
Iterable<E> skip(int n) #
Returns an Iterable that skips the first n elements.
If this
has fewer than
n elements, then the resulting Iterable is
empty.
It is an error if n is negative.
Iterable<E> skip(int n) => this.toIterable().skip(n);
Iterable<E> skipWhile(bool test(E value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable iterates over all elements of this
.
As long as the iterator's elements satisfy
test they are
discarded. Once an element does not satisfy the
test the iterator stops
testing and uses every later element unconditionally. That is, the elements
of the returned Iterable are the elements of this
starting from the
first element that does not satisfy
test.
Iterable<E> skipWhile(bool test(E value)) => this.toIterable().skipWhile(test);
Iterable<E> takeWhile(bool test(E value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable starts iterating over the elements of this
.
When the iterator encounters an element e
that does not satisfy
test,
it discards e
and moves into the finished state. That is, it does not
get or provide any more elements.
Iterable<E> takeWhile(bool test(E value)) => this.toIterable().takeWhile(test);
Iterable toIterable() #
Iterable implementation methods and properties *
Iterable toIterable() { return _objectData.values; }
Iterable<E> where(bool f(E element)) #
Returns a lazy Iterable with all elements that satisfy the
predicate test
.
This method returns a view of the mapped elements. As long as the
returned Iterable is not iterated over, the supplied function test
will
not be invoked. Iterating will not cache results, and thus iterating
multiple times over the returned Iterable will invoke the supplied
function test
multiple times on the same element.
Iterable<E> where(bool f(E element)) => this.toIterable().where(f);