agora-react-native-rtm
Version:
React Native around the Agora RTM SDKs for Android and iOS agora
193 lines (192 loc) • 6.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.RtmClientInternal = void 0;
var _RTMClient = require("../api/RTMClient");
var _AgoraRtmBase = require("../legacy/AgoraRtmBase");
var _IAgoraRtmClientImpl = require("../legacy/impl/IAgoraRtmClientImpl");
var _IrisRtmEngine = require("./IrisRtmEngine");
var _RtmHistoryInternal = require("./RtmHistoryInternal");
var _RtmLockInternal = require("./RtmLockInternal");
var _RtmPresenceInternal = require("./RtmPresenceInternal");
var _RtmStorageInternal = require("./RtmStorageInternal");
var _StreamChannelInternal = require("./StreamChannelInternal");
class RtmClientInternal extends _RTMClient.RTMClient {
_rtmClientImpl = new _IAgoraRtmClientImpl.IRtmClientImpl();
static _event_handlers = [];
presence = new _RtmPresenceInternal.RtmPresenceInternal();
storage = new _RtmStorageInternal.RtmStorageInternal();
lock = new _RtmLockInternal.RtmLockInternal();
history = new _RtmHistoryInternal.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 = _IrisRtmEngine.callIrisApi.call(this, 'RtmClient_create', jsonParams);
if (result.result < 0) {
throw (0, _IrisRtmEngine.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 (0, _IrisRtmEngine.handleError)(status, 'createStreamChannel');
} else {
const streamChannel = new _StreamChannelInternal.StreamChannelInternal(channelName);
RtmClientInternal._streamChannels.set(channelName, streamChannel);
return streamChannel;
}
} catch (error) {
throw (0, _IrisRtmEngine.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) !== _IrisRtmEngine.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;
_IrisRtmEngine.DeviceEventEmitter.addListener(eventName, callback);
}
removeEventListener(eventType, listener) {
_IrisRtmEngine.DeviceEventEmitter.removeListener(eventType,
// @ts-ignore
(listener === null || listener === void 0 ? void 0 : listener.agoraCallback) ?? listener);
}
removeAllListeners(eventType) {
RtmClientInternal._event_handlers = [];
_IrisRtmEngine.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 (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return result;
} catch (error) {
throw (0, _IrisRtmEngine.handleError)(error, operation);
}
}
async logout() {
let operation = 'logout';
let callBack = 'onLogoutResult';
try {
const status = this._rtmClientImpl.logout();
let result = await (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return result;
} catch (error) {
throw (0, _IrisRtmEngine.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 _AgoraRtmBase.PublishOptions());
let result = await (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return {
...result,
channelName
};
} catch (error) {
throw (0, _IrisRtmEngine.handleError)(error, operation);
}
}
async subscribe(channelName, options) {
let operation = 'subscribe';
let callBack = 'onSubscribeResult';
try {
const status = this._rtmClientImpl.subscribe(channelName, options ? options : new _AgoraRtmBase.SubscribeOptions());
let result = await (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return {
...result,
channelName
};
} catch (error) {
throw (0, _IrisRtmEngine.handleError)(error, operation);
}
}
async unsubscribe(channelName) {
let operation = 'unsubscribe';
let callBack = 'onUnsubscribeResult';
try {
const status = this._rtmClientImpl.unsubscribe(channelName);
let result = await (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return {
...result,
channelName
};
} catch (error) {
throw (0, _IrisRtmEngine.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 (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return result;
} else {
const channelName = options.channelName;
if (!channelName) {
throw (0, _IrisRtmEngine.handleError)(new Error('Channel name is required'), operation);
}
if (!RtmClientInternal._streamChannels.has(channelName)) {
throw (0, _IrisRtmEngine.handleError)(new Error('Stream channel not found'), operation);
}
const status = RtmClientInternal._streamChannels.get(channelName).renewToken(token);
let result = await (0, _IrisRtmEngine.wrapRtmResult)(status, operation, callBack);
return result;
}
} catch (error) {
throw (0, _IrisRtmEngine.handleError)(error, operation);
}
}
}
exports.RtmClientInternal = RtmClientInternal;
//# sourceMappingURL=RtmClientInternal.js.map