| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library observe.src.list_path_observer; | 5 library observe.src.list_path_observer; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'package:observe/observe.dart'; | 8 import 'package:observe/observe.dart'; |
| 9 | 9 |
| 10 // Inspired by ArrayReduction at: | 10 // Inspired by ArrayReduction at: |
| 11 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js | 11 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js |
| 12 // The main difference is we support anything on the rich Dart Iterable API. | 12 // The main difference is we support anything on the rich Dart Iterable API. |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Observes a path starting from each item in the list. | 15 * Observes a path starting from each item in the list. |
| 16 */ | 16 */ |
| 17 class ListPathObserver<E, P> extends ChangeNotifier { | 17 class ListPathObserver<E, P> extends ChangeNotifier { |
| 18 final ObservableList<E> list; | 18 final ObservableList<E> list; |
| 19 final String _itemPath; | 19 final String _itemPath; |
| 20 final List<PathObserver> _observers = <PathObserver>[]; | 20 final List<PathObserver> _observers = <PathObserver>[]; |
| 21 final List<StreamSubscription> _subs = <StreamSubscription>[]; | 21 final List<StreamSubscription> _subs = <StreamSubscription>[]; |
| 22 StreamSubscription _sub; | 22 StreamSubscription _sub; |
| 23 bool _scheduled = false; | 23 bool _scheduled = false; |
| 24 Iterable<P> _value; | 24 Iterable<P> _value; |
| 25 | 25 |
| 26 ListPathObserver(this.list, String path) | 26 ListPathObserver(this.list, String path) |
| 27 : _itemPath = path { | 27 : _itemPath = path { |
| 28 | 28 |
| 29 _sub = list.changes.listen((records) { | 29 // TODO(jmesserly): delay observation until we are observed. |
| 30 _sub = list.listChanges.listen((records) { |
| 30 for (var record in records) { | 31 for (var record in records) { |
| 31 if (record is ListChangeRecord) { | 32 _observeItems(record.addedCount - record.removed.length); |
| 32 _observeItems(record.addedCount - record.removedCount); | |
| 33 } | |
| 34 } | 33 } |
| 35 _scheduleReduce(null); | 34 _scheduleReduce(null); |
| 36 }); | 35 }); |
| 37 | 36 |
| 38 _observeItems(list.length); | 37 _observeItems(list.length); |
| 39 _reduce(); | 38 _reduce(); |
| 40 } | 39 } |
| 41 | 40 |
| 42 @reflectable Iterable<P> get value => _value; | 41 @reflectable Iterable<P> get value => _value; |
| 43 | 42 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 69 } | 68 } |
| 70 } else if (lengthAdjust < 0) { | 69 } else if (lengthAdjust < 0) { |
| 71 for (int i = 0; i < -lengthAdjust; i++) { | 70 for (int i = 0; i < -lengthAdjust; i++) { |
| 72 _subs.removeLast().cancel(); | 71 _subs.removeLast().cancel(); |
| 73 } | 72 } |
| 74 int len = _observers.length; | 73 int len = _observers.length; |
| 75 _observers.removeRange(len + lengthAdjust, len); | 74 _observers.removeRange(len + lengthAdjust, len); |
| 76 } | 75 } |
| 77 } | 76 } |
| 78 } | 77 } |
| OLD | NEW |