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.path_observer; | 5 library observe.src.path_observer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:math' show min; | 9 import 'dart:math' show min; |
10 @MirrorsUsed(metaTargets: const [Reflectable, ObservableProperty], | 10 @MirrorsUsed(metaTargets: const [Reflectable, ObservableProperty], |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 bool get _isOpen => _notifyCallback != null; | 499 bool get _isOpen => _notifyCallback != null; |
500 | 500 |
501 /// The number of arguments the subclass will pass to [_report]. | 501 /// The number of arguments the subclass will pass to [_report]. |
502 int get _reportArgumentCount; | 502 int get _reportArgumentCount; |
503 | 503 |
504 open(callback) { | 504 open(callback) { |
505 if (_isOpen || _isClosed) { | 505 if (_isOpen || _isClosed) { |
506 throw new StateError('Observer has already been opened.'); | 506 throw new StateError('Observer has already been opened.'); |
507 } | 507 } |
508 | 508 |
509 if (_minArgumentCount(callback) > _reportArgumentCount) { | 509 if (smoke.minArgs(callback) > _reportArgumentCount) { |
510 throw new ArgumentError('callback should take $_reportArgumentCount or ' | 510 throw new ArgumentError('callback should take $_reportArgumentCount or ' |
511 'fewer arguments'); | 511 'fewer arguments'); |
512 } | 512 } |
513 | 513 |
514 _notifyCallback = callback; | 514 _notifyCallback = callback; |
515 _notifyArgumentCount = min(_reportArgumentCount, | 515 _notifyArgumentCount = min(_reportArgumentCount, smoke.maxArgs(callback)); |
516 _maxArgumentCount(callback)); | |
517 | 516 |
518 _connect(); | 517 _connect(); |
519 return _value; | 518 return _value; |
520 } | 519 } |
521 | 520 |
522 get value { | 521 get value { |
523 _check(skipChanges: true); | 522 _check(skipChanges: true); |
524 return _value; | 523 return _value; |
525 } | 524 } |
526 | 525 |
(...skipping 26 matching lines...) Expand all Loading... | |
553 case 3: _notifyCallback(newValue, oldValue, extraArg); break; | 552 case 3: _notifyCallback(newValue, oldValue, extraArg); break; |
554 } | 553 } |
555 } catch (e, s) { | 554 } catch (e, s) { |
556 // Deliver errors async, so if a single callback fails it doesn't prevent | 555 // Deliver errors async, so if a single callback fails it doesn't prevent |
557 // other things from working. | 556 // other things from working. |
558 new Completer().completeError(e, s); | 557 new Completer().completeError(e, s); |
559 } | 558 } |
560 } | 559 } |
561 } | 560 } |
562 | 561 |
563 typedef _Func0(); | |
564 typedef _Func1(a); | |
565 typedef _Func2(a, b); | |
566 typedef _Func3(a, b, c); | |
567 | |
568 int _minArgumentCount(fn) { | |
Siggi Cherem (dart-lang)
2014/02/25 23:23:23
I forgot to delete this last time (now that smoke
| |
569 if (fn is _Func0) return 0; | |
570 if (fn is _Func1) return 1; | |
571 if (fn is _Func2) return 2; | |
572 if (fn is _Func3) return 3; | |
573 return 4; // at least 4 arguments are required. | |
574 } | |
575 | |
576 int _maxArgumentCount(fn) { | |
577 if (fn is _Func3) return 3; | |
578 if (fn is _Func2) return 2; | |
579 if (fn is _Func1) return 1; | |
580 if (fn is _Func0) return 0; | |
581 return -1; | |
582 } | |
583 | |
584 class _ObservedSet { | 562 class _ObservedSet { |
585 /// To prevent sequential [PathObserver]s and [CompoundObserver]s from | 563 /// To prevent sequential [PathObserver]s and [CompoundObserver]s from |
586 /// observing the same object, we check if they are observing the same root | 564 /// observing the same object, we check if they are observing the same root |
587 /// as the most recently created observer, and if so merge it into the | 565 /// as the most recently created observer, and if so merge it into the |
588 /// existing _ObservedSet. | 566 /// existing _ObservedSet. |
589 /// | 567 /// |
590 /// See <https://github.com/Polymer/observe-js/commit/f0990b1> and | 568 /// See <https://github.com/Polymer/observe-js/commit/f0990b1> and |
591 /// <https://codereview.appspot.com/46780044/>. | 569 /// <https://codereview.appspot.com/46780044/>. |
592 static _ObservedSet _lastSet; | 570 static _ObservedSet _lastSet; |
593 | 571 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
678 for (var observer in _observers.values.toList(growable: false)) { | 656 for (var observer in _observers.values.toList(growable: false)) { |
679 if (observer._isOpen) observer._check(); | 657 if (observer._isOpen) observer._check(); |
680 } | 658 } |
681 | 659 |
682 _resetNeeded = true; | 660 _resetNeeded = true; |
683 scheduleMicrotask(reset); | 661 scheduleMicrotask(reset); |
684 } | 662 } |
685 } | 663 } |
686 | 664 |
687 const int _MAX_DIRTY_CHECK_CYCLES = 1000; | 665 const int _MAX_DIRTY_CHECK_CYCLES = 1000; |
OLD | NEW |