rxjs-zone-less
Version:
A set of wrappers for RxJS to avoid unnecessary change detection and zone interference in Angular.
40 lines (39 loc) • 1.96 kB
JavaScript
import { animationFrameProvider } from '../../internals/animationFrameProvider';
import { AsyncAction } from '../async/AsyncAction';
export class AnimationFrameAction extends AsyncAction {
constructor(scheduler, work) {
super(scheduler, work);
this.scheduler = scheduler;
this.work = work;
}
requestAsyncId(scheduler, id, delay = 0) {
// If delay is greater than 0, request as an async action.
if (delay !== null && delay > 0) {
return super.requestAsyncId(scheduler, id, delay);
}
// Push the action to the end of the scheduler queue.
scheduler.actions.push(this);
// If an animation frame has already been requested, don't request another
// one. If an animation frame hasn't been requested yet, request one. Return
// the current animation frame request id.
return (scheduler._scheduled ||
(scheduler._scheduled = animationFrameProvider.requestAnimationFrame(() => scheduler.flush(undefined))));
}
recycleAsyncId(scheduler, id, delay = 0) {
// If delay exists and is greater than 0, or if the delay is null (the
// action wasn't rescheduled) but was originally scheduled as an async
// action, then recycle as an async action.
if ((delay != null && delay > 0) || (delay == null && this.delay > 0)) {
return super.recycleAsyncId(scheduler, id, delay);
}
// If the scheduler queue is empty, cancel the requested animation frame and
// set the scheduled flag to undefined so the next AnimationFrameAction will
// request its own.
if (!scheduler.actions.some((action) => action.id === id)) {
animationFrameProvider.cancelAnimationFrame(id);
scheduler._scheduled = undefined;
}
// Return undefined so the action knows to request a new async id if it's rescheduled.
return undefined;
}
}