OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library observe_utils; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'package:observe/observe.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 toSymbolMap(Map map) { |
| 12 var result = new ObservableMap.linked(); |
| 13 map.forEach((key, value) { |
| 14 if (value is Map) value = toSymbolMap(value); |
| 15 result[new Symbol(key)] = value; |
| 16 }); |
| 17 return result; |
| 18 } |
| 19 |
| 20 class FooBarModel extends ObservableBase { |
| 21 @observable var foo; |
| 22 @observable var bar; |
| 23 |
| 24 FooBarModel([this.foo, this.bar]); |
| 25 } |
| 26 |
| 27 class FooBarNotifyModel extends ChangeNotifierBase implements FooBarModel { |
| 28 var _foo; |
| 29 var _bar; |
| 30 |
| 31 FooBarNotifyModel([this._foo, this._bar]); |
| 32 |
| 33 get foo => _foo; |
| 34 set foo(value) { |
| 35 _foo = notifyPropertyChange(const Symbol('foo'), _foo, value); |
| 36 } |
| 37 |
| 38 get bar => _bar; |
| 39 set bar(value) { |
| 40 _bar = notifyPropertyChange(const Symbol('bar'), _bar, value); |
| 41 } |
| 42 } |
| 43 |
| 44 // TODO(jmesserly): this is a copy/paste from observe_test_utils.dart |
| 45 // Is it worth putting it in its own package, or in an existing one? |
| 46 |
| 47 void performMicrotaskCheckpoint() { |
| 48 Observable.dirtyCheck(); |
| 49 |
| 50 while (_pending.length > 0) { |
| 51 var pending = _pending; |
| 52 _pending = []; |
| 53 |
| 54 for (var callback in pending) { |
| 55 try { |
| 56 callback(); |
| 57 } catch (e, s) { |
| 58 new Completer().completeError(e, s); |
| 59 } |
| 60 } |
| 61 |
| 62 Observable.dirtyCheck(); |
| 63 } |
| 64 } |
| 65 |
| 66 List<Function> _pending = []; |
| 67 |
| 68 wrapMicrotask(void testCase()) { |
| 69 return () { |
| 70 runZonedExperimental(() { |
| 71 try { |
| 72 testCase(); |
| 73 } finally { |
| 74 performMicrotaskCheckpoint(); |
| 75 } |
| 76 }, onRunAsync: (callback) => _pending.add(callback)); |
| 77 }; |
| 78 } |
| 79 |
| 80 observeTest(name, testCase) => test(name, wrapMicrotask(testCase)); |
| 81 |
| 82 solo_observeTest(name, testCase) => solo_test(name, wrapMicrotask(testCase)); |
OLD | NEW |