mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-08-02 11:26:46 +02:00
Rename EventBus events
This commit is contained in:
@@ -1,26 +1,30 @@
|
||||
import type { PrefKey, StorageKey } from "@/enums/pref-keys";
|
||||
import { BX_FLAGS } from "./bx-flags";
|
||||
import { BxLogger } from "./bx-logger";
|
||||
import { AppInterface } from "./global";
|
||||
|
||||
type EventCallback<T = any> = (payload: T) => void;
|
||||
|
||||
type ScriptEvents = {
|
||||
xcloudServerReady: {};
|
||||
xcloudServerUnavailable: {};
|
||||
'xcloud.server.ready': {};
|
||||
'xcloud.server.unavailable': {};
|
||||
|
||||
titleInfoReady: {};
|
||||
settingChanged: {
|
||||
'dialog.shown': {},
|
||||
'dialog.dismissed': {},
|
||||
|
||||
'titleInfo.ready': {};
|
||||
'setting.changed': {
|
||||
storageKey: StorageKey;
|
||||
settingKey: PrefKey;
|
||||
settingValue: any;
|
||||
};
|
||||
|
||||
mkbSettingUpdated: {};
|
||||
keyboardShortcutsUpdated: {};
|
||||
deviceVibrationUpdated: {};
|
||||
'mkb.setting.updated': {};
|
||||
'keyboardShortcuts.updated': {};
|
||||
'deviceVibration.updated': {};
|
||||
|
||||
// GH pages
|
||||
listForcedNativeMkbUpdated: {
|
||||
'list.forcedNativeMkb.updated': {
|
||||
data: {
|
||||
data: any;
|
||||
};
|
||||
@@ -28,20 +32,25 @@ type ScriptEvents = {
|
||||
};
|
||||
|
||||
type StreamEvents = {
|
||||
stateLoading: {};
|
||||
stateStarting: {};
|
||||
statePlaying: { $video?: HTMLVideoElement };
|
||||
stateStopped: {};
|
||||
stateError: {};
|
||||
'state.loading': {};
|
||||
'state.starting': {};
|
||||
'state.playing': { $video?: HTMLVideoElement };
|
||||
'state.stopped': {};
|
||||
'state.error': {};
|
||||
|
||||
dataChannelCreated: { dataChannel: RTCDataChannel };
|
||||
};
|
||||
|
||||
export class BxEventBus<TEvents extends Record<string, any>> {
|
||||
private listeners: Map<keyof TEvents, Set<EventCallback<any>>> = new Map();
|
||||
private group: string;
|
||||
|
||||
static readonly Script = new BxEventBus<ScriptEvents>();
|
||||
static readonly Stream = new BxEventBus<StreamEvents>();
|
||||
static readonly Script = new BxEventBus<ScriptEvents>('script');
|
||||
static readonly Stream = new BxEventBus<StreamEvents>('stream');
|
||||
|
||||
constructor(group: string) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
on<K extends keyof TEvents>(event: K, callback: EventCallback<TEvents[K]>): void {
|
||||
if (!this.listeners.has(event)) {
|
||||
@@ -52,6 +61,16 @@ export class BxEventBus<TEvents extends Record<string, any>> {
|
||||
BX_FLAGS.Debug && BxLogger.warning('EventBus', 'on', event, callback);
|
||||
}
|
||||
|
||||
once<K extends keyof TEvents>(event: string, callback: EventCallback<TEvents[K]>): void {
|
||||
const wrapper = (...args: any[]) => {
|
||||
// @ts-ignore
|
||||
callback(...args);
|
||||
this.off(event, wrapper);
|
||||
};
|
||||
|
||||
this.on(event, wrapper);
|
||||
}
|
||||
|
||||
off<K extends keyof TEvents>(event: K, callback: EventCallback<TEvents[K]> | null): void {
|
||||
BX_FLAGS.Debug && BxLogger.warning('EventBus', 'off', event, callback);
|
||||
|
||||
@@ -77,12 +96,13 @@ export class BxEventBus<TEvents extends Record<string, any>> {
|
||||
}
|
||||
|
||||
emit<K extends keyof TEvents>(event: K, payload: TEvents[K]): void {
|
||||
BX_FLAGS.Debug && BxLogger.warning('EventBus', 'emit', event, payload);
|
||||
|
||||
const callbacks = this.listeners.get(event) || [];
|
||||
for (const callback of callbacks) {
|
||||
callback(payload);
|
||||
}
|
||||
|
||||
AppInterface && AppInterface.onEventBus(this.group + '.' + (event as string));
|
||||
BX_FLAGS.Debug && BxLogger.warning('EventBus', 'emit', event, payload);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -28,10 +28,6 @@ export namespace BxEvent {
|
||||
|
||||
export const NAVIGATION_FOCUS_CHANGED = 'bx-nav-focus-changed';
|
||||
|
||||
// xCloud Dialog events
|
||||
export const XCLOUD_DIALOG_SHOWN = 'bx-xcloud-dialog-shown';
|
||||
export const XCLOUD_DIALOG_DISMISSED = 'bx-xcloud-dialog-dismissed';
|
||||
|
||||
export const XCLOUD_GUIDE_MENU_SHOWN = 'bx-xcloud-guide-menu-shown';
|
||||
|
||||
export const XCLOUD_POLLING_MODE_CHANGED = 'bx-xcloud-polling-mode-changed';
|
||||
|
@@ -139,7 +139,7 @@ export const BxExposed = {
|
||||
|
||||
// Save this info in STATES
|
||||
STATES.currentStream.titleInfo = titleInfo;
|
||||
BxEventBus.Script.emit('titleInfoReady', {});
|
||||
BxEventBus.Script.emit('titleInfo.ready', {});
|
||||
|
||||
return titleInfo;
|
||||
},
|
||||
|
@@ -53,7 +53,7 @@ export class GhPagesUtils {
|
||||
if (json.$schemaVersion === supportedSchema) {
|
||||
// Save to storage
|
||||
window.localStorage.setItem(key, JSON.stringify(json));
|
||||
BxEventBus.Script.emit('listForcedNativeMkbUpdated', {
|
||||
BxEventBus.Script.emit('list.forcedNativeMkb.updated', {
|
||||
data: json,
|
||||
});
|
||||
}
|
||||
|
@@ -33,5 +33,5 @@ export function onHistoryChanged(e: PopStateEvent) {
|
||||
LoadingScreen.reset();
|
||||
window.setTimeout(HeaderSection.watchHeader, 2000);
|
||||
|
||||
BxEventBus.Stream.emit('stateStopped', {});
|
||||
BxEventBus.Stream.emit('state.stopped', {});
|
||||
}
|
||||
|
@@ -28,7 +28,7 @@ export function patchVideoApi() {
|
||||
} satisfies StreamPlayerOptions;
|
||||
STATES.currentStream.streamPlayer = new StreamPlayer(this, getPref(PrefKey.VIDEO_PLAYER_TYPE), playerOptions);
|
||||
|
||||
BxEventBus.Stream.emit('statePlaying', {
|
||||
BxEventBus.Stream.emit('state.playing', {
|
||||
$video: this,
|
||||
})
|
||||
}
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import { GuideMenu } from "@/modules/ui/guide-menu";
|
||||
import { BxEvent } from "./bx-event";
|
||||
import { BX_FLAGS } from "./bx-flags";
|
||||
import { BxLogger } from "./bx-logger";
|
||||
import { BxIcon } from "./bx-icon";
|
||||
@@ -7,6 +6,7 @@ import { AppInterface } from "./global";
|
||||
import { createButton, ButtonStyle } from "./html";
|
||||
import { t } from "./translation";
|
||||
import { parseDetailsPath } from "./utils";
|
||||
import { BxEventBus } from "./bx-event-bus";
|
||||
|
||||
|
||||
export class RootDialogObserver {
|
||||
@@ -85,7 +85,7 @@ export class RootDialogObserver {
|
||||
const shown = !!($root.firstElementChild && $root.firstElementChild.childElementCount > 0);
|
||||
if (shown !== beingShown) {
|
||||
beingShown = shown;
|
||||
BxEvent.dispatch(window, shown ? BxEvent.XCLOUD_DIALOG_SHOWN : BxEvent.XCLOUD_DIALOG_DISMISSED);
|
||||
BxEventBus.Script.emit(shown ? 'dialog.shown' : 'dialog.dismissed', {});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@@ -93,7 +93,7 @@ export class BaseSettingsStore {
|
||||
this.settings[key] = this.validateValue('get', key, value);
|
||||
this.saveSettings();
|
||||
|
||||
emitEvent && BxEventBus.Script.emit('settingChanged', {
|
||||
emitEvent && BxEventBus.Script.emit('setting.changed', {
|
||||
storageKey: this.storageKey,
|
||||
settingKey: key,
|
||||
settingValue: value,
|
||||
|
@@ -432,7 +432,7 @@ export class GlobalSettingsStorage extends BaseSettingsStorage {
|
||||
if (!setting.unsupported) {
|
||||
(setting as any).multipleOptions = GhPagesUtils.getNativeMkbCustomList(true);
|
||||
|
||||
BxEventBus.Script.on('listForcedNativeMkbUpdated', payload => {
|
||||
BxEventBus.Script.on('list.forcedNativeMkb.updated', payload => {
|
||||
(setting as any).multipleOptions = payload.data.data;
|
||||
});
|
||||
}
|
||||
|
@@ -110,7 +110,7 @@ export class StreamSettings {
|
||||
}
|
||||
|
||||
StreamSettings.settings.deviceVibrationIntensity = intensity;
|
||||
BxEventBus.Script.emit('deviceVibrationUpdated', {});
|
||||
BxEventBus.Script.emit('deviceVibration.updated', {});
|
||||
}
|
||||
|
||||
static async refreshMkbSettings() {
|
||||
@@ -148,7 +148,7 @@ export class StreamSettings {
|
||||
settings.mkbPreset = converted;
|
||||
|
||||
setPref(PrefKey.MKB_P1_MAPPING_PRESET_ID, orgPreset.id);
|
||||
BxEventBus.Script.emit('mkbSettingUpdated', {});
|
||||
BxEventBus.Script.emit('mkb.setting.updated', {});
|
||||
}
|
||||
|
||||
static async refreshKeyboardShortcuts() {
|
||||
@@ -159,7 +159,7 @@ export class StreamSettings {
|
||||
settings.keyboardShortcuts = null;
|
||||
|
||||
setPref(PrefKey.KEYBOARD_SHORTCUTS_IN_GAME_PRESET_ID, presetId);
|
||||
BxEventBus.Script.emit('keyboardShortcutsUpdated', {});
|
||||
BxEventBus.Script.emit('keyboardShortcuts.updated', {});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -179,7 +179,7 @@ export class StreamSettings {
|
||||
settings.keyboardShortcuts = converted;
|
||||
|
||||
setPref(PrefKey.KEYBOARD_SHORTCUTS_IN_GAME_PRESET_ID, orgPreset.id);
|
||||
BxEventBus.Script.emit('keyboardShortcutsUpdated', {});
|
||||
BxEventBus.Script.emit('keyboardShortcuts.updated', {});
|
||||
}
|
||||
|
||||
static async refreshAllSettings() {
|
||||
|
@@ -310,7 +310,7 @@ export class StreamStatsCollector {
|
||||
}
|
||||
|
||||
static setupEvents() {
|
||||
BxEventBus.Stream.on('statePlaying', () => {
|
||||
BxEventBus.Stream.on('state.playing', () => {
|
||||
StreamStatsCollector.getInstance().reset();
|
||||
});
|
||||
}
|
||||
|
@@ -52,7 +52,7 @@ export class XcloudInterceptor {
|
||||
const response = await NATIVE_FETCH(request, init);
|
||||
if (response.status !== 200) {
|
||||
// Unsupported region
|
||||
BxEventBus.Script.emit('xcloudServerUnavailable', {});
|
||||
BxEventBus.Script.emit('xcloud.server.unavailable', {});
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ export class XcloudInterceptor {
|
||||
STATES.serverRegions[region.name] = Object.assign({}, region);
|
||||
}
|
||||
|
||||
BxEventBus.Script.emit('xcloudServerReady', {});
|
||||
BxEventBus.Script.emit('xcloud.server.ready', {});
|
||||
|
||||
const preferredRegion = getPreferredServerRegion();
|
||||
if (preferredRegion && preferredRegion in STATES.serverRegions) {
|
||||
@@ -107,7 +107,7 @@ export class XcloudInterceptor {
|
||||
}
|
||||
|
||||
private static async handlePlay(request: RequestInfo | URL, init?: RequestInit) {
|
||||
BxEventBus.Stream.emit('stateLoading', {});
|
||||
BxEventBus.Stream.emit('state.loading', {});
|
||||
|
||||
const PREF_STREAM_TARGET_RESOLUTION = getPref<StreamResolution>(PrefKey.STREAM_RESOLUTION);
|
||||
const PREF_STREAM_PREFERRED_LOCALE = getPref<StreamPreferredLocale>(PrefKey.STREAM_PREFERRED_LOCALE);
|
||||
@@ -189,7 +189,7 @@ export class XcloudInterceptor {
|
||||
return response;
|
||||
}
|
||||
|
||||
BxEventBus.Stream.emit('stateStarting', {});
|
||||
BxEventBus.Stream.emit('state.starting', {});
|
||||
|
||||
const obj = JSON.parse(text);
|
||||
let overrides = JSON.parse(obj.clientStreamingConfigOverrides || '{}') || {};
|
||||
|
@@ -36,7 +36,7 @@ export class XhomeInterceptor {
|
||||
}
|
||||
|
||||
private static async handleConfiguration(request: Request | URL) {
|
||||
BxEventBus.Stream.emit('stateStarting', {});
|
||||
BxEventBus.Stream.emit('state.starting', {});
|
||||
|
||||
const response = await NATIVE_FETCH(request);
|
||||
const obj = await response.clone().json();
|
||||
@@ -125,7 +125,7 @@ export class XhomeInterceptor {
|
||||
}
|
||||
|
||||
private static async handlePlay(request: RequestInfo | URL) {
|
||||
BxEventBus.Stream.emit('stateLoading', {});
|
||||
BxEventBus.Stream.emit('state.loading', {});
|
||||
|
||||
const clone = (request as Request).clone();
|
||||
const body = await clone.json();
|
||||
|
Reference in New Issue
Block a user