animationFrames.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { Observable } from '../../Observable';
  2. import { performanceTimestampProvider } from '../../scheduler/performanceTimestampProvider';
  3. import { animationFrameProvider } from '../../scheduler/animationFrameProvider';
  4. export function animationFrames(timestampProvider) {
  5. return timestampProvider ? animationFramesFactory(timestampProvider) : DEFAULT_ANIMATION_FRAMES;
  6. }
  7. function animationFramesFactory(timestampProvider) {
  8. return new Observable((subscriber) => {
  9. const provider = timestampProvider || performanceTimestampProvider;
  10. const start = provider.now();
  11. let id = 0;
  12. const run = () => {
  13. if (!subscriber.closed) {
  14. id = animationFrameProvider.requestAnimationFrame((timestamp) => {
  15. id = 0;
  16. const now = provider.now();
  17. subscriber.next({
  18. timestamp: timestampProvider ? now : timestamp,
  19. elapsed: now - start,
  20. });
  21. run();
  22. });
  23. }
  24. };
  25. run();
  26. return () => {
  27. if (id) {
  28. animationFrameProvider.cancelAnimationFrame(id);
  29. }
  30. };
  31. });
  32. }
  33. const DEFAULT_ANIMATION_FRAMES = animationFramesFactory();
  34. //# sourceMappingURL=animationFrames.js.map