mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-06-06 23:57:19 +02:00
Call methods inside app in EventBus
This commit is contained in:
parent
4ffc034076
commit
5fb0dec9f2
22
dist/better-xcloud.lite.user.js
vendored
22
dist/better-xcloud.lite.user.js
vendored
@ -159,10 +159,18 @@ window.BxEvent = BxEvent;
|
|||||||
class BxEventBus {
|
class BxEventBus {
|
||||||
listeners = new Map;
|
listeners = new Map;
|
||||||
group;
|
group;
|
||||||
static Script = new BxEventBus("script");
|
appJsInterfaces;
|
||||||
static Stream = new BxEventBus("stream");
|
static Script = new BxEventBus("script", {
|
||||||
constructor(group) {
|
"dialog.shown": "onDialogShown",
|
||||||
this.group = group;
|
"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) {
|
on(event, callback) {
|
||||||
if (!this.listeners.has(event)) this.listeners.set(event, new Set);
|
if (!this.listeners.has(event)) this.listeners.set(event, new Set);
|
||||||
@ -190,7 +198,11 @@ class BxEventBus {
|
|||||||
let callbacks = this.listeners.get(event) || [];
|
let callbacks = this.listeners.get(event) || [];
|
||||||
for (let callback of callbacks)
|
for (let callback of callbacks)
|
||||||
callback(payload);
|
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;
|
window.BxEventBus = BxEventBus;
|
||||||
|
22
dist/better-xcloud.user.js
vendored
22
dist/better-xcloud.user.js
vendored
@ -188,10 +188,18 @@ var GamepadKeyName = {
|
|||||||
class BxEventBus {
|
class BxEventBus {
|
||||||
listeners = new Map;
|
listeners = new Map;
|
||||||
group;
|
group;
|
||||||
static Script = new BxEventBus("script");
|
appJsInterfaces;
|
||||||
static Stream = new BxEventBus("stream");
|
static Script = new BxEventBus("script", {
|
||||||
constructor(group) {
|
"dialog.shown": "onDialogShown",
|
||||||
this.group = group;
|
"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) {
|
on(event, callback) {
|
||||||
if (!this.listeners.has(event)) this.listeners.set(event, new Set);
|
if (!this.listeners.has(event)) this.listeners.set(event, new Set);
|
||||||
@ -219,7 +227,11 @@ class BxEventBus {
|
|||||||
let callbacks = this.listeners.get(event) || [];
|
let callbacks = this.listeners.get(event) || [];
|
||||||
for (let callback of callbacks)
|
for (let callback of callbacks)
|
||||||
callback(payload);
|
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;
|
window.BxEventBus = BxEventBus;
|
||||||
|
@ -44,12 +44,21 @@ type StreamEvents = {
|
|||||||
export class BxEventBus<TEvents extends Record<string, any>> {
|
export class BxEventBus<TEvents extends Record<string, any>> {
|
||||||
private listeners: Map<keyof TEvents, Set<EventCallback<any>>> = new Map();
|
private listeners: Map<keyof TEvents, Set<EventCallback<any>>> = new Map();
|
||||||
private group: string;
|
private group: string;
|
||||||
|
private appJsInterfaces: { [key in keyof TEvents]?: string };
|
||||||
|
|
||||||
static readonly Script = new BxEventBus<ScriptEvents>('script');
|
static readonly Script = new BxEventBus<ScriptEvents>('script', {
|
||||||
static readonly Stream = new BxEventBus<StreamEvents>('stream');
|
'dialog.shown': 'onDialogShown',
|
||||||
|
'dialog.dismissed': 'onDialogDismissed',
|
||||||
|
});
|
||||||
|
static readonly Stream = new BxEventBus<StreamEvents>('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.group = group;
|
||||||
|
this.appJsInterfaces = appJsInterfaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
on<K extends keyof TEvents>(event: K, callback: EventCallback<TEvents[K]>): void {
|
on<K extends keyof TEvents>(event: K, callback: EventCallback<TEvents[K]>): void {
|
||||||
@ -101,7 +110,16 @@ export class BxEventBus<TEvents extends Record<string, any>> {
|
|||||||
callback(payload);
|
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);
|
BX_FLAGS.Debug && BxLogger.warning('EventBus', 'emit', event, payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user