| 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:html' show Text, MutationObserver; | 15 import 'dart:html' show Text, MutationObserver; |
| 15 import 'dart:collection' show Queue; | 16 import 'dart:collection' show Queue; |
| 16 import 'package:observe/src/microtask.dart' show performMicrotaskCheckpoint; | 17 import 'package:observe/src/microtask.dart' show performMicrotaskCheckpoint; |
| 17 | 18 |
| 18 void flush() { | 19 void flush() { |
| 19 endOfMicrotask(performMicrotaskCheckpoint); | 20 endOfMicrotask(performMicrotaskCheckpoint); |
| 20 } | 21 } |
| 21 | 22 |
| 22 int _iterations = 0; | 23 int _iterations = 0; |
| 23 final Queue _callbacks = new Queue(); | 24 final Queue _callbacks = new Queue(); |
| 24 final Text _twiddle = () { | 25 final Text _twiddle = () { |
| 25 var twiddle = new Text(''); | 26 var twiddle = new Text(''); |
| 26 new MutationObserver((x, y) { | 27 new MutationObserver((x, y) { |
| 27 while (_callbacks.isNotEmpty) { | 28 while (_callbacks.isNotEmpty) { |
| 28 try { | 29 try { |
| 29 _callbacks.removeFirst()(); | 30 _callbacks.removeFirst()(); |
| 30 } catch (e) { // Dart note: fire the error async. | 31 } catch (e) { // Dart note: fire the error async. |
| 31 new Completer().completeError(e); | 32 new Completer().completeError(e); |
| 32 } | 33 } |
| 33 } | 34 } |
| 34 }).observe(twiddle, characterData: true); | 35 }).observe(twiddle, characterData: true); |
| 35 return twiddle; | 36 return twiddle; |
| 36 }(); | 37 }(); |
| 37 | 38 |
| 38 void endOfMicrotask(void callback()) { | 39 void endOfMicrotask(void callback()) { |
| 39 _twiddle.text = '${_iterations++}'; | 40 _twiddle.text = '${_iterations++}'; |
| 40 _callbacks.add(callback); | 41 _callbacks.add(callback); |
| 41 } | 42 } |
| OLD | NEW |