revenge-bundle-next/lib/discord/src/flux/dispatcher.ts

68 lines
2 KiB
TypeScript
Raw Normal View History

import { fPatches, fPatchesAll } from '../patches/flux'
import type { DiscordModules } from '../types'
2025-08-05 20:36:08 +07:00
export type FluxEventDispatchPatch<T extends object = object> = (
payload: DiscordModules.Flux.DispatcherPayload & T,
) => (DiscordModules.Flux.DispatcherPayload & T) | undefined | void
2025-08-05 20:36:08 +07:00
/**
* Registers a patch for all Flux events.
*
* @see {@link onFluxEventDispatched} for more details.
*
* @param patch The patch function to apply when any Flux event is dispatched.
* @returns A function that can be used to remove the patch.
*/
export function onAnyFluxEventDispatched(patch: FluxEventDispatchPatch) {
fPatchesAll.add(patch)
return () => {
fPatchesAll.delete(patch)
}
}
/**
* Registers a patch for a specific Flux event type.
* @param type The type of the Flux event to patch.
* @param patch The patch function to apply when the event is dispatched.
* @returns A function that can be used to remove the patch.
*
2026-08-14 01:50:09 +07:00
* @example Blocking the dispatch
2025-08-05 20:36:08 +07:00
* ```ts
* // Returning falsy values will prevent the event from being dispatched.
* onFluxEventDispatched('TYPING_START', () => {})
* ```
*
* @example Modifying the payload
* ```ts
* onFluxEventDispatched('TYPING_START', payload => {
* // Send the typing event to this channel instead.
* payload.channelId = '123456789012345678'
* // Make sure to return the modified payload!
* return payload
* })
* ```
*
* @example Reading and passing through the payload
* ```ts
* onFluxEventDispatched('TYPING_START', payload => {
* console.log('Typing started:', payload)
* // Do nothing, just return the payload.
* return payload
* })
* ```
*/
export function onFluxEventDispatched<T extends object = object>(
2025-08-05 20:36:08 +07:00
type: DiscordModules.Flux.DispatcherPayload['type'],
patch: FluxEventDispatchPatch<T>,
2025-08-05 20:36:08 +07:00
) {
let set = fPatches.get(type)
if (!set) fPatches.set(type, (set = new Set<FluxEventDispatchPatch>()))
set.add(patch)
return () => {
set.delete(patch)
}
}