UNPKG

@nativescript/core

Version:

A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.

62 lines 1.98 kB
import { SDK_VERSION } from '../utils/constants'; export class FPSCallback { constructor(onFrame) { this.running = false; this.onFrame = onFrame; this.nativeFramesSupported = SDK_VERSION >= 24 && this._isNativeFramesSupported(); if (this.nativeFramesSupported) { this.impl = (nanos) => { this.handleFrame(nanos); }; } else { this.impl = new android.view.Choreographer.FrameCallback({ doFrame: (nanos) => { this.handleFrame(nanos); }, }); } } _isNativeFramesSupported() { return typeof global.__postFrameCallback === 'function' && typeof global.__removeFrameCallback === 'function'; } start() { if (this.running) { return; } if (this.nativeFramesSupported) { global.__postFrameCallback(this.impl); } else { android.view.Choreographer.getInstance().postFrameCallback(this.impl); } this.running = true; } stop() { if (!this.running) { return; } if (this.nativeFramesSupported) { global.__removeFrameCallback(this.impl); } else { android.view.Choreographer.getInstance().removeFrameCallback(this.impl); } this.running = false; } handleFrame(nanos) { if (!this.running) { return; } // divide by 1 000 000 since the parameter is in nanoseconds this.onFrame(nanos / 1000000); // add the FrameCallback instance again since it is automatically removed from the Choreographer if (this.nativeFramesSupported) { global.__postFrameCallback(this.impl); } else { android.view.Choreographer.getInstance().postFrameCallback(this.impl); } } } //# sourceMappingURL=fps-native.android.js.map