@vnejs/module.controller.bg 0.2.1

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.
@@ -0,0 +1,28 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { ModuleControllerBgConstants, ModuleControllerBgEvents, ModuleControllerBgParams, ModuleControllerBgSettings } from "./types.js";
3
+ export type { ModuleControllerBgConstants, ModuleControllerBgEvents, ModuleControllerBgParams, ModuleControllerBgSettings, ModuleControllerBgState, } from "./types.js";
4
+ export declare abstract class ModuleControllerBg<TEvents extends ModuleControllerBgEvents = ModuleControllerBgEvents, TConstants extends ModuleControllerBgConstants = ModuleControllerBgConstants, TSettings extends ModuleControllerBgSettings = ModuleControllerBgSettings, TParams extends ModuleControllerBgParams = ModuleControllerBgParams, TState = unknown> extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
5
+ abstract name: string;
6
+ abstract EVENT_SHOW: string;
7
+ abstract EVENT_HIDE: string;
8
+ abstract EVENT_SHOW_BEFORE: string;
9
+ abstract EVENT_STATE_UPDATE: string;
10
+ abstract EVENT_UPDATE_FAST: string;
11
+ EVENT_PRELOAD?: string;
12
+ abstract BGNAME: string;
13
+ abstract QUALITY_SETTING: string;
14
+ MEDIA_PREFIX: string;
15
+ MEDIA_TYPE: string;
16
+ IS_START_PRELOAD_ENABLED: boolean;
17
+ IS_STATE_CLEAR_PRELOAD_ENABLED: boolean;
18
+ IS_MEMORY_CLEARED_PRELOAD_ENABLED: boolean;
19
+ IS_MEMORY_CLEARED_UPDATE_ENABLED: boolean;
20
+ isShow: boolean;
21
+ subscribe: () => void;
22
+ start: () => Promise<HTMLImageElement> | undefined;
23
+ onShow: () => void;
24
+ onHide: () => void;
25
+ onShowBefore: () => Promise<void>;
26
+ onMemoryCleared: () => Promise<void>;
27
+ loadBg: () => Promise<HTMLImageElement>;
28
+ }
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class ModuleControllerBg extends ModuleCore {
3
+ EVENT_PRELOAD;
4
+ MEDIA_PREFIX = "bg/";
5
+ MEDIA_TYPE = "view";
6
+ IS_START_PRELOAD_ENABLED = false;
7
+ IS_STATE_CLEAR_PRELOAD_ENABLED = false;
8
+ IS_MEMORY_CLEARED_PRELOAD_ENABLED = false;
9
+ IS_MEMORY_CLEARED_UPDATE_ENABLED = false;
10
+ isShow = false;
11
+ subscribe = () => {
12
+ this.on(this.EVENT_SHOW, this.onShow);
13
+ this.on(this.EVENT_HIDE, this.onHide);
14
+ this.on(this.EVENT_SHOW_BEFORE, this.onShowBefore);
15
+ if (this.EVENT_PRELOAD)
16
+ this.on(this.EVENT_PRELOAD, this.loadBg);
17
+ this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
18
+ if (this.IS_STATE_CLEAR_PRELOAD_ENABLED)
19
+ this.on(this.EVENTS.STATE.CLEAR, this.loadBg);
20
+ };
21
+ start = () => {
22
+ if (this.IS_START_PRELOAD_ENABLED)
23
+ return this.loadBg();
24
+ };
25
+ onShow = () => {
26
+ this.isShow = true;
27
+ };
28
+ onHide = () => {
29
+ this.isShow = false;
30
+ };
31
+ onShowBefore = async () => {
32
+ const media = await this.loadBg();
33
+ await this.emit(this.EVENT_STATE_UPDATE, { bgSrc: media.src });
34
+ };
35
+ onMemoryCleared = async () => {
36
+ if (this.IS_MEMORY_CLEARED_UPDATE_ENABLED && this.isShow) {
37
+ const media = await this.loadBg();
38
+ if (!this.isShow)
39
+ return;
40
+ await this.emit(this.EVENT_STATE_UPDATE, { bgSrc: media.src });
41
+ await this.emit(this.EVENT_UPDATE_FAST);
42
+ return;
43
+ }
44
+ if (this.IS_MEMORY_CLEARED_PRELOAD_ENABLED)
45
+ await this.loadBg();
46
+ };
47
+ loadBg = () => {
48
+ const [type, priority] = [this.CONST.MEDIA.TYPES.IMAGE, this.CONST.MEDIA.PRIORITIES.HIGH];
49
+ return this.emitOne(this.EVENTS.MEDIA.LOAD, {
50
+ type,
51
+ priority,
52
+ quality: this.shared.settings[this.QUALITY_SETTING],
53
+ name: `${this.MEDIA_PREFIX}${this.BGNAME}`,
54
+ mediaType: this.MEDIA_TYPE,
55
+ });
56
+ };
57
+ }
@@ -0,0 +1,9 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ export type ModuleControllerBgEvents = ModuleCoreEvents;
3
+ export type ModuleControllerBgConstants = ModuleCoreConstants;
4
+ export type ModuleControllerBgSettings = ModuleCoreSettings;
5
+ export type ModuleControllerBgParams = ModuleCoreParams;
6
+ export type ModuleControllerBgState = {
7
+ bgSrc?: string;
8
+ isShow?: boolean;
9
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@vnejs/module.controller.bg",
3
+ "version": "0.2.1",
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": "echo \"Error: no test specified\" && exit 1",
22
+ "build": "npx @vnejs/monorepo package",
23
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
24
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
25
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
26
+ },
27
+ "author": "",
28
+ "license": "ISC",
29
+ "peerDependencies": {
30
+ "@vnejs/module.core": "~0.2.0",
31
+ "@vnejs/shared": "~0.2.0"
32
+ },
33
+ "devDependencies": {
34
+ "@vnejs/configs.ts-common": "~0.2.0",
35
+ "@vnejs/module.core": "~0.2.0",
36
+ "@vnejs/shared": "~0.2.0"
37
+ }
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,97 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { ModuleControllerBgConstants, ModuleControllerBgEvents, ModuleControllerBgParams, ModuleControllerBgSettings } from "./types.js";
4
+
5
+ export type {
6
+ ModuleControllerBgConstants,
7
+ ModuleControllerBgEvents,
8
+ ModuleControllerBgParams,
9
+ ModuleControllerBgSettings,
10
+ ModuleControllerBgState,
11
+ } from "./types.js";
12
+
13
+ export abstract class ModuleControllerBg<
14
+ TEvents extends ModuleControllerBgEvents = ModuleControllerBgEvents,
15
+ TConstants extends ModuleControllerBgConstants = ModuleControllerBgConstants,
16
+ TSettings extends ModuleControllerBgSettings = ModuleControllerBgSettings,
17
+ TParams extends ModuleControllerBgParams = ModuleControllerBgParams,
18
+ TState = unknown,
19
+ > extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
20
+ abstract override name: string;
21
+
22
+ abstract EVENT_SHOW: string;
23
+ abstract EVENT_HIDE: string;
24
+ abstract EVENT_SHOW_BEFORE: string;
25
+ abstract EVENT_STATE_UPDATE: string;
26
+ abstract EVENT_UPDATE_FAST: string;
27
+ EVENT_PRELOAD?: string;
28
+
29
+ abstract BGNAME: string;
30
+ abstract QUALITY_SETTING: string;
31
+
32
+ MEDIA_PREFIX = "bg/";
33
+ MEDIA_TYPE = "view";
34
+
35
+ IS_START_PRELOAD_ENABLED = false;
36
+ IS_STATE_CLEAR_PRELOAD_ENABLED = false;
37
+ IS_MEMORY_CLEARED_PRELOAD_ENABLED = false;
38
+ IS_MEMORY_CLEARED_UPDATE_ENABLED = false;
39
+
40
+ isShow = false;
41
+
42
+ subscribe = () => {
43
+ this.on(this.EVENT_SHOW, this.onShow);
44
+ this.on(this.EVENT_HIDE, this.onHide);
45
+ this.on(this.EVENT_SHOW_BEFORE, this.onShowBefore);
46
+
47
+ if (this.EVENT_PRELOAD) this.on(this.EVENT_PRELOAD, this.loadBg);
48
+
49
+ this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
50
+
51
+ if (this.IS_STATE_CLEAR_PRELOAD_ENABLED) this.on(this.EVENTS.STATE.CLEAR, this.loadBg);
52
+ };
53
+
54
+ start = () => {
55
+ if (this.IS_START_PRELOAD_ENABLED) return this.loadBg();
56
+ };
57
+
58
+ onShow = () => {
59
+ this.isShow = true;
60
+ };
61
+
62
+ onHide = () => {
63
+ this.isShow = false;
64
+ };
65
+
66
+ onShowBefore = async () => {
67
+ const media = await this.loadBg();
68
+
69
+ await this.emit(this.EVENT_STATE_UPDATE, { bgSrc: media.src });
70
+ };
71
+
72
+ onMemoryCleared = async () => {
73
+ if (this.IS_MEMORY_CLEARED_UPDATE_ENABLED && this.isShow) {
74
+ const media = await this.loadBg();
75
+ if (!this.isShow) return;
76
+
77
+ await this.emit(this.EVENT_STATE_UPDATE, { bgSrc: media.src });
78
+ await this.emit(this.EVENT_UPDATE_FAST);
79
+
80
+ return;
81
+ }
82
+
83
+ if (this.IS_MEMORY_CLEARED_PRELOAD_ENABLED) await this.loadBg();
84
+ };
85
+
86
+ loadBg = () => {
87
+ const [type, priority] = [this.CONST.MEDIA.TYPES.IMAGE, this.CONST.MEDIA.PRIORITIES.HIGH];
88
+
89
+ return this.emitOne(this.EVENTS.MEDIA.LOAD, {
90
+ type,
91
+ priority,
92
+ quality: this.shared.settings[this.QUALITY_SETTING],
93
+ name: `${this.MEDIA_PREFIX}${this.BGNAME}`,
94
+ mediaType: this.MEDIA_TYPE,
95
+ }) as Promise<HTMLImageElement>;
96
+ };
97
+ }
package/src/types.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+
3
+ export type ModuleControllerBgEvents = ModuleCoreEvents;
4
+
5
+ export type ModuleControllerBgConstants = ModuleCoreConstants;
6
+
7
+ export type ModuleControllerBgSettings = ModuleCoreSettings;
8
+
9
+ export type ModuleControllerBgParams = ModuleCoreParams;
10
+
11
+ export type ModuleControllerBgState = {
12
+ bgSrc?: string;
13
+ isShow?: boolean;
14
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts"],
8
+ "exclude": ["dist", "node_modules"]
9
+ }