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 * Exposes helper functionality for interacting with the platform. Similar to | 6 * Exposes helper functionality for interacting with the platform. Similar to |
7 * the [Platform] class from dart:html. | 7 * the [Platform] class from dart:html. |
8 */ | 8 */ |
9 // TODO(jmesserly): in Polymer this is a static class called "Platform", but | 9 // TODO(jmesserly): in Polymer this is a static class called "Platform", but |
10 // that conflicts with dart:html. What should we do? Does this functionality | 10 // that conflicts with dart:html. What should we do? Does this functionality |
11 // belong in html's Platform instead? | 11 // belong in html's Platform instead? |
12 library polymer.platform; | 12 library polymer.platform; |
13 | 13 |
14 import 'dart:async' show Completer; | 14 import 'dart:async' show Completer; |
15 import 'dart:html' show Text, MutationObserver; | 15 import 'dart:html' show Text, MutationObserver; |
16 import 'dart:collection' show Queue; | 16 import 'dart:collection' show Queue; |
17 import 'package:observe/src/microtask.dart' show performMicrotaskCheckpoint; | 17 import 'package:observe/observe.dart'; |
18 | 18 |
19 void flush() { | 19 void flush() => endOfMicrotask(Observable.dirtyCheck); |
20 endOfMicrotask(performMicrotaskCheckpoint); | |
21 } | |
22 | 20 |
23 int _iterations = 0; | 21 int _iterations = 0; |
24 final Queue _callbacks = new Queue(); | 22 final Queue _callbacks = new Queue(); |
25 final Text _twiddle = () { | 23 final Text _twiddle = () { |
26 var twiddle = new Text(''); | 24 var twiddle = new Text(''); |
27 new MutationObserver((x, y) { | 25 new MutationObserver((x, y) { |
28 while (_callbacks.isNotEmpty) { | 26 while (_callbacks.isNotEmpty) { |
29 try { | 27 try { |
30 _callbacks.removeFirst()(); | 28 _callbacks.removeFirst()(); |
31 } catch (e) { // Dart note: fire the error async. | 29 } catch (e) { // Dart note: fire the error async. |
32 new Completer().completeError(e); | 30 new Completer().completeError(e); |
33 } | 31 } |
34 } | 32 } |
35 }).observe(twiddle, characterData: true); | 33 }).observe(twiddle, characterData: true); |
36 return twiddle; | 34 return twiddle; |
37 }(); | 35 }(); |
38 | 36 |
39 void endOfMicrotask(void callback()) { | 37 void endOfMicrotask(void callback()) { |
40 _twiddle.text = '${_iterations++}'; | 38 _twiddle.text = '${_iterations++}'; |
41 _callbacks.add(callback); | 39 _callbacks.add(callback); |
42 } | 40 } |
OLD | NEW |