rxjs-zone-less
Version:
A set of wrappers for RxJS to avoid unnecessary change detection and zone interference in Angular.
34 lines (33 loc) • 1.62 kB
JavaScript
import { Subscription } from 'rxjs';
import { getZoneUnPatchedApi } from './zone-less';
const cancelAnimationFrame = getZoneUnPatchedApi('cancelAnimationFrame');
const requestAnimationFrame = getZoneUnPatchedApi('requestAnimationFrame');
export const animationFrameProvider = {
// When accessing the delegate, use the variable rather than `this` so that
// the functions can be called without being bound to the provider.
schedule(callback) {
let request = requestAnimationFrame;
let cancel = cancelAnimationFrame;
const { delegate } = animationFrameProvider;
if (delegate) {
request = delegate.requestAnimationFrame;
cancel = delegate.cancelAnimationFrame;
}
const handle = request((timestamp) => {
// Clear the cancel function. The request has been fulfilled, so
// attempting to cancel the request upon unsubscription would be
// pointless.
cancel = undefined;
callback(timestamp);
});
return new Subscription(() => cancel === null || cancel === void 0 ? void 0 : cancel(handle));
},
requestAnimationFrame(...args) {
const { delegate } = animationFrameProvider;
return ((delegate === null || delegate === void 0 ? void 0 : delegate.requestAnimationFrame) || requestAnimationFrame)(...args);
},
cancelAnimationFrame(...args) {
const { delegate } = animationFrameProvider;
return ((delegate === null || delegate === void 0 ? void 0 : delegate.cancelAnimationFrame) || cancelAnimationFrame)(...args);
},
};