| 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 /// Observes a path starting from each item in the list. | 14 /// Observes a path starting from each item in the list. |
| 15 class ListPathObserver<E, P> extends ChangeNotifier { | 15 class ListPathObserver<E, P> extends ChangeNotifier { |
| 16 final ObservableList<E> list; | 16 final ObservableList<E> list; |
| 17 final String _itemPath; | 17 final String _itemPath; |
| 18 final List<PathObserver> _observers = <PathObserver>[]; | 18 final List<PathObserver> _observers = <PathObserver>[]; |
| 19 final List<StreamSubscription> _subs = <StreamSubscription>[]; | |
| 20 StreamSubscription _sub; | 19 StreamSubscription _sub; |
| 21 bool _scheduled = false; | 20 bool _scheduled = false; |
| 22 Iterable<P> _value; | 21 Iterable<P> _value; |
| 23 | 22 |
| 24 ListPathObserver(this.list, String path) | 23 ListPathObserver(this.list, String path) |
| 25 : _itemPath = path { | 24 : _itemPath = path { |
| 26 | 25 |
| 27 // TODO(jmesserly): delay observation until we are observed. | 26 // TODO(jmesserly): delay observation until we are observed. |
| 28 _sub = list.listChanges.listen((records) { | 27 _sub = list.listChanges.listen((records) { |
| 29 for (var record in records) { | 28 for (var record in records) { |
| 30 _observeItems(record.addedCount - record.removed.length); | 29 _observeItems(record.addedCount - record.removed.length); |
| 31 } | 30 } |
| 32 _scheduleReduce(null); | 31 _scheduleReduce(null); |
| 33 }); | 32 }); |
| 34 | 33 |
| 35 _observeItems(list.length); | 34 _observeItems(list.length); |
| 36 _reduce(); | 35 _reduce(); |
| 37 } | 36 } |
| 38 | 37 |
| 39 @reflectable Iterable<P> get value => _value; | 38 @reflectable Iterable<P> get value => _value; |
| 40 | 39 |
| 41 void dispose() { | 40 void dispose() { |
| 42 if (_sub != null) _sub.cancel(); | 41 if (_sub != null) _sub.cancel(); |
| 43 _subs.forEach((s) => s.cancel()); | 42 _observers.forEach((o) => o.close()); |
| 44 _subs.clear(); | 43 _observers.clear(); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void _reduce() { | 46 void _reduce() { |
| 48 _scheduled = false; | 47 _scheduled = false; |
| 49 var newValue = _observers.map((o) => o.value); | 48 var newValue = _observers.map((o) => o.value); |
| 50 _value = notifyPropertyChange(#value, _value, newValue); | 49 _value = notifyPropertyChange(#value, _value, newValue); |
| 51 } | 50 } |
| 52 | 51 |
| 53 void _scheduleReduce(_) { | 52 void _scheduleReduce(_) { |
| 54 if (_scheduled) return; | 53 if (_scheduled) return; |
| 55 _scheduled = true; | 54 _scheduled = true; |
| 56 scheduleMicrotask(_reduce); | 55 scheduleMicrotask(_reduce); |
| 57 } | 56 } |
| 58 | 57 |
| 59 void _observeItems(int lengthAdjust) { | 58 void _observeItems(int lengthAdjust) { |
| 60 if (lengthAdjust > 0) { | 59 if (lengthAdjust > 0) { |
| 61 for (int i = 0; i < lengthAdjust; i++) { | 60 for (int i = 0; i < lengthAdjust; i++) { |
| 62 int len = _observers.length; | 61 int len = _observers.length; |
| 63 var pathObs = new PathObserver(list, '$len.$_itemPath'); | 62 var pathObs = new PathObserver(list, '$len.$_itemPath'); |
| 64 _subs.add(pathObs.changes.listen(_scheduleReduce)); | 63 pathObs.open(_scheduleReduce); |
| 65 _observers.add(pathObs); | 64 _observers.add(pathObs); |
| 66 } | 65 } |
| 67 } else if (lengthAdjust < 0) { | 66 } else if (lengthAdjust < 0) { |
| 68 for (int i = 0; i < -lengthAdjust; i++) { | 67 for (int i = 0; i < -lengthAdjust; i++) { |
| 69 _subs.removeLast().cancel(); | 68 _observers.removeLast().close(); |
| 70 } | 69 } |
| 71 int len = _observers.length; | |
| 72 _observers.removeRange(len + lengthAdjust, len); | |
| 73 } | 70 } |
| 74 } | 71 } |
| 75 } | 72 } |
| OLD | NEW |