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 /** | 5 /** |
6 * This library is used to implement [Observable] types. | 6 * This library is used to implement [Observable] types. |
7 * | 7 * |
8 * It exposes lower level functionality such as [hasObservers], [observeReads] | 8 * It exposes lower level functionality such as [hasObservers], [observeReads] |
9 * [notifyChange] and [notifyRead]. | 9 * [notifyChange] and [notifyRead]. |
10 * | 10 * |
11 * Unless you are mixing in [Observable], it is usually better to write: | 11 * Unless you are mixing in [Observable], it is usually better to write: |
12 * | 12 * |
13 * import 'package:web_ui/observe.dart'; | 13 * import 'package:web_ui/observe.dart'; |
14 */ | 14 */ |
15 library web_ui.observe.observable; | 15 library web_ui.observe.observable; |
16 | 16 |
| 17 import 'dart:async'; |
17 import 'dart:collection' hide LinkedList; | 18 import 'dart:collection' hide LinkedList; |
18 import 'list.dart'; | 19 import 'list.dart'; |
19 import 'map.dart'; | 20 import 'map.dart'; |
20 import 'reference.dart'; | 21 import 'reference.dart'; |
21 import 'set.dart'; | 22 import 'set.dart'; |
22 import 'package:web_ui/src/utils_observe.dart' show setImmediate, hash3, hash4; | 23 import 'package:web_ui/src/utils_observe.dart' show hash3, hash4; |
23 import 'package:web_ui/src/linked_list.dart'; | 24 import 'package:web_ui/src/linked_list.dart'; |
24 | 25 |
25 /** | 26 /** |
26 * Use `@observable` to make a class observable. All fields in the class will | 27 * Use `@observable` to make a class observable. All fields in the class will |
27 * be transformed to track changes. The overhead will be minimal unless they are | 28 * be transformed to track changes. The overhead will be minimal unless they are |
28 * actually being observed. | 29 * actually being observed. |
29 */ | 30 */ |
30 const observable = const _ObservableAnnotation(); | 31 const observable = const _ObservableAnnotation(); |
31 | 32 |
32 /** Callback fired when an expression changes. */ | 33 /** Callback fired when an expression changes. */ |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 338 |
338 // If this is an assignment (and not insert/remove) then check if | 339 // If this is an assignment (and not insert/remove) then check if |
339 // the value actually changed. If not don't signal a change event. | 340 // the value actually changed. If not don't signal a change event. |
340 // This helps programmers avoid some common cases of cycles in their code. | 341 // This helps programmers avoid some common cases of cycles in their code. |
341 if ((type & (ChangeRecord.INSERT | ChangeRecord.REMOVE)) == 0) { | 342 if ((type & (ChangeRecord.INSERT | ChangeRecord.REMOVE)) == 0) { |
342 if (oldValue == newValue) return; | 343 if (oldValue == newValue) return; |
343 } | 344 } |
344 | 345 |
345 if (_changedObjects == null) { | 346 if (_changedObjects == null) { |
346 _changedObjects = []; | 347 _changedObjects = []; |
347 setImmediate(deliverChangesSync); | 348 runAsync(deliverChangesSync); |
348 } | 349 } |
349 if (self.$_changes == null) { | 350 if (self.$_changes == null) { |
350 self.$_changes = []; | 351 self.$_changes = []; |
351 _changedObjects.add(self); | 352 _changedObjects.add(self); |
352 } | 353 } |
353 self.$_changes.add(new ChangeRecord(type, key, oldValue, newValue)); | 354 self.$_changes.add(new ChangeRecord(type, key, oldValue, newValue)); |
354 } | 355 } |
355 | 356 |
356 // Optimizations to avoid extra work if observing const/final data. | 357 // Optimizations to avoid extra work if observing const/final data. |
357 void _doNothing() {} | 358 void _doNothing() {} |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 /** | 687 /** |
687 * The type of the `@observable` annotation. | 688 * The type of the `@observable` annotation. |
688 * | 689 * |
689 * Library private because you should be able to use the [observable] field | 690 * Library private because you should be able to use the [observable] field |
690 * to get the one and only instance. We could make it public though, if anyone | 691 * to get the one and only instance. We could make it public though, if anyone |
691 * needs it for some reason. | 692 * needs it for some reason. |
692 */ | 693 */ |
693 class _ObservableAnnotation { | 694 class _ObservableAnnotation { |
694 const _ObservableAnnotation(); | 695 const _ObservableAnnotation(); |
695 } | 696 } |
OLD | NEW |