UNPKG

agora-react-native-rtm

Version:

React Native around the Agora RTM SDKs for Android and iOS agora

186 lines 6.3 kB
import { RTMClient } from '../api/RTMClient'; import { PublishOptions, SubscribeOptions } from '../legacy/AgoraRtmBase'; import { IRtmClientImpl } from '../legacy/impl/IAgoraRtmClientImpl'; import { DeviceEventEmitter, EVENT_TYPE, callIrisApi, handleError, wrapRtmResult } from './IrisRtmEngine'; import { RtmHistoryInternal } from './RtmHistoryInternal'; import { RtmLockInternal } from './RtmLockInternal'; import { RtmPresenceInternal } from './RtmPresenceInternal'; import { RtmStorageInternal } from './RtmStorageInternal'; import { StreamChannelInternal } from './StreamChannelInternal'; export class RtmClientInternal extends RTMClient { _rtmClientImpl = new IRtmClientImpl(); static _event_handlers = []; presence = new RtmPresenceInternal(); storage = new RtmStorageInternal(); lock = new RtmLockInternal(); history = new RtmHistoryInternal(); static _streamChannels = new Map(); event_name_map = { linkState: 'onLinkStateEvent', presence: 'onPresenceEvent', message: 'onMessageEvent', storage: 'onStorageEvent', lock: 'onLockEvent', topic: 'onTopicEvent', tokenPrivilegeWillExpire: 'onTokenPrivilegeWillExpire' }; constructor(config) { super(); if (config !== null && config !== void 0 && config.eventHandler) { Object.entries(config.eventHandler).forEach(([key, value]) => { this.addEventListener(key, value); }); } const jsonParams = { config: config, toJSON: () => { return { config: config }; } }; let result = callIrisApi.call(this, 'RtmClient_create', jsonParams); if (result.result < 0) { throw handleError(result, 'RtmClient_create'); } this._rtmClientImpl.setParameters(JSON.stringify({ 'rtm.app_type': 8 })); } async createStreamChannel(channelName) { let operation = 'createStreamChannel'; try { const status = this._rtmClientImpl.createStreamChannel(channelName); if (status.result < 0) { throw handleError(status, 'createStreamChannel'); } else { const streamChannel = new StreamChannelInternal(channelName); RtmClientInternal._streamChannels.set(channelName, streamChannel); return streamChannel; } } catch (error) { throw handleError(error, operation); } } release() { RtmClientInternal._event_handlers = []; this.removeAllListeners(); const ret = this._rtmClientImpl.release(); return ret; } addEventListener(eventType, listener) { const callback = (eventProcessor, data) => { if (eventProcessor.type(data) !== EVENT_TYPE.RTMEvent) { return; } eventProcessor.func.map(it => { it({ [eventType]: listener }, eventType, data); }); }; // @ts-ignore listener.agoraCallback = callback; const eventName = this.event_name_map[eventType] || eventType; DeviceEventEmitter.addListener(eventName, callback); } removeEventListener(eventType, listener) { DeviceEventEmitter.removeListener(eventType, // @ts-ignore (listener === null || listener === void 0 ? void 0 : listener.agoraCallback) ?? listener); } removeAllListeners(eventType) { RtmClientInternal._event_handlers = []; DeviceEventEmitter.removeAllListeners(eventType); } async login(options) { const token = (options === null || options === void 0 ? void 0 : options.token) || ''; let operation = 'login'; let callBack = 'onLoginResult'; try { const status = this._rtmClientImpl.login(token); let result = await wrapRtmResult(status, operation, callBack); return result; } catch (error) { throw handleError(error, operation); } } async logout() { let operation = 'logout'; let callBack = 'onLogoutResult'; try { const status = this._rtmClientImpl.logout(); let result = await wrapRtmResult(status, operation, callBack); return result; } catch (error) { throw handleError(error, operation); } } async publish(channelName, message, options) { let operation = 'publish'; let callBack = 'onPublishResult'; try { const status = this._rtmClientImpl.publish(channelName, message, message.length, options ? options : new PublishOptions()); let result = await wrapRtmResult(status, operation, callBack); return { ...result, channelName }; } catch (error) { throw handleError(error, operation); } } async subscribe(channelName, options) { let operation = 'subscribe'; let callBack = 'onSubscribeResult'; try { const status = this._rtmClientImpl.subscribe(channelName, options ? options : new SubscribeOptions()); let result = await wrapRtmResult(status, operation, callBack); return { ...result, channelName }; } catch (error) { throw handleError(error, operation); } } async unsubscribe(channelName) { let operation = 'unsubscribe'; let callBack = 'onUnsubscribeResult'; try { const status = this._rtmClientImpl.unsubscribe(channelName); let result = await wrapRtmResult(status, operation, callBack); return { ...result, channelName }; } catch (error) { throw handleError(error, operation); } } async renewToken(token, options) { let operation = 'renewToken'; let callBack = 'onRenewTokenResult'; try { if (!options) { const status = this._rtmClientImpl.renewToken(token); let result = await wrapRtmResult(status, operation, callBack); return result; } else { const channelName = options.channelName; if (!channelName) { throw handleError(new Error('Channel name is required'), operation); } if (!RtmClientInternal._streamChannels.has(channelName)) { throw handleError(new Error('Stream channel not found'), operation); } const status = RtmClientInternal._streamChannels.get(channelName).renewToken(token); let result = await wrapRtmResult(status, operation, callBack); return result; } } catch (error) { throw handleError(error, operation); } } } //# sourceMappingURL=RtmClientInternal.js.map