import type { NativeEventEmitter } from 'react-native'; import { FWEventName } from './models/FWEventName'; import type { LiveStreamChatEvent, LiveStreamEvent, CustomLinkInteractionClickEvent, } from './models/FWEvents'; import LiveStreamModule, { LiveStreamModuleEventEmitter, } from './modules/LiveStreamModule'; import FWLoggerUtil from './utils/FWLoggerUtil'; export type onLiveStreamEventCallback = ( event: LiveStreamEvent ) => Promise | void; export type onLiveStreamChatEventCallback = ( event: LiveStreamChatEvent ) => Promise | void; export type CustomLinkInteractionClickCallback = ( event: CustomLinkInteractionClickEvent ) => Promise | void; /** * The entry class of live stream. */ class LiveStream { private static _instance?: LiveStream; /** * The callback of live stream event. */ public onLiveStreamEvent?: onLiveStreamEventCallback; /** * The callback of live stream chat event. */ public onLiveStreamChatEvent?: onLiveStreamChatEventCallback; private _onCustomLinkInteractionClick?: CustomLinkInteractionClickCallback; /** * This callback is triggered when the user clicks * the link interaction in livestream. * * The host app can customize the click event processing logic of * the link interaction by setting the callback. */ public get onCustomLinkInteractionClick(): | CustomLinkInteractionClickCallback | undefined { return this._onCustomLinkInteractionClick; } public set onCustomLinkInteractionClick( value: CustomLinkInteractionClickCallback | undefined ) { this._onCustomLinkInteractionClick = value; LiveStreamModule.setCustomLinkInteractionClickEnabled(value != null); } private get eventEmitter(): NativeEventEmitter { return LiveStreamModuleEventEmitter; } public static getInstance() { if (!LiveStream._instance) { LiveStream._instance = new LiveStream(); } return LiveStream._instance!; } private constructor() { FWLoggerUtil.log('LiveStream constructor'); this.eventEmitter.addListener(FWEventName.LiveStream, (event) => { if (this.onLiveStreamEvent) { this.onLiveStreamEvent(event ?? {}); } }); this.eventEmitter.addListener(FWEventName.LiveStreamChat, (event) => { if (this.onLiveStreamChatEvent) { this.onLiveStreamChatEvent(event ?? {}); } }); this.eventEmitter.addListener( FWEventName.CustomLinkInteractionClick, (event) => { if (this._onCustomLinkInteractionClick) { this._onCustomLinkInteractionClick(event ?? {}); } } ); } } export default LiveStream;