Call methods inside app in EventBus

This commit is contained in:
redphx 2024-12-08 22:20:46 +07:00
parent 4ffc034076
commit 5fb0dec9f2
3 changed files with 56 additions and 14 deletions

View File

@ -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;

View File

@ -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;

View File

@ -44,12 +44,21 @@ type StreamEvents = {
export class BxEventBus<TEvents extends Record<string, any>> {
private listeners: Map<keyof TEvents, Set<EventCallback<any>>> = new Map();
private group: string;
private appJsInterfaces: { [key in keyof TEvents]?: string };
static readonly Script = new BxEventBus<ScriptEvents>('script');
static readonly Stream = new BxEventBus<StreamEvents>('stream');
static readonly Script = new BxEventBus<ScriptEvents>('script', {
'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.appJsInterfaces = appJsInterfaces;
}
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);
}
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);
}
}