@vnejs/module.controller.audio 0.2.1 → 0.2.2

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 CHANGED
@@ -1,10 +1,11 @@
1
- import { ModuleCore } from "@vnejs/module.core";
1
+ import { ModuleCore, type SettingsChangedResult } from "@vnejs/module.core";
2
2
  import type { ModuleControllerAudioConstants, ModuleControllerAudioEvents, ModuleControllerAudioParams, ModuleControllerAudioSettings } from "./types.js";
3
- export type { ModuleControllerAudioConstants, ModuleControllerAudioEvents, ModuleControllerAudioParams, ModuleControllerAudioSettings, } from "./types.js";
3
+ export type { ModuleControllerAudioConstants, ModuleControllerAudioEvents, ModuleControllerAudioParams, ModuleControllerAudioSettings } from "./types.js";
4
4
  export declare abstract class ModuleControllerAudio<TEvents extends ModuleControllerAudioEvents = ModuleControllerAudioEvents, TConstants extends ModuleControllerAudioConstants = ModuleControllerAudioConstants, TSettings extends ModuleControllerAudioSettings = ModuleControllerAudioSettings, TParams extends ModuleControllerAudioParams = ModuleControllerAudioParams, TState = unknown> extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
5
5
  abstract name: string;
6
6
  abstract AUDIO_NAME: string;
7
7
  abstract AUDIO_START_DURATION: number;
8
+ abstract VOLUME_SETTING: string;
8
9
  abstract SHOW_EVENT: string;
9
10
  abstract HIDE_EVENT: string;
10
11
  abstract LOAD_EVENT: string;
@@ -16,9 +17,14 @@ export declare abstract class ModuleControllerAudio<TEvents extends ModuleContro
16
17
  PARAMS_DURATION: {
17
18
  duration: number;
18
19
  };
20
+ isPlaying: boolean;
19
21
  subscribe: () => void;
20
22
  init: () => void;
21
23
  onShow: () => void;
22
24
  onHide: () => void;
23
25
  onLoad: () => undefined;
26
+ onVolumeChanged: () => false | Promise<unknown[]> | undefined;
27
+ onSettingsChanged: ({ name }: SettingsChangedResult) => false | Promise<unknown[]> | undefined;
28
+ emitVolume: () => Promise<unknown[]> | undefined;
29
+ getRealVolume: (volume?: number) => number;
24
30
  }
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import { ModuleCore } from "@vnejs/module.core";
2
2
  export class ModuleControllerAudio extends ModuleCore {
3
3
  PARAMS_ALL;
4
4
  PARAMS_DURATION;
5
+ isPlaying = false;
5
6
  subscribe = () => {
6
7
  if (!this.AUDIO_NAME)
7
8
  return;
@@ -10,18 +11,26 @@ export class ModuleControllerAudio extends ModuleCore {
10
11
  this.on(this.LOAD_EVENT, this.onLoad);
11
12
  this.on(this.EVENTS.STATE.SET, this.onHide);
12
13
  this.on(this.EVENTS.STATE.CLEAR, this.onHide);
14
+ this.on(this.EVENTS.AUDIO.VOLUME_CHANGED, this.onVolumeChanged);
15
+ this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingsChanged);
13
16
  };
14
17
  init = () => {
15
18
  this.PARAMS_ALL = { layer: "view", name: this.AUDIO_NAME, duration: this.AUDIO_START_DURATION };
16
19
  this.PARAMS_DURATION = { duration: this.AUDIO_START_DURATION };
17
20
  };
18
21
  onShow = () => {
22
+ this.isPlaying = true;
19
23
  this.emit(this.EVENTS.AUDIO.GLOBAL_PAUSE, this.PARAMS_DURATION);
20
24
  this.emit(this.EVENTS.AUDIO.PLAY, this.PARAMS_ALL);
21
25
  };
22
26
  onHide = () => {
27
+ this.isPlaying = false;
23
28
  this.emit(this.EVENTS.AUDIO.STOP, this.PARAMS_ALL);
24
29
  this.emit(this.EVENTS.AUDIO.GLOBAL_RESUME, this.PARAMS_DURATION);
25
30
  };
26
31
  onLoad = () => void this.emit(this.EVENTS.AUDIO.MEDIA_LOAD, this.PARAMS_ALL);
32
+ onVolumeChanged = () => this.isPlaying && this.emitVolume();
33
+ onSettingsChanged = ({ name }) => name === this.VOLUME_SETTING && this.isPlaying && this.emitVolume();
34
+ emitVolume = () => this.emit(this.EVENTS.AUDIO.VOLUME, { layer: "view", name: this.AUDIO_NAME, volume: this.getRealVolume() });
35
+ getRealVolume = (volume = 100) => ((Number(this.shared.settings?.[this.VOLUME_SETTING] ?? 100) || 0) / 100) * volume;
27
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/module.controller.audio",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1,18 +1,8 @@
1
- import { ModuleCore } from "@vnejs/module.core";
2
-
3
- import type {
4
- ModuleControllerAudioConstants,
5
- ModuleControllerAudioEvents,
6
- ModuleControllerAudioParams,
7
- ModuleControllerAudioSettings,
8
- } from "./types.js";
9
-
10
- export type {
11
- ModuleControllerAudioConstants,
12
- ModuleControllerAudioEvents,
13
- ModuleControllerAudioParams,
14
- ModuleControllerAudioSettings,
15
- } from "./types.js";
1
+ import { ModuleCore, type SettingsChangedResult } from "@vnejs/module.core";
2
+
3
+ import type { ModuleControllerAudioConstants, ModuleControllerAudioEvents, ModuleControllerAudioParams, ModuleControllerAudioSettings } from "./types.js";
4
+
5
+ export type { ModuleControllerAudioConstants, ModuleControllerAudioEvents, ModuleControllerAudioParams, ModuleControllerAudioSettings } from "./types.js";
16
6
 
17
7
  export abstract class ModuleControllerAudio<
18
8
  TEvents extends ModuleControllerAudioEvents = ModuleControllerAudioEvents,
@@ -25,6 +15,7 @@ export abstract class ModuleControllerAudio<
25
15
 
26
16
  abstract AUDIO_NAME: string;
27
17
  abstract AUDIO_START_DURATION: number;
18
+ abstract VOLUME_SETTING: string;
28
19
 
29
20
  abstract SHOW_EVENT: string;
30
21
  abstract HIDE_EVENT: string;
@@ -33,6 +24,8 @@ export abstract class ModuleControllerAudio<
33
24
  PARAMS_ALL!: { layer: string; name: string; duration: number };
34
25
  PARAMS_DURATION!: { duration: number };
35
26
 
27
+ isPlaying = false;
28
+
36
29
  subscribe = () => {
37
30
  if (!this.AUDIO_NAME) return;
38
31
 
@@ -42,6 +35,9 @@ export abstract class ModuleControllerAudio<
42
35
 
43
36
  this.on(this.EVENTS.STATE.SET, this.onHide);
44
37
  this.on(this.EVENTS.STATE.CLEAR, this.onHide);
38
+
39
+ this.on(this.EVENTS.AUDIO.VOLUME_CHANGED, this.onVolumeChanged);
40
+ this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingsChanged);
45
41
  };
46
42
 
47
43
  init = () => {
@@ -50,12 +46,21 @@ export abstract class ModuleControllerAudio<
50
46
  };
51
47
 
52
48
  onShow = () => {
49
+ this.isPlaying = true;
53
50
  this.emit(this.EVENTS.AUDIO.GLOBAL_PAUSE, this.PARAMS_DURATION);
54
51
  this.emit(this.EVENTS.AUDIO.PLAY, this.PARAMS_ALL);
55
52
  };
56
53
  onHide = () => {
54
+ this.isPlaying = false;
57
55
  this.emit(this.EVENTS.AUDIO.STOP, this.PARAMS_ALL);
58
56
  this.emit(this.EVENTS.AUDIO.GLOBAL_RESUME, this.PARAMS_DURATION);
59
57
  };
60
58
  onLoad = () => void this.emit(this.EVENTS.AUDIO.MEDIA_LOAD, this.PARAMS_ALL);
59
+
60
+ onVolumeChanged = () => this.isPlaying && this.emitVolume();
61
+ onSettingsChanged = ({ name }: SettingsChangedResult) => name === this.VOLUME_SETTING && this.isPlaying && this.emitVolume();
62
+
63
+ emitVolume = () => this.emit(this.EVENTS.AUDIO.VOLUME, { layer: "view", name: this.AUDIO_NAME, volume: this.getRealVolume() });
64
+
65
+ getRealVolume = (volume = 100) => ((Number(this.shared.settings?.[this.VOLUME_SETTING] ?? 100) || 0) / 100) * volume;
61
66
  }