| 1 | /** |
| 2 | * Helper method that calls a specified callback before the browser has |
| 3 | * performed a specified number of repaints. |
| 4 | * |
| 5 | * Uses `requestAnimationFrame` under the hood to determine the next repaint. |
| 6 | * |
| 7 | * @param {Function} callback |
| 8 | * @param {number} frameCount The number of frames to wait before calling the |
| 9 | * specified callback. |
| 10 | */ |
| 11 | function deferUntilFrame( callback, frameCount ) { |
| 12 | if ( frameCount === 0 ) { |
| 13 | callback(); |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | requestAnimationFrame( () => { |
| 18 | deferUntilFrame( callback, frameCount - 1 ); |
| 19 | } ); |
| 20 | } |
| 21 | |
| 22 | module.exports = deferUntilFrame; |