UNPKG

1.76 kBJavaScriptView Raw
1import * as fpsNative from './fps-native';
2const callbacks = {};
3let idCounter = 0;
4let _minFps = 1000;
5let framesRendered = 0;
6let frameStartTime = 0;
7function doFrame(currentTimeMillis) {
8 let fps = 0;
9 if (frameStartTime > 0) {
10 // take the span in milliseconds
11 const timeSpan = currentTimeMillis - frameStartTime;
12 framesRendered++;
13 if (timeSpan > 1000) {
14 fps = (framesRendered * 1000) / timeSpan;
15 if (fps < _minFps) {
16 _minFps = fps;
17 }
18 notify(fps);
19 frameStartTime = currentTimeMillis;
20 framesRendered = 0;
21 }
22 }
23 else {
24 frameStartTime = currentTimeMillis;
25 }
26}
27let native;
28function ensureNative() {
29 if (!native) {
30 native = new fpsNative.FPSCallback(doFrame);
31 }
32}
33export function reset() {
34 _minFps = 1000;
35 frameStartTime = 0;
36 framesRendered = 0;
37}
38export function running() {
39 if (!native) {
40 return false;
41 }
42 return native.running;
43}
44export function minFps() {
45 return _minFps;
46}
47export function start() {
48 ensureNative();
49 native.start();
50}
51export function stop() {
52 if (!native) {
53 return;
54 }
55 native.stop();
56 reset();
57}
58export function addCallback(callback) {
59 const id = idCounter;
60 // Wrap all calback in zonedCallback so that they work with the current zone.
61 callbacks[id] = zonedCallback(callback);
62 idCounter++;
63 return id;
64}
65export function removeCallback(id) {
66 if (id in callbacks) {
67 delete callbacks[id];
68 }
69}
70function notify(fps) {
71 let callback;
72 for (const id in callbacks) {
73 callback = callbacks[id];
74 callback(fps, _minFps);
75 }
76}
77//# sourceMappingURL=index.js.map
\No newline at end of file