@vnejs/plugins.controllers.debug.fps 0.0.0-stage → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -0
- package/dist/modules/controller.d.ts +12 -0
- package/dist/modules/controller.js +10 -0
- package/dist/modules/fps.d.ts +18 -0
- package/dist/modules/fps.js +46 -0
- package/dist/modules/view.d.ts +9 -0
- package/dist/modules/view.js +8 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/view/index.d.ts +3 -0
- package/dist/view/index.js +7 -0
- package/package.json +50 -4
- package/src/index.ts +11 -0
- package/src/modules/controller.ts +15 -0
- package/src/modules/fps.ts +64 -0
- package/src/modules/view.ts +15 -0
- package/src/tests/controller.test.ts +37 -0
- package/src/tests/fps.test.ts +65 -0
- package/src/tests/setup.ts +35 -0
- package/src/types.ts +22 -0
- package/src/view/index.tsx +30 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@vnejs/contracts.controllers.debug.fps";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import "@vnejs/contracts.controllers.debug.fps";
|
|
2
|
+
import { regPlugin } from "@vnejs/registry";
|
|
3
|
+
import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.debug.fps";
|
|
4
|
+
import { FpsController } from "./modules/controller.js";
|
|
5
|
+
import { Fps } from "./modules/fps.js";
|
|
6
|
+
import { FpsView } from "./modules/view.js";
|
|
7
|
+
regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [FpsController, Fps, FpsView]);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
3
|
+
import type { FpsPluginState } from "../types.js";
|
|
4
|
+
export declare class FpsController extends ModuleController<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams, FpsPluginState> {
|
|
5
|
+
name: string;
|
|
6
|
+
EVENT_UPDATE_VIEW: "vne:fps:update";
|
|
7
|
+
EVENT_UPDATE_STATE: "vne:fps:update_state";
|
|
8
|
+
EVENT_UPDATE_FAST: "vne:fps:update_fast";
|
|
9
|
+
EVENT_UPDATE_FORCE: "vne:fps:update_force";
|
|
10
|
+
EVENT_SHOW: "vne:fps:show";
|
|
11
|
+
EVENT_HIDE: "vne:fps:hide";
|
|
12
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
export class FpsController extends ModuleController {
|
|
3
|
+
name = "fps.controller";
|
|
4
|
+
EVENT_UPDATE_VIEW = this.EVENTS.FPS.UPDATE;
|
|
5
|
+
EVENT_UPDATE_STATE = this.EVENTS.FPS.UPDATE_STATE;
|
|
6
|
+
EVENT_UPDATE_FAST = this.EVENTS.FPS.UPDATE_FAST;
|
|
7
|
+
EVENT_UPDATE_FORCE = this.EVENTS.FPS.UPDATE_FORCE;
|
|
8
|
+
EVENT_SHOW = this.EVENTS.FPS.SHOW;
|
|
9
|
+
EVENT_HIDE = this.EVENTS.FPS.HIDE;
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { SettingsChangedResult } from "@vnejs/module.core";
|
|
3
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
4
|
+
export declare class Fps extends ModuleCore<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams> {
|
|
5
|
+
name: string;
|
|
6
|
+
fpsSum: number;
|
|
7
|
+
fpsCount: number;
|
|
8
|
+
lastTs: number;
|
|
9
|
+
oldValue: number | undefined;
|
|
10
|
+
raf: number | null;
|
|
11
|
+
subscribe: () => void;
|
|
12
|
+
setup: () => Promise<import("@vnejs/registry").EmitResults<"vne:settings:init">> | undefined;
|
|
13
|
+
start: () => Promise<import("@vnejs/registry").EmitResults<"vne:fps:show">> | undefined;
|
|
14
|
+
onSettingChange: ({ name, value }: SettingsChangedResult) => false | Promise<import("@vnejs/registry").EmitResults<"vne:fps:show">> | undefined;
|
|
15
|
+
setEnabled: (value: boolean) => Promise<import("@vnejs/registry").EmitResults<"vne:fps:show">> | undefined;
|
|
16
|
+
recalcFps: () => void;
|
|
17
|
+
unsetValues: (value?: number) => void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class Fps extends ModuleCore {
|
|
3
|
+
name = "fps";
|
|
4
|
+
fpsSum = 0;
|
|
5
|
+
fpsCount = 0;
|
|
6
|
+
lastTs = 0;
|
|
7
|
+
oldValue = -1;
|
|
8
|
+
raf = null;
|
|
9
|
+
subscribe = () => {
|
|
10
|
+
this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingChange);
|
|
11
|
+
};
|
|
12
|
+
setup = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FPS.ENABLED, value: false });
|
|
13
|
+
start = () => this.setEnabled(Boolean(this.shared.settings[this.SETTINGS.FPS.ENABLED]));
|
|
14
|
+
onSettingChange = ({ name, value }) => name === this.SETTINGS.FPS.ENABLED && this.setEnabled(Boolean(value));
|
|
15
|
+
setEnabled = (value) => {
|
|
16
|
+
if (value) {
|
|
17
|
+
this.lastTs = new Date().getTime();
|
|
18
|
+
this.unsetValues();
|
|
19
|
+
this.raf = requestAnimationFrame(this.recalcFps);
|
|
20
|
+
return this.emit(this.EVENTS.FPS.SHOW);
|
|
21
|
+
}
|
|
22
|
+
if (this.raf !== null)
|
|
23
|
+
cancelAnimationFrame(this.raf);
|
|
24
|
+
this.emit(this.EVENTS.FPS.HIDE);
|
|
25
|
+
};
|
|
26
|
+
recalcFps = () => {
|
|
27
|
+
const ts = new Date().getTime();
|
|
28
|
+
this.fpsSum += ts - this.lastTs;
|
|
29
|
+
this.fpsCount++;
|
|
30
|
+
this.lastTs = ts;
|
|
31
|
+
this.raf = requestAnimationFrame(this.recalcFps);
|
|
32
|
+
if (this.fpsSum < 200)
|
|
33
|
+
return;
|
|
34
|
+
const value = Math.round((1000 * this.fpsCount) / this.fpsSum);
|
|
35
|
+
if (this.oldValue !== value) {
|
|
36
|
+
this.emit(this.EVENTS.FPS.UPDATE_STATE, { text: value });
|
|
37
|
+
this.emit(this.EVENTS.FPS.UPDATE_FAST);
|
|
38
|
+
}
|
|
39
|
+
this.unsetValues(value);
|
|
40
|
+
};
|
|
41
|
+
unsetValues = (value) => {
|
|
42
|
+
this.fpsSum = 0;
|
|
43
|
+
this.fpsCount = 0;
|
|
44
|
+
this.oldValue = value;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
3
|
+
import type { FpsPluginState } from "../types.js";
|
|
4
|
+
export declare class FpsView extends ModuleView<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams, FpsPluginState> {
|
|
5
|
+
name: string;
|
|
6
|
+
EVENT_UPDATE_VIEW: "vne:fps:update";
|
|
7
|
+
TRANSITION: number;
|
|
8
|
+
renderFunc: import("@vnejs/module.components").ViewRenderFunc<FpsPluginState>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
import { render } from "../view/index.js";
|
|
3
|
+
export class FpsView extends ModuleView {
|
|
4
|
+
name = "fps.view";
|
|
5
|
+
EVENT_UPDATE_VIEW = this.EVENTS.FPS.UPDATE;
|
|
6
|
+
TRANSITION = this.PARAMS.FPS.TRANSITION;
|
|
7
|
+
renderFunc = render;
|
|
8
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
import type { Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/contracts.controllers.debug.fps";
|
|
3
|
+
import type { PluginName as SettingsPluginName, SubscribeEvents as SettingsSubscribeEvents } from "@vnejs/contracts.core.settings";
|
|
4
|
+
import type { PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/contracts.core.system";
|
|
5
|
+
export type FpsPluginEvents = ModuleComponentsEvents & Record<PluginName, SubscribeEvents> & Record<SettingsPluginName, SettingsSubscribeEvents> & Record<SystemPluginName, SystemSubscribeEvents>;
|
|
6
|
+
export type FpsPluginConstants = ModuleComponentsConstants;
|
|
7
|
+
export type FpsPluginSettings = ModuleComponentsSettings & Record<PluginName, SettingsKeys>;
|
|
8
|
+
export type FpsPluginParams = ModuleComponentsParams & Record<PluginName, Params>;
|
|
9
|
+
export type FpsPluginState = {
|
|
10
|
+
isShow: boolean;
|
|
11
|
+
isForce: boolean;
|
|
12
|
+
text?: string | number;
|
|
13
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createRenderFunc, Text, View, Wrap, useStoreState } from "@vnejs/uis.react";
|
|
3
|
+
const FPS = ({ store, onMount, PARAMS }) => {
|
|
4
|
+
const { isShow = false, isForce = false, text = "" } = useStoreState(store, onMount);
|
|
5
|
+
return (_jsx(View, { ...PARAMS.FPS.VIEW_PROPS.view, isShow: isShow, isForce: isForce, children: _jsx(Wrap, { ...PARAMS.FPS.VIEW_PROPS.wrap, children: _jsx(Text, { ...PARAMS.FPS.VIEW_PROPS.text, text: text }) }) }));
|
|
6
|
+
};
|
|
7
|
+
export const render = createRenderFunc(FPS);
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.controllers.debug.fps",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.14",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "npx @vnejs/monorepo test",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@vnejs/contracts.controllers.debug.fps": "~0.2.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/module.core": "~0.2.0",
|
|
37
|
+
"@vnejs/module.components": "~0.2.0",
|
|
38
|
+
"@vnejs/registry": "~0.2.0",
|
|
39
|
+
"@vnejs/uis.react": "~0.2.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@vnejs/configs.ts-common": "~0.2.0",
|
|
43
|
+
"@vnejs/configs.vitest": "~0.2.0",
|
|
44
|
+
"@vnejs/test-utils": "~0.2.0",
|
|
45
|
+
"@vnejs/contracts.controllers.debug.fps": "~0.2.0",
|
|
46
|
+
"@vnejs/module.core": "~0.2.0",
|
|
47
|
+
"@vnejs/module.components": "~0.2.0",
|
|
48
|
+
"@vnejs/registry": "~0.2.0",
|
|
49
|
+
"@vnejs/sources-set": "~0.2.0",
|
|
50
|
+
"@vnejs/uis.react": "~0.2.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "@vnejs/contracts.controllers.debug.fps";
|
|
2
|
+
|
|
3
|
+
import { regPlugin } from "@vnejs/registry";
|
|
4
|
+
|
|
5
|
+
import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.debug.fps";
|
|
6
|
+
|
|
7
|
+
import { FpsController } from "./modules/controller.js";
|
|
8
|
+
import { Fps } from "./modules/fps.js";
|
|
9
|
+
import { FpsView } from "./modules/view.js";
|
|
10
|
+
|
|
11
|
+
regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [FpsController, Fps, FpsView]);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
4
|
+
import type { FpsPluginState } from "../types.js";
|
|
5
|
+
|
|
6
|
+
export class FpsController extends ModuleController<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams, FpsPluginState> {
|
|
7
|
+
name = "fps.controller";
|
|
8
|
+
|
|
9
|
+
EVENT_UPDATE_VIEW = this.EVENTS.FPS.UPDATE;
|
|
10
|
+
EVENT_UPDATE_STATE = this.EVENTS.FPS.UPDATE_STATE;
|
|
11
|
+
EVENT_UPDATE_FAST = this.EVENTS.FPS.UPDATE_FAST;
|
|
12
|
+
EVENT_UPDATE_FORCE = this.EVENTS.FPS.UPDATE_FORCE;
|
|
13
|
+
EVENT_SHOW = this.EVENTS.FPS.SHOW;
|
|
14
|
+
EVENT_HIDE = this.EVENTS.FPS.HIDE;
|
|
15
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
|
|
3
|
+
import type { SettingsChangedResult } from "@vnejs/module.core";
|
|
4
|
+
|
|
5
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
6
|
+
|
|
7
|
+
export class Fps extends ModuleCore<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams> {
|
|
8
|
+
name = "fps";
|
|
9
|
+
|
|
10
|
+
fpsSum = 0;
|
|
11
|
+
fpsCount = 0;
|
|
12
|
+
lastTs = 0;
|
|
13
|
+
oldValue: number | undefined = -1;
|
|
14
|
+
raf: number | null = null;
|
|
15
|
+
|
|
16
|
+
subscribe = () => {
|
|
17
|
+
this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingChange);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
setup = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FPS.ENABLED, value: false });
|
|
21
|
+
|
|
22
|
+
start = () => this.setEnabled(Boolean(this.shared.settings[this.SETTINGS.FPS.ENABLED]));
|
|
23
|
+
|
|
24
|
+
onSettingChange = ({ name, value }: SettingsChangedResult) => name === this.SETTINGS.FPS.ENABLED && this.setEnabled(Boolean(value));
|
|
25
|
+
|
|
26
|
+
setEnabled = (value: boolean) => {
|
|
27
|
+
if (value) {
|
|
28
|
+
this.lastTs = new Date().getTime();
|
|
29
|
+
this.unsetValues();
|
|
30
|
+
this.raf = requestAnimationFrame(this.recalcFps);
|
|
31
|
+
|
|
32
|
+
return this.emit(this.EVENTS.FPS.SHOW);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (this.raf !== null) cancelAnimationFrame(this.raf);
|
|
36
|
+
this.emit(this.EVENTS.FPS.HIDE);
|
|
37
|
+
};
|
|
38
|
+
recalcFps = () => {
|
|
39
|
+
const ts = new Date().getTime();
|
|
40
|
+
|
|
41
|
+
this.fpsSum += ts - this.lastTs;
|
|
42
|
+
this.fpsCount++;
|
|
43
|
+
|
|
44
|
+
this.lastTs = ts;
|
|
45
|
+
|
|
46
|
+
this.raf = requestAnimationFrame(this.recalcFps);
|
|
47
|
+
|
|
48
|
+
if (this.fpsSum < 200) return;
|
|
49
|
+
|
|
50
|
+
const value = Math.round((1000 * this.fpsCount) / this.fpsSum);
|
|
51
|
+
|
|
52
|
+
if (this.oldValue !== value) {
|
|
53
|
+
this.emit(this.EVENTS.FPS.UPDATE_STATE, { text: value });
|
|
54
|
+
this.emit(this.EVENTS.FPS.UPDATE_FAST);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.unsetValues(value);
|
|
58
|
+
};
|
|
59
|
+
unsetValues = (value?: number) => {
|
|
60
|
+
this.fpsSum = 0;
|
|
61
|
+
this.fpsCount = 0;
|
|
62
|
+
this.oldValue = value;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
4
|
+
import type { FpsPluginState } from "../types.js";
|
|
5
|
+
|
|
6
|
+
import { render } from "../view/index.js";
|
|
7
|
+
export class FpsView extends ModuleView<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams, FpsPluginState> {
|
|
8
|
+
name = "fps.view";
|
|
9
|
+
|
|
10
|
+
EVENT_UPDATE_VIEW = this.EVENTS.FPS.UPDATE;
|
|
11
|
+
|
|
12
|
+
TRANSITION = this.PARAMS.FPS.TRANSITION;
|
|
13
|
+
|
|
14
|
+
renderFunc = render;
|
|
15
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { FpsController } from "../modules/controller.js";
|
|
4
|
+
|
|
5
|
+
import { createFpsTestModule, registerFpsPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
|
|
6
|
+
|
|
7
|
+
describe("FpsController", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
registerFpsPluginVne();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("UPDATE_STATE updates text in state", async () => {
|
|
13
|
+
const { module, observer } = createFpsTestModule(FpsController);
|
|
14
|
+
|
|
15
|
+
await observer.emit(SUBSCRIBE_EVENTS.UPDATE_STATE, { text: 60 });
|
|
16
|
+
|
|
17
|
+
expect((module as FpsController).state.text).toBe(60);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("SHOW sets isShow true", async () => {
|
|
21
|
+
const { module, observer } = createFpsTestModule(FpsController);
|
|
22
|
+
|
|
23
|
+
await observer.emit(SUBSCRIBE_EVENTS.SHOW);
|
|
24
|
+
|
|
25
|
+
expect((module as FpsController).state.isShow).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("HIDE sets isShow false", async () => {
|
|
29
|
+
const { module, observer } = createFpsTestModule(FpsController, {
|
|
30
|
+
state: { "fps.controller": { isShow: true } },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await observer.emit(SUBSCRIBE_EVENTS.HIDE);
|
|
34
|
+
|
|
35
|
+
expect((module as FpsController).state.isShow).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { spyEvent } from "@vnejs/test-utils";
|
|
4
|
+
|
|
5
|
+
import { Fps } from "../modules/fps.js";
|
|
6
|
+
|
|
7
|
+
import { createFpsTestModule, registerFpsPluginVne, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "./setup.js";
|
|
8
|
+
|
|
9
|
+
describe("Fps", () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
registerFpsPluginVne();
|
|
12
|
+
vi.stubGlobal(
|
|
13
|
+
"requestAnimationFrame",
|
|
14
|
+
vi.fn(() => 1),
|
|
15
|
+
);
|
|
16
|
+
vi.stubGlobal("cancelAnimationFrame", vi.fn());
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("setup registers enabled setting as false", async () => {
|
|
20
|
+
const { module } = createFpsTestModule(Fps, { subscribe: false });
|
|
21
|
+
const emit = vi.spyOn((module as Fps).observer!, "emit");
|
|
22
|
+
|
|
23
|
+
await (module as Fps).setup();
|
|
24
|
+
|
|
25
|
+
expect(emit).toHaveBeenCalledWith("vne:settings:init", { name: SETTINGS_KEYS.ENABLED, value: false }, { module: "fps" });
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("setEnabled true emits SHOW and starts rAF", () => {
|
|
29
|
+
const { module, observer } = createFpsTestModule(Fps, { subscribe: false });
|
|
30
|
+
const show = spyEvent(observer, SUBSCRIBE_EVENTS.SHOW);
|
|
31
|
+
|
|
32
|
+
(module as Fps).setEnabled(true);
|
|
33
|
+
|
|
34
|
+
expect(show).toHaveBeenCalledOnce();
|
|
35
|
+
expect(requestAnimationFrame).toHaveBeenCalled();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("setEnabled false emits HIDE and cancels rAF", () => {
|
|
39
|
+
const { module, observer } = createFpsTestModule(Fps, { subscribe: false });
|
|
40
|
+
const hide = spyEvent(observer, SUBSCRIBE_EVENTS.HIDE);
|
|
41
|
+
|
|
42
|
+
(module as Fps).setEnabled(true);
|
|
43
|
+
(module as Fps).setEnabled(false);
|
|
44
|
+
|
|
45
|
+
expect(hide).toHaveBeenCalledOnce();
|
|
46
|
+
expect(cancelAnimationFrame).toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("recalcFps emits UPDATE_STATE and UPDATE_FAST when value changes", () => {
|
|
50
|
+
const { module, observer } = createFpsTestModule(Fps, { subscribe: false });
|
|
51
|
+
const updateState = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
|
|
52
|
+
const updateFast = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_FAST);
|
|
53
|
+
const fps = module as Fps;
|
|
54
|
+
|
|
55
|
+
vi.useFakeTimers();
|
|
56
|
+
vi.setSystemTime(0);
|
|
57
|
+
fps.setEnabled(true);
|
|
58
|
+
vi.setSystemTime(200);
|
|
59
|
+
fps.recalcFps();
|
|
60
|
+
vi.useRealTimers();
|
|
61
|
+
|
|
62
|
+
expect(updateState).toHaveBeenCalledWith({ text: expect.any(Number) });
|
|
63
|
+
expect(updateFast).toHaveBeenCalledOnce();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SourcesSet } from "@vnejs/sources-set";
|
|
2
|
+
import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
|
|
3
|
+
|
|
4
|
+
import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.debug.fps";
|
|
5
|
+
|
|
6
|
+
export const registerFpsPluginVne = () => {
|
|
7
|
+
registerCoreVne();
|
|
8
|
+
registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS, settings: SETTINGS_KEYS });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const createFpsTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
|
|
12
|
+
ModuleClass: T,
|
|
13
|
+
options: Parameters<typeof baseCreateTestModule>[1] = {},
|
|
14
|
+
) => {
|
|
15
|
+
const result = baseCreateTestModule(ModuleClass, {
|
|
16
|
+
state: {
|
|
17
|
+
"fps.controller": { text: 0, isShow: false },
|
|
18
|
+
...(options.state ?? {}),
|
|
19
|
+
},
|
|
20
|
+
shared: {
|
|
21
|
+
settings: { [SETTINGS_KEYS.ENABLED]: false, ...(options.shared?.settings ?? {}) },
|
|
22
|
+
viewForceAnimationSources: new SourcesSet(),
|
|
23
|
+
...(options.shared ?? {}),
|
|
24
|
+
},
|
|
25
|
+
...options,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.SHOW);
|
|
29
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.HIDE);
|
|
30
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE);
|
|
31
|
+
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { SUBSCRIBE_EVENTS, PARAMS, PLUGIN_NAME, SETTINGS_KEYS };
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/contracts.controllers.debug.fps";
|
|
4
|
+
import type { PluginName as SettingsPluginName, SubscribeEvents as SettingsSubscribeEvents } from "@vnejs/contracts.core.settings";
|
|
5
|
+
import type { PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/contracts.core.system";
|
|
6
|
+
|
|
7
|
+
export type FpsPluginEvents = ModuleComponentsEvents &
|
|
8
|
+
Record<PluginName, SubscribeEvents> &
|
|
9
|
+
Record<SettingsPluginName, SettingsSubscribeEvents> &
|
|
10
|
+
Record<SystemPluginName, SystemSubscribeEvents>;
|
|
11
|
+
|
|
12
|
+
export type FpsPluginConstants = ModuleComponentsConstants;
|
|
13
|
+
|
|
14
|
+
export type FpsPluginSettings = ModuleComponentsSettings & Record<PluginName, SettingsKeys>;
|
|
15
|
+
|
|
16
|
+
export type FpsPluginParams = ModuleComponentsParams & Record<PluginName, Params>;
|
|
17
|
+
|
|
18
|
+
export type FpsPluginState = {
|
|
19
|
+
isShow: boolean;
|
|
20
|
+
isForce: boolean;
|
|
21
|
+
text?: string | number;
|
|
22
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ViewRenderFunc } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import { createRenderFunc, Text, View, Wrap, useStoreState } from "@vnejs/uis.react";
|
|
4
|
+
|
|
5
|
+
import type { ReactComponentProps } from "@vnejs/uis.react";
|
|
6
|
+
|
|
7
|
+
import type { FpsPluginConstants, FpsPluginEvents, FpsPluginParams, FpsPluginSettings } from "../types.js";
|
|
8
|
+
import type { FpsPluginState } from "../types.js";
|
|
9
|
+
type FpsComponentProps = ReactComponentProps<FpsPluginEvents, FpsPluginConstants, FpsPluginSettings, FpsPluginParams, FpsPluginState>;
|
|
10
|
+
|
|
11
|
+
const FPS = ({ store, onMount, PARAMS }: FpsComponentProps) => {
|
|
12
|
+
const { isShow = false, isForce = false, text = "" } = useStoreState<FpsPluginState>(store, onMount);
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
<View
|
|
16
|
+
{...PARAMS.FPS.VIEW_PROPS.view}
|
|
17
|
+
isShow={isShow}
|
|
18
|
+
isForce={isForce}
|
|
19
|
+
>
|
|
20
|
+
<Wrap {...PARAMS.FPS.VIEW_PROPS.wrap}>
|
|
21
|
+
<Text
|
|
22
|
+
{...PARAMS.FPS.VIEW_PROPS.text}
|
|
23
|
+
text={text}
|
|
24
|
+
/>
|
|
25
|
+
</Wrap>
|
|
26
|
+
</View>
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const render: ViewRenderFunc<FpsPluginState> = createRenderFunc(FPS);
|
package/tsconfig.json
ADDED
package/README.md
DELETED