Dart Documentationobserve

observe library

Warning: this library is experimental, and APIs are subject to change.

This library is used to observe changes to Observable types. It also has helpers to implement Observable objects.

For example:

class Monster extends Unit with ObservableMixin {
  int _health = 100;
  get health => _health;
  set health(value) {
    _health = notifyChange(const Symbol('health'), _health, value);
  }

  void damage(int amount) {
    print('$this takes $amount damage!');
    health -= amount;
  }

  toString() => 'Monster with $health hit points';
}

main() {
  var obj = new Monster();
  obj.changes.listen((records) {
    print('Changes to $obj were: $records');
  });
  // Schedules asynchronous delivery of these changes
  obj.damage(10);
  obj.damage(20);
  print('done!');
}

Functions

dynamic toObservable(value, {bool deep: true}) #

Converts the Iterable or Map to an ObservableList or ObservableMap, respectively. This is a convenience function to make it easier to convert literals into the corresponding observable collection type.

If value is not one of those collection types, or is already Observable, it will be returned unmodified.

If value is a Map, the resulting value will use the appropriate kind of backing map: either HashMap, LinkedHashMap, or SplayTreeMap.

By default this performs a deep conversion, but you can set deep to false for a shallow conversion. This does not handle circular data structures. If a conversion is peformed, mutations are only observed to the result of this function. Changing the original collection will not affect it.

toObservable(value, {bool deep: true}) =>
   deep ? _toObservableDeep(value) : _toObservableShallow(value);

void queueChangeRecords(void deliverChanges()) #

Queues an action to happen during the deliverChangeRecords timeslice.

void queueChangeRecords(void deliverChanges()) {
 if (_deliverCallbacks == null) {
   _deliverCallbacks = new Queue<Function>();
   runAsync(deliverChangeRecords);
 }
 _deliverCallbacks.add(deliverChanges);
}

void deliverChangeRecords() #

Synchronously deliver Observable.changes for all observables. If new changes are added as a result of delivery, this will keep running until all pending change records are delivered.

void deliverChangeRecords() {
 if (_deliverCallbacks == null) return;

 while (!_deliverCallbacks.isEmpty) {
   var deliver = _deliverCallbacks.removeFirst();

   try {
     deliver();
   } catch (e, s) {
     // Schedule the error to be top-leveled later.
     new Completer().completeError(e, s);
   }
 }

 // Null it out, so [queueChangeRecords] will reschedule this method.
 _deliverCallbacks = null;
}

Abstract Classes

Classes

Typedefs