forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfps-native.ios.ts
More file actions
70 lines (55 loc) · 2.11 KB
/
fps-native.ios.ts
File metadata and controls
70 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import definition = require("fps-meter/fps-native");
class FrameHandlerImpl extends NSObject {
private _owner: WeakRef<FPSCallback>;
public static initWithOwner(owner: WeakRef<FPSCallback>): FrameHandlerImpl {
let handler = <FrameHandlerImpl>FrameHandlerImpl.new();
handler._owner = owner;
return handler;
}
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] }
};
}
export class FPSCallback implements definition.FPSCallback {
public running: boolean;
private onFrame: Function;
private displayLink: CADisplayLink;
private impl: FrameHandlerImpl;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.onFrame = onFrame;
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);
}
public start() {
if (this.running) {
return;
}
this.running = true;
this.displayLink.paused = false;
}
public stop() {
if (!this.running) {
return;
}
this.displayLink.paused = true;
this.running = false;
}
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);
}
}