chore: cleanup

This commit is contained in:
Nathan Walker
2020-07-06 14:25:11 -07:00
parent 94cf5c4706
commit 6336eac4b5
477 changed files with 59087 additions and 74961 deletions

View File

@@ -1,48 +1,48 @@
import * as definition from "./fps-native";
import * as definition from './fps-native';
export class FPSCallback implements definition.FPSCallback {
private impl: android.view.Choreographer.FrameCallback;
private onFrame: (currentTimeMillis: number) => void;
private impl: android.view.Choreographer.FrameCallback;
private onFrame: (currentTimeMillis: number) => void;
public running: boolean;
public running: boolean;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.running = false;
this.onFrame = onFrame;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.running = false;
this.onFrame = onFrame;
this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.handleFrame(nanos);
},
});
}
this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.handleFrame(nanos);
},
});
}
public start() {
if (this.running) {
return;
}
public start() {
if (this.running) {
return;
}
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
this.running = true;
}
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
this.running = true;
}
public stop() {
if (!this.running) {
return;
}
public stop() {
if (!this.running) {
return;
}
android.view.Choreographer.getInstance().removeFrameCallback(this.impl);
this.running = false;
}
android.view.Choreographer.getInstance().removeFrameCallback(this.impl);
this.running = false;
}
private handleFrame(nanos: number) {
if (!this.running) {
return;
}
private handleFrame(nanos: number) {
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
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
}
// 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
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
}
}

View File

@@ -2,23 +2,23 @@
* An utility class used to measure frames per second.
*/
export class FPSCallback {
/**
* Initializes a new instance of FPSCallback class.
*/
constructor(onFrame: (currentTimeMillis: number) => void);
/**
* Initializes a new instance of FPSCallback class.
*/
constructor(onFrame: (currentTimeMillis: number) => void);
/**
* Starts the frame per seconds measurement.
*/
start(): void;
/**
* Starts the frame per seconds measurement.
*/
start(): void;
/**
* Stops the frame per seconds measurement.
*/
stop(): void;
/**
* Stops the frame per seconds measurement.
*/
stop(): void;
/**
* Gets if the current instance of FPSCallback is running.
*/
running: boolean;
/**
* Gets if the current instance of FPSCallback is running.
*/
running: boolean;
}

View File

@@ -1,79 +1,70 @@
import * as definition from "./fps-native";
import * as definition from './fps-native';
class FrameHandlerImpl extends NSObject {
private _owner: WeakRef<FPSCallback>;
private _owner: WeakRef<FPSCallback>;
public static initWithOwner(owner: WeakRef<FPSCallback>): FrameHandlerImpl {
let handler = <FrameHandlerImpl>FrameHandlerImpl.new();
handler._owner = owner;
public static initWithOwner(owner: WeakRef<FPSCallback>): FrameHandlerImpl {
let handler = <FrameHandlerImpl>FrameHandlerImpl.new();
handler._owner = owner;
return handler;
}
return handler;
}
public handleFrame(sender: CADisplayLink): void {
let owner = this._owner.get();
if (owner) {
owner._handleFrame(sender);
}
}
public handleFrame(sender: CADisplayLink): void {
let owner = this._owner.get();
if (owner) {
owner._handleFrame(sender);
}
}
public static ObjCExposedMethods = {
handleFrame: { returns: interop.types.void, params: [CADisplayLink] },
};
public static ObjCExposedMethods = {
handleFrame: { returns: interop.types.void, params: [CADisplayLink] },
};
}
export class FPSCallback implements definition.FPSCallback {
public running: boolean;
private onFrame: Function;
private displayLink: CADisplayLink;
private impl: FrameHandlerImpl;
public running: boolean;
private onFrame: Function;
private displayLink: CADisplayLink;
private impl: FrameHandlerImpl;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.onFrame = onFrame;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.onFrame = onFrame;
this.impl = FrameHandlerImpl.initWithOwner(new WeakRef(this));
this.impl = FrameHandlerImpl.initWithOwner(new WeakRef(this));
this.displayLink = CADisplayLink.displayLinkWithTargetSelector(
this.impl,
"handleFrame"
);
this.displayLink.paused = true;
this.displayLink.addToRunLoopForMode(
NSRunLoop.currentRunLoop,
NSDefaultRunLoopMode
);
// UIScrollView (including in UIITableView) will run a loop in UITrackingRunLoopMode during scrolling.
// If we do not add the CADisplayLink in this mode, it would appear paused during scrolling.
this.displayLink.addToRunLoopForMode(
NSRunLoop.currentRunLoop,
UITrackingRunLoopMode
);
}
this.displayLink = CADisplayLink.displayLinkWithTargetSelector(this.impl, 'handleFrame');
this.displayLink.paused = true;
this.displayLink.addToRunLoopForMode(NSRunLoop.currentRunLoop, NSDefaultRunLoopMode);
// UIScrollView (including in UIITableView) will run a loop in UITrackingRunLoopMode during scrolling.
// If we do not add the CADisplayLink in this mode, it would appear paused during scrolling.
this.displayLink.addToRunLoopForMode(NSRunLoop.currentRunLoop, UITrackingRunLoopMode);
}
public start() {
if (this.running) {
return;
}
public start() {
if (this.running) {
return;
}
this.running = true;
this.displayLink.paused = false;
}
this.running = true;
this.displayLink.paused = false;
}
public stop() {
if (!this.running) {
return;
}
public stop() {
if (!this.running) {
return;
}
this.displayLink.paused = true;
this.running = false;
}
this.displayLink.paused = true;
this.running = false;
}
public _handleFrame(sender: CADisplayLink) {
if (!this.running) {
return;
}
public _handleFrame(sender: CADisplayLink) {
if (!this.running) {
return;
}
// timestamp is CFTimeInterval, which is in seconds, the onFrame callback expects millis, so multiply by 1000
this.onFrame(sender.timestamp * 1000);
}
// timestamp is CFTimeInterval, which is in seconds, the onFrame callback expects millis, so multiply by 1000
this.onFrame(sender.timestamp * 1000);
}
}

View File

@@ -16,9 +16,7 @@ export function running(): boolean;
/**
* Adds a callback function to be called each time FPS data is due to be reported. Returns an unique id which can be used to remove this callback later.
*/
export function addCallback(
callback: (fps: number, minFps?: number) => void
): number;
export function addCallback(callback: (fps: number, minFps?: number) => void): number;
/**
* Removes the callback with the specified id.

View File

@@ -1,4 +1,4 @@
import * as fpsNative from "./fps-native";
import * as fpsNative from './fps-native';
const callbacks = {};
let idCounter = 0;
@@ -7,89 +7,87 @@ let framesRendered = 0;
let frameStartTime = 0;
function doFrame(currentTimeMillis: number) {
let fps = 0;
if (frameStartTime > 0) {
// take the span in milliseconds
const timeSpan = currentTimeMillis - frameStartTime;
framesRendered++;
let fps = 0;
if (frameStartTime > 0) {
// take the span in milliseconds
const timeSpan = currentTimeMillis - frameStartTime;
framesRendered++;
if (timeSpan > 1000) {
fps = (framesRendered * 1000) / timeSpan;
if (fps < _minFps) {
_minFps = fps;
}
if (timeSpan > 1000) {
fps = (framesRendered * 1000) / timeSpan;
if (fps < _minFps) {
_minFps = fps;
}
notify(fps);
notify(fps);
frameStartTime = currentTimeMillis;
framesRendered = 0;
}
} else {
frameStartTime = currentTimeMillis;
}
frameStartTime = currentTimeMillis;
framesRendered = 0;
}
} else {
frameStartTime = currentTimeMillis;
}
}
let native: fpsNative.FPSCallback;
function ensureNative() {
if (!native) {
native = new fpsNative.FPSCallback(doFrame);
}
if (!native) {
native = new fpsNative.FPSCallback(doFrame);
}
}
export function reset() {
_minFps = 1000;
frameStartTime = 0;
framesRendered = 0;
_minFps = 1000;
frameStartTime = 0;
framesRendered = 0;
}
export function running(): boolean {
if (!native) {
return false;
}
if (!native) {
return false;
}
return native.running;
return native.running;
}
export function minFps(): number {
return _minFps;
return _minFps;
}
export function start() {
ensureNative();
native.start();
ensureNative();
native.start();
}
export function stop() {
if (!native) {
return;
}
if (!native) {
return;
}
native.stop();
reset();
native.stop();
reset();
}
export function addCallback(
callback: (fps: number, minFps?: number) => void
): number {
const id = idCounter;
export function addCallback(callback: (fps: number, minFps?: number) => void): number {
const id = idCounter;
// Wrap all calback in zonedCallback so that they work with the current zone.
callbacks[id] = zonedCallback(callback);
idCounter++;
// Wrap all calback in zonedCallback so that they work with the current zone.
callbacks[id] = zonedCallback(callback);
idCounter++;
return id;
return id;
}
export function removeCallback(id: number) {
if (id in callbacks) {
delete callbacks[id];
}
if (id in callbacks) {
delete callbacks[id];
}
}
function notify(fps) {
let callback: Function;
for (let id in callbacks) {
callback = callbacks[id];
callback(fps, _minFps);
}
let callback: Function;
for (let id in callbacks) {
callback = callbacks[id];
callback(fps, _minFps);
}
}