From 0eaba07dc0f98cb77da53254de8b6720a5920d2d Mon Sep 17 00:00:00 2001 From: Zerthox Date: Tue, 26 Nov 2024 18:54:28 +0100 Subject: [PATCH] Release BetterVolume v3.1.0 - Fix initialization (#194) --- dist/bd/BetterVolume.plugin.js | 56 ++++++++++++++++++++--------- dist/bd/README.md | 2 +- packages/dium/src/modules/flux.ts | 2 +- src/BetterVolume/package.json | 2 +- src/BetterVolume/sync.ts | 60 ++++++++++++++++++++++--------- 5 files changed, 86 insertions(+), 36 deletions(-) diff --git a/dist/bd/BetterVolume.plugin.js b/dist/bd/BetterVolume.plugin.js index 43acd09e..c6eed726 100644 --- a/dist/bd/BetterVolume.plugin.js +++ b/dist/bd/BetterVolume.plugin.js @@ -1,6 +1,6 @@ /** * @name BetterVolume - * @version 3.0.0 + * @version 3.1.0 * @author Zerthox * @authorLink https://github.com/Zerthox * @description Set user volume values manually instead of using a slider. Allows setting volumes higher than 200%. @@ -402,7 +402,31 @@ const dispatchVolumeOverrides = () => { } } }; -const settingsUpdateHandler = (_action) => dispatchVolumeOverrides(); +const findAudioSettingsManager = () => { + const hasSetVolume = byKeys$1("AUDIO_SET_LOCAL_VOLUME" ); + return find((exported) => exported.actions && hasSetVolume(exported.actions)); +}; +const handleAudioSettingsManager = (AudioSettingsManager) => { + originalHandler = AudioSettingsManager.actions["AUDIO_SET_LOCAL_VOLUME" ]; + const swapped = trySwapHandler("AUDIO_SET_LOCAL_VOLUME" , originalHandler, wrappedSettingsManagerHandler); + if (swapped) { + log(`Replaced AudioSettingsManager ${"AUDIO_SET_LOCAL_VOLUME" } handler`); + } + else { + warn(`AudioSettingsManager ${"AUDIO_SET_LOCAL_VOLUME" } handler not present`); + } +}; +const postConnectionOpenHandler = (_action) => { + log(`Received ${"POST_CONNECTION_OPEN" }`); + dispatchVolumeOverrides(); + const AudioSettingsManager = findAudioSettingsManager(); + if (AudioSettingsManager) { + handleAudioSettingsManager(AudioSettingsManager); + } + else { + warn("Failed to find AudioSettingsManager"); + } +}; let originalHandler = null; const wrappedSettingsManagerHandler = (action) => { const { userId, volume, context } = action; @@ -430,24 +454,24 @@ const trySwapHandler = (action, prev, next) => { } return isPresent; }; -const hasSetVolume = byKeys$1("AUDIO_SET_LOCAL_VOLUME" ); const handleVolumeSync = () => { - Dispatcher$1.subscribe("USER_SETTINGS_PROTO_UPDATE" , settingsUpdateHandler); + Dispatcher$1.subscribe("POST_CONNECTION_OPEN" , postConnectionOpenHandler); + log(`Subscribed to ${"POST_CONNECTION_OPEN" } events`); + Dispatcher$1.subscribe("USER_SETTINGS_PROTO_UPDATE" , dispatchVolumeOverrides); log(`Subscribed to ${"USER_SETTINGS_PROTO_UPDATE" } events`); - dispatchVolumeOverrides(); - waitFor((exported) => exported.actions && hasSetVolume(exported.actions)).then((AudioSettingsManager) => { - originalHandler = AudioSettingsManager.actions["AUDIO_SET_LOCAL_VOLUME" ]; - const swapped = trySwapHandler("AUDIO_SET_LOCAL_VOLUME" , originalHandler, wrappedSettingsManagerHandler); - if (swapped) { - log(`Replaced ${"AUDIO_SET_LOCAL_VOLUME" } handler`); - } - else { - warn(`${"AUDIO_SET_LOCAL_VOLUME" } handler not present`); - } - }); + const AudioSettingsManager = findAudioSettingsManager(); + if (AudioSettingsManager) { + dispatchVolumeOverrides(); + handleAudioSettingsManager(AudioSettingsManager); + } + else { + log(`AudioSettingsManager not found, waiting for ${"POST_CONNECTION_OPEN" }`); + } }; const resetVolumeSync = () => { - Dispatcher$1.unsubscribe("USER_SETTINGS_PROTO_UPDATE" , settingsUpdateHandler); + Dispatcher$1.unsubscribe("POST_CONNECTION_OPEN" , postConnectionOpenHandler); + log(`Unsubscribed from ${"POST_CONNECTION_OPEN" } events`); + Dispatcher$1.unsubscribe("USER_SETTINGS_PROTO_UPDATE" , dispatchVolumeOverrides); log(`Unsubscribed from ${"USER_SETTINGS_PROTO_UPDATE" } events`); const swapped = trySwapHandler("AUDIO_SET_LOCAL_VOLUME" , wrappedSettingsManagerHandler, originalHandler); if (swapped) { diff --git a/dist/bd/README.md b/dist/bd/README.md index be71ae43..0b96781d 100644 --- a/dist/bd/README.md +++ b/dist/bd/README.md @@ -1,7 +1,7 @@ ## [BetterFolders](https://betterdiscord.app/plugin/BetterFolders) `v3.5.1` *(Updated: 17/11/2024)* Adds new functionality to server folders. Custom Folder Icons. Close other folders on open. -## [BetterVolume](https://betterdiscord.app/plugin/BetterVolume) `v3.0.0` *(Updated: 10/08/2024)* +## [BetterVolume](https://betterdiscord.app/plugin/BetterVolume) `v3.1.0` *(Updated: 26/11/2024)* Set user volume values manually instead of using a slider. Allows setting volumes higher than 200%. ## [CollapseEmbeds](https://betterdiscord.app/plugin/CollapseEmbeds) `v1.1.1` *(Updated: 04/05/2024)* diff --git a/packages/dium/src/modules/flux.ts b/packages/dium/src/modules/flux.ts index f25291fd..0099c322 100644 --- a/packages/dium/src/modules/flux.ts +++ b/packages/dium/src/modules/flux.ts @@ -71,7 +71,7 @@ export interface Dispatcher { _subscriptions: Record>; _processingWaitQueue: boolean; _waitQueue: any[]; - _interceptor: (arg: any) => any; + _interceptors: ActionHandler[]; _dispatch(action: A): void; _dispatchWithDevtools(action: A): void; diff --git a/src/BetterVolume/package.json b/src/BetterVolume/package.json index 156611d5..6c819407 100644 --- a/src/BetterVolume/package.json +++ b/src/BetterVolume/package.json @@ -1,7 +1,7 @@ { "name": "BetterVolume", "author": "Zerthox", - "version": "3.0.0", + "version": "3.1.0", "description": "Set user volume values manually instead of using a slider. Allows setting volumes higher than 200%.", "dependencies": { "dium": "*" diff --git a/src/BetterVolume/sync.ts b/src/BetterVolume/sync.ts index 21c8806d..c444362e 100644 --- a/src/BetterVolume/sync.ts +++ b/src/BetterVolume/sync.ts @@ -3,6 +3,7 @@ import {Snowflake, Dispatcher, MediaEngineContext, AudioConvert} from "@dium/mod import {Settings, updateVolumeOverride as updateVolumeOverride, tryResetVolumeOverride} from "./settings"; const enum ActionType { + POST_CONNECTION_OPEN = "POST_CONNECTION_OPEN", AUDIO_SET_LOCAL_VOLUME = "AUDIO_SET_LOCAL_VOLUME", USER_SETTINGS_PROTO_UPDATE = "USER_SETTINGS_PROTO_UPDATE" } @@ -32,7 +33,33 @@ interface SetVolumeAction extends Flux.Action { context: MediaEngineContext; } -const settingsUpdateHandler = (_action: Flux.Action) => dispatchVolumeOverrides(); +const findAudioSettingsManager = (): AudioSettingsManager => { + const hasSetVolume = Filters.byKeys(ActionType.AUDIO_SET_LOCAL_VOLUME); + return Finder.find((exported) => exported.actions && hasSetVolume(exported.actions)); +}; + +const handleAudioSettingsManager = (AudioSettingsManager: AudioSettingsManager): void => { + originalHandler = AudioSettingsManager.actions[ActionType.AUDIO_SET_LOCAL_VOLUME]; + const swapped = trySwapHandler(ActionType.AUDIO_SET_LOCAL_VOLUME, originalHandler, wrappedSettingsManagerHandler); + if (swapped) { + Logger.log(`Replaced AudioSettingsManager ${ActionType.AUDIO_SET_LOCAL_VOLUME} handler`); + } else { + Logger.warn(`AudioSettingsManager ${ActionType.AUDIO_SET_LOCAL_VOLUME} handler not present`); + } +}; + +const postConnectionOpenHandler = (_action: Flux.Action): void => { + Logger.log(`Received ${ActionType.POST_CONNECTION_OPEN}`); + + dispatchVolumeOverrides(); + + const AudioSettingsManager = findAudioSettingsManager(); + if (AudioSettingsManager) { + handleAudioSettingsManager(AudioSettingsManager); + } else { + Logger.warn("Failed to find AudioSettingsManager"); + } +}; interface AudioSettingsManager { actions: Record & { @@ -71,28 +98,27 @@ const trySwapHandler = (action: Flux.Action["type"], prev return isPresent; }; -const hasSetVolume = Filters.byKeys(ActionType.AUDIO_SET_LOCAL_VOLUME); - export const handleVolumeSync = (): void => { - Dispatcher.subscribe(ActionType.USER_SETTINGS_PROTO_UPDATE, settingsUpdateHandler); - Logger.log(`Subscribed to ${ActionType.USER_SETTINGS_PROTO_UPDATE} events`); + Dispatcher.subscribe(ActionType.POST_CONNECTION_OPEN, postConnectionOpenHandler); + Logger.log(`Subscribed to ${ActionType.POST_CONNECTION_OPEN} events`); - // TODO: needed on connection open? - dispatchVolumeOverrides(); + Dispatcher.subscribe(ActionType.USER_SETTINGS_PROTO_UPDATE, dispatchVolumeOverrides); + Logger.log(`Subscribed to ${ActionType.USER_SETTINGS_PROTO_UPDATE} events`); - Finder.waitFor((exported) => exported.actions && hasSetVolume(exported.actions)).then((AudioSettingsManager: AudioSettingsManager) => { - originalHandler = AudioSettingsManager.actions[ActionType.AUDIO_SET_LOCAL_VOLUME]; - const swapped = trySwapHandler(ActionType.AUDIO_SET_LOCAL_VOLUME, originalHandler, wrappedSettingsManagerHandler); - if (swapped) { - Logger.log(`Replaced ${ActionType.AUDIO_SET_LOCAL_VOLUME} handler`); - } else { - Logger.warn(`${ActionType.AUDIO_SET_LOCAL_VOLUME} handler not present`); - } - }); + const AudioSettingsManager = findAudioSettingsManager(); + if (AudioSettingsManager) { + dispatchVolumeOverrides(); + handleAudioSettingsManager(AudioSettingsManager); + } else { + Logger.log(`AudioSettingsManager not found, waiting for ${ActionType.POST_CONNECTION_OPEN}`); + } }; export const resetVolumeSync = (): void => { - Dispatcher.unsubscribe(ActionType.USER_SETTINGS_PROTO_UPDATE, settingsUpdateHandler); + Dispatcher.unsubscribe(ActionType.POST_CONNECTION_OPEN, postConnectionOpenHandler); + Logger.log(`Unsubscribed from ${ActionType.POST_CONNECTION_OPEN} events`); + + Dispatcher.unsubscribe(ActionType.USER_SETTINGS_PROTO_UPDATE, dispatchVolumeOverrides); Logger.log(`Unsubscribed from ${ActionType.USER_SETTINGS_PROTO_UPDATE} events`); const swapped = trySwapHandler(ActionType.AUDIO_SET_LOCAL_VOLUME, wrappedSettingsManagerHandler, originalHandler);