* Creates a wrapper function which ensures that provided callback will be * invoked only once during the specified delay period. * * @param {Function} callback - Function to be invoked after the delay period. * @param {number} delay - Delay after which to invoke callback. *
(callback, delay)
| 747 | * @returns {Function} |
| 748 | */ |
| 749 | function throttle (callback, delay) { |
| 750 | var leadingCall = false, trailingCall = false, lastCallTime = 0; |
| 751 | /** |
| 752 | * Invokes the original callback function and schedules new invocation if |
| 753 | * the "proxy" was called during current request. |
| 754 | * |
| 755 | * @returns {void} |
| 756 | */ |
| 757 | function resolvePending() { |
| 758 | if (leadingCall) { |
| 759 | leadingCall = false; |
| 760 | callback(); |
| 761 | } |
| 762 | if (trailingCall) { |
| 763 | proxy(); |
| 764 | } |
| 765 | } |
| 766 | /** |
| 767 | * Callback invoked after the specified delay. It will further postpone |
| 768 | * invocation of the original function delegating it to the |
| 769 | * requestAnimationFrame. |
| 770 | * |
| 771 | * @returns {void} |
| 772 | */ |
| 773 | function timeoutCallback() { |
| 774 | requestAnimationFrame$1(resolvePending); |
| 775 | } |
| 776 | /** |
| 777 | * Schedules invocation of the original function. |
| 778 | * |
| 779 | * @returns {void} |
| 780 | */ |
| 781 | function proxy() { |
| 782 | var timeStamp = Date.now(); |
| 783 | if (leadingCall) { |
| 784 | // Reject immediately following calls. |
| 785 | if (timeStamp - lastCallTime < trailingTimeout) { |
| 786 | return; |
| 787 | } |
| 788 | // Schedule new call to be in invoked when the pending one is resolved. |
| 789 | // This is important for "transitions" which never actually start |
| 790 | // immediately so there is a chance that we might miss one if change |
| 791 | // happens amids the pending invocation. |
| 792 | trailingCall = true; |
| 793 | } |
| 794 | else { |
| 795 | leadingCall = true; |
| 796 | trailingCall = false; |
| 797 | setTimeout(timeoutCallback, delay); |
| 798 | } |
| 799 | lastCallTime = timeStamp; |
| 800 | } |
| 801 | return proxy; |
| 802 | } |
| 803 | |
| 804 | // Minimum delay before invoking the update of observers. |
| 805 | var REFRESH_DELAY = 20; |
no outgoing calls
no test coverage detected