UNPKG

five-bells-visualization

Version:
69 lines (58 loc) 1.63 kB
<!-- @license Copyright (c) 2014 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as part of the polymer project is also subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt --> <script> Polymer.Async = (function() { var currVal = 0; var lastVal = 0; var callbacks = []; var twiddle = document.createTextNode(''); function runAsync(callback, waitTime) { if (waitTime > 0) { return ~setTimeout(callback, waitTime); } else { twiddle.textContent = currVal++; callbacks.push(callback); return currVal - 1; } } function cancelAsync(handle) { if (handle < 0) { clearTimeout(~handle); } else { var idx = handle - lastVal; if (idx >= 0) { if (!callbacks[idx]) { throw 'invalid async handle: ' + handle; } callbacks[idx] = null; } } } function atEndOfMicrotask() { var len = callbacks.length; for (var i=0; i<len; i++) { var cb = callbacks[i]; if (cb) { cb(); } } callbacks.splice(0, len); lastVal += len; } new (window.MutationObserver || JsMutationObserver)(atEndOfMicrotask) .observe(twiddle, {characterData: true}) ; // exports return { run: runAsync, cancel: cancelAsync }; })(); </script>