Skip to content

Commit

Permalink
Release BetterVolume v3.1.0
Browse files Browse the repository at this point in the history
- Fix initialization (#194)
  • Loading branch information
Zerthox committed Nov 26, 2024
1 parent f31b82b commit 0eaba07
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
56 changes: 40 additions & 16 deletions dist/bd/BetterVolume.plugin.js
Original file line number Diff line number Diff line change
@@ -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%.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion dist/bd/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## [BetterFolders](https://betterdiscord.app/plugin/BetterFolders) <sub><sup>`v3.5.1` *(Updated: 17/11/2024)*</sup></sub>
Adds new functionality to server folders. Custom Folder Icons. Close other folders on open.

## [BetterVolume](https://betterdiscord.app/plugin/BetterVolume) <sub><sup>`v3.0.0` *(Updated: 10/08/2024)*</sup></sub>
## [BetterVolume](https://betterdiscord.app/plugin/BetterVolume) <sub><sup>`v3.1.0` *(Updated: 26/11/2024)*</sup></sub>
Set user volume values manually instead of using a slider. Allows setting volumes higher than 200%.

## [CollapseEmbeds](https://betterdiscord.app/plugin/CollapseEmbeds) <sub><sup>`v1.1.1` *(Updated: 04/05/2024)*</sup></sub>
Expand Down
2 changes: 1 addition & 1 deletion packages/dium/src/modules/flux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface Dispatcher {
_subscriptions: Record<string, Set<ActionHandler>>;
_processingWaitQueue: boolean;
_waitQueue: any[];
_interceptor: (arg: any) => any;
_interceptors: ActionHandler[];

_dispatch<A extends Action>(action: A): void;
_dispatchWithDevtools<A extends Action>(action: A): void;
Expand Down
2 changes: 1 addition & 1 deletion src/BetterVolume/package.json
Original file line number Diff line number Diff line change
@@ -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": "*"
Expand Down
60 changes: 43 additions & 17 deletions src/BetterVolume/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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<string, Flux.ActionHandler> & {
Expand Down Expand Up @@ -71,28 +98,27 @@ const trySwapHandler = <A extends Flux.Action>(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);
Expand Down

0 comments on commit 0eaba07

Please sign in to comment.