| 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 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 typedef void _AsyncCallback(); | 7 typedef void _AsyncCallback(); |
| 8 | 8 |
| 9 class _AsyncCallbackEntry { | 9 class _AsyncCallbackEntry { |
| 10 final _AsyncCallback callback; | 10 final _AsyncCallback callback; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 132 } |
| 133 Zone.current.scheduleMicrotask( | 133 Zone.current.scheduleMicrotask( |
| 134 Zone.current.bindCallback(callback, runGuarded: true)); | 134 Zone.current.bindCallback(callback, runGuarded: true)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 class _AsyncRun { | 137 class _AsyncRun { |
| 138 /** Schedule the given callback before any other event in the event-loop. */ | 138 /** Schedule the given callback before any other event in the event-loop. */ |
| 139 static void _scheduleImmediate(void callback()) { | 139 static void _scheduleImmediate(void callback()) { |
| 140 scheduleImmediateClosure(callback); | 140 scheduleImmediateClosure(callback); |
| 141 } | 141 } |
| 142 |
| 143 static final Function scheduleImmediateClosure = |
| 144 _initializeScheduleImmediate(); |
| 145 |
| 146 static Function _initializeScheduleImmediate() { |
| 147 requiresPreamble(); |
| 148 if (JS('', 'self.scheduleImmediate') != null) { |
| 149 return _scheduleImmediateJsOverride; |
| 150 } |
| 151 if (JS('', 'self.MutationObserver') != null && |
| 152 JS('', 'self.document') != null) { |
| 153 // Use mutationObservers. |
| 154 var div = JS('', 'self.document.createElement("div")'); |
| 155 var span = JS('', 'self.document.createElement("span")'); |
| 156 var storedCallback; |
| 157 |
| 158 internalCallback(_) { |
| 159 leaveJsAsync(); |
| 160 var f = storedCallback; |
| 161 storedCallback = null; |
| 162 f(); |
| 163 }; |
| 164 |
| 165 var observer = JS('', 'new self.MutationObserver(#)', |
| 166 convertDartClosureToJS(internalCallback, 1)); |
| 167 JS('', '#.observe(#, { childList: true })', |
| 168 observer, div); |
| 169 |
| 170 return (void callback()) { |
| 171 assert(storedCallback == null); |
| 172 enterJsAsync(); |
| 173 storedCallback = callback; |
| 174 // Because of a broken shadow-dom polyfill we have to change the |
| 175 // children instead a cheap property. |
| 176 // See https://github.com/Polymer/ShadowDOM/issues/468 |
| 177 JS('', '#.firstChild ? #.removeChild(#): #.appendChild(#)', |
| 178 div, div, span, div, span); |
| 179 }; |
| 180 } else if (JS('', 'self.setImmediate') != null) { |
| 181 return _scheduleImmediateWithSetImmediate; |
| 182 } |
| 183 // TODO(20055): We should use DOM promises when available. |
| 184 return _scheduleImmediateWithTimer; |
| 185 } |
| 186 |
| 187 static void _scheduleImmediateJsOverride(void callback()) { |
| 188 internalCallback() { |
| 189 leaveJsAsync(); |
| 190 callback(); |
| 191 }; |
| 192 enterJsAsync(); |
| 193 JS('void', 'self.scheduleImmediate(#)', |
| 194 convertDartClosureToJS(internalCallback, 0)); |
| 195 } |
| 196 |
| 197 static void _scheduleImmediateWithSetImmediate(void callback()) { |
| 198 internalCallback() { |
| 199 leaveJsAsync(); |
| 200 callback(); |
| 201 }; |
| 202 enterJsAsync(); |
| 203 JS('void', 'self.setImmediate(#)', |
| 204 convertDartClosureToJS(internalCallback, 0)); |
| 205 } |
| 206 |
| 207 static void _scheduleImmediateWithTimer(void callback()) { |
| 208 Timer._createTimer(Duration.ZERO, callback); |
| 209 } |
| 142 } | 210 } |
| OLD | NEW |