| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2017 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 <link rel="import" href="boot.html"> |
| 11 |
| 12 <script> |
| 13 (function() { |
| 14 |
| 15 'use strict'; |
| 16 |
| 17 /** @typedef {{run: function(function(), number=):number, cancel: function(num
ber)}} */ |
| 18 let AsyncInterface; // eslint-disable-line no-unused-vars |
| 19 |
| 20 // Microtask implemented using Mutation Observer |
| 21 let microtaskCurrHandle = 0; |
| 22 let microtaskLastHandle = 0; |
| 23 let microtaskCallbacks = []; |
| 24 let microtaskNodeContent = 0; |
| 25 let microtaskNode = document.createTextNode(''); |
| 26 new window.MutationObserver(microtaskFlush).observe(microtaskNode, {characterD
ata: true}); |
| 27 |
| 28 function microtaskFlush() { |
| 29 const len = microtaskCallbacks.length; |
| 30 for (let i = 0; i < len; i++) { |
| 31 let cb = microtaskCallbacks[i]; |
| 32 if (cb) { |
| 33 try { |
| 34 cb(); |
| 35 } catch (e) { |
| 36 setTimeout(() => { throw e }); |
| 37 } |
| 38 } |
| 39 } |
| 40 microtaskCallbacks.splice(0, len); |
| 41 microtaskLastHandle += len; |
| 42 } |
| 43 |
| 44 /** |
| 45 * Module that provides a number of strategies for enqueuing asynchronous |
| 46 * tasks. Each sub-module provides a standard `run(fn)` interface that return
s a |
| 47 * handle, and a `cancel(handle)` interface for canceling async tasks before |
| 48 * they run. |
| 49 * |
| 50 * @namespace |
| 51 * @memberof Polymer |
| 52 * @summary Module that provides a number of strategies for enqueuing asynchro
nous |
| 53 * tasks. |
| 54 */ |
| 55 Polymer.Async = { |
| 56 |
| 57 /** |
| 58 * Async interface wrapper around `setTimeout`. |
| 59 * |
| 60 * @namespace |
| 61 * @memberof Polymer.Async |
| 62 * @summary Async interface wrapper around `setTimeout`. |
| 63 */ |
| 64 timeOut: { |
| 65 /** |
| 66 * Returns a sub-module with the async interface providing the provided |
| 67 * delay. |
| 68 * |
| 69 * @memberof Polymer.Async.timeOut |
| 70 * @param {number} delay Time to wait before calling callbacks in ms |
| 71 * @return {AsyncInterface} An async timeout interface |
| 72 */ |
| 73 after(delay) { |
| 74 return { |
| 75 run(fn) { return setTimeout(fn, delay) }, |
| 76 cancel: window.clearTimeout.bind(window) |
| 77 } |
| 78 }, |
| 79 /** |
| 80 * Enqueues a function called in the next task. |
| 81 * |
| 82 * @memberof Polymer.Async.timeOut |
| 83 * @param {Function} fn Callback to run |
| 84 * @return {number} Handle used for canceling task |
| 85 */ |
| 86 run: window.setTimeout.bind(window), |
| 87 /** |
| 88 * Cancels a previously enqueued `timeOut` callback. |
| 89 * |
| 90 * @memberof Polymer.Async.timeOut |
| 91 * @param {number} handle Handle returned from `run` of callback to cancel |
| 92 */ |
| 93 cancel: window.clearTimeout.bind(window) |
| 94 }, |
| 95 |
| 96 /** |
| 97 * Async interface wrapper around `requestAnimationFrame`. |
| 98 * |
| 99 * @namespace |
| 100 * @memberof Polymer.Async |
| 101 * @summary Async interface wrapper around `requestAnimationFrame`. |
| 102 */ |
| 103 animationFrame: { |
| 104 /** |
| 105 * Enqueues a function called at `requestAnimationFrame` timing. |
| 106 * |
| 107 * @memberof Polymer.Async.animationFrame |
| 108 * @param {Function} fn Callback to run |
| 109 * @return {number} Handle used for canceling task |
| 110 */ |
| 111 run: window.requestAnimationFrame.bind(window), |
| 112 /** |
| 113 * Cancels a previously enqueued `animationFrame` callback. |
| 114 * |
| 115 * @memberof Polymer.Async.timeOut |
| 116 * @param {number} handle Handle returned from `run` of callback to cancel |
| 117 */ |
| 118 cancel: window.cancelAnimationFrame.bind(window) |
| 119 }, |
| 120 |
| 121 /** |
| 122 * Async interface wrapper around `requestIdleCallback`. Falls back to |
| 123 * `setTimeout` on browsers that do not support `requestIdleCallback`. |
| 124 * |
| 125 * @namespace |
| 126 * @memberof Polymer.Async |
| 127 * @summary Async interface wrapper around `requestIdleCallback`. |
| 128 */ |
| 129 idlePeriod: { |
| 130 /** |
| 131 * Enqueues a function called at `requestIdleCallback` timing. |
| 132 * |
| 133 * @memberof Polymer.Async.idlePeriod |
| 134 * @param {function(IdleDeadline)} fn Callback to run |
| 135 * @return {number} Handle used for canceling task |
| 136 */ |
| 137 run(fn) { |
| 138 return window.requestIdleCallback ? |
| 139 window.requestIdleCallback(fn) : |
| 140 window.setTimeout(fn, 16); |
| 141 }, |
| 142 /** |
| 143 * Cancels a previously enqueued `idlePeriod` callback. |
| 144 * |
| 145 * @memberof Polymer.Async.idlePeriod |
| 146 * @param {number} handle Handle returned from `run` of callback to cancel |
| 147 */ |
| 148 cancel(handle) { |
| 149 window.cancelIdleCallback ? |
| 150 window.cancelIdleCallback(handle) : |
| 151 window.clearTimeout(handle); |
| 152 } |
| 153 }, |
| 154 |
| 155 /** |
| 156 * Async interface for enqueueing callbacks that run at microtask timing. |
| 157 * |
| 158 * Note that microtask timing is achieved via a single `MutationObserver`, |
| 159 * and thus callbacks enqueued with this API will all run in a single |
| 160 * batch, and not interleaved with other microtasks such as promises. |
| 161 * Promises are avoided as an implementation choice for the time being |
| 162 * due to Safari bugs that cause Promises to lack microtask guarantees. |
| 163 * |
| 164 * @namespace |
| 165 * @memberof Polymer.Async |
| 166 * @summary Async interface for enqueueing callbacks that run at microtask |
| 167 * timing. |
| 168 */ |
| 169 microTask: { |
| 170 |
| 171 /** |
| 172 * Enqueues a function called at microtask timing. |
| 173 * |
| 174 * @memberof Polymer.Async.microTask |
| 175 * @param {Function} callback Callback to run |
| 176 * @return {*} Handle used for canceling task |
| 177 */ |
| 178 run(callback) { |
| 179 microtaskNode.textContent = microtaskNodeContent++; |
| 180 microtaskCallbacks.push(callback); |
| 181 return microtaskCurrHandle++; |
| 182 }, |
| 183 |
| 184 /** |
| 185 * Cancels a previously enqueued `microTask` callback. |
| 186 * |
| 187 * @memberof Polymer.Async.microTask |
| 188 * @param {number} handle Handle returned from `run` of callback to cancel |
| 189 */ |
| 190 cancel(handle) { |
| 191 const idx = handle - microtaskLastHandle; |
| 192 if (idx >= 0) { |
| 193 if (!microtaskCallbacks[idx]) { |
| 194 throw new Error('invalid async handle: ' + handle); |
| 195 } |
| 196 microtaskCallbacks[idx] = null; |
| 197 } |
| 198 } |
| 199 |
| 200 } |
| 201 }; |
| 202 |
| 203 })(); |
| 204 </script> |
| OLD | NEW |