From 5fb0dec9f275659adb2d1a7cb449d4f6a880f1ff Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:20:46 +0700 Subject: [PATCH] Call methods inside app in EventBus --- dist/better-xcloud.lite.user.js | 22 +++++++++++++++++----- dist/better-xcloud.user.js | 22 +++++++++++++++++----- src/utils/bx-event-bus.ts | 26 ++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/dist/better-xcloud.lite.user.js b/dist/better-xcloud.lite.user.js index 70ed99c..1bdf8ab 100755 --- a/dist/better-xcloud.lite.user.js +++ b/dist/better-xcloud.lite.user.js @@ -159,10 +159,18 @@ window.BxEvent = BxEvent; class BxEventBus { listeners = new Map; group; - static Script = new BxEventBus("script"); - static Stream = new BxEventBus("stream"); - constructor(group) { - this.group = group; + appJsInterfaces; + static Script = new BxEventBus("script", { + "dialog.shown": "onDialogShown", + "dialog.dismissed": "onDialogDismissed" + }); + static Stream = new BxEventBus("stream", { + "state.loading": "onStreamPlaying", + "state.playing": "onStreamPlaying", + "state.stopped": "onStreamStopped" + }); + constructor(group, appJsInterfaces) { + this.group = group, this.appJsInterfaces = appJsInterfaces; } on(event, callback) { if (!this.listeners.has(event)) this.listeners.set(event, new Set); @@ -190,7 +198,11 @@ class BxEventBus { let callbacks = this.listeners.get(event) || []; for (let callback of callbacks) callback(payload); - AppInterface && AppInterface.onEventBus(this.group + "." + event), BX_FLAGS.Debug && BxLogger.warning("EventBus", "emit", event, payload); + if (AppInterface) if (event in this.appJsInterfaces) { + let method = this.appJsInterfaces[event]; + AppInterface[method] && AppInterface[method](); + } else AppInterface.onEventBus(this.group + "." + event); + BX_FLAGS.Debug && BxLogger.warning("EventBus", "emit", event, payload); } } window.BxEventBus = BxEventBus; diff --git a/dist/better-xcloud.user.js b/dist/better-xcloud.user.js index 69639a2..08225d7 100755 --- a/dist/better-xcloud.user.js +++ b/dist/better-xcloud.user.js @@ -188,10 +188,18 @@ var GamepadKeyName = { class BxEventBus { listeners = new Map; group; - static Script = new BxEventBus("script"); - static Stream = new BxEventBus("stream"); - constructor(group) { - this.group = group; + appJsInterfaces; + static Script = new BxEventBus("script", { + "dialog.shown": "onDialogShown", + "dialog.dismissed": "onDialogDismissed" + }); + static Stream = new BxEventBus("stream", { + "state.loading": "onStreamPlaying", + "state.playing": "onStreamPlaying", + "state.stopped": "onStreamStopped" + }); + constructor(group, appJsInterfaces) { + this.group = group, this.appJsInterfaces = appJsInterfaces; } on(event, callback) { if (!this.listeners.has(event)) this.listeners.set(event, new Set); @@ -219,7 +227,11 @@ class BxEventBus { let callbacks = this.listeners.get(event) || []; for (let callback of callbacks) callback(payload); - AppInterface && AppInterface.onEventBus(this.group + "." + event), BX_FLAGS.Debug && BxLogger.warning("EventBus", "emit", event, payload); + if (AppInterface) if (event in this.appJsInterfaces) { + let method = this.appJsInterfaces[event]; + AppInterface[method] && AppInterface[method](); + } else AppInterface.onEventBus(this.group + "." + event); + BX_FLAGS.Debug && BxLogger.warning("EventBus", "emit", event, payload); } } window.BxEventBus = BxEventBus; diff --git a/src/utils/bx-event-bus.ts b/src/utils/bx-event-bus.ts index c78ba7b..f0d53d9 100644 --- a/src/utils/bx-event-bus.ts +++ b/src/utils/bx-event-bus.ts @@ -44,12 +44,21 @@ type StreamEvents = { export class BxEventBus> { private listeners: Map>> = new Map(); private group: string; + private appJsInterfaces: { [key in keyof TEvents]?: string }; - static readonly Script = new BxEventBus('script'); - static readonly Stream = new BxEventBus('stream'); + static readonly Script = new BxEventBus('script', { + 'dialog.shown': 'onDialogShown', + 'dialog.dismissed': 'onDialogDismissed', + }); + static readonly Stream = new BxEventBus('stream', { + 'state.loading': 'onStreamPlaying', + 'state.playing': 'onStreamPlaying', + 'state.stopped': 'onStreamStopped', + }); - constructor(group: string) { + constructor(group: string, appJsInterfaces: { [key in keyof TEvents]?: string }) { this.group = group; + this.appJsInterfaces = appJsInterfaces; } on(event: K, callback: EventCallback): void { @@ -101,7 +110,16 @@ export class BxEventBus> { callback(payload); } - AppInterface && AppInterface.onEventBus(this.group + '.' + (event as string)); + // Call method inside Android app + if (AppInterface) { + if (event in this.appJsInterfaces) { + const method = this.appJsInterfaces[event]; + AppInterface[method] && AppInterface[method](); + } else { + AppInterface.onEventBus(this.group + '.' + (event as string)); + } + } + BX_FLAGS.Debug && BxLogger.warning('EventBus', 'emit', event, payload); } }