UNPKG

fcr-core

Version:

Core APIs for building online scenes

283 lines (281 loc) 13.4 kB
"use strict"; require("core-js/modules/es.array.push.js"); require("core-js/modules/esnext.function.metadata.js"); require("core-js/modules/esnext.map.delete-all.js"); require("core-js/modules/esnext.map.emplace.js"); require("core-js/modules/esnext.map.every.js"); require("core-js/modules/esnext.map.filter.js"); require("core-js/modules/esnext.map.find.js"); require("core-js/modules/esnext.map.find-key.js"); require("core-js/modules/esnext.map.includes.js"); require("core-js/modules/esnext.map.key-of.js"); require("core-js/modules/esnext.map.map-keys.js"); require("core-js/modules/esnext.map.map-values.js"); require("core-js/modules/esnext.map.merge.js"); require("core-js/modules/esnext.map.reduce.js"); require("core-js/modules/esnext.map.some.js"); require("core-js/modules/esnext.map.update.js"); require("core-js/modules/esnext.symbol.metadata.js"); Object.defineProperty(exports, "__esModule", { value: true }); exports.FcrChatConnectorImpl = void 0; require("core-js/modules/es.json.stringify.js"); var _imports = require("../../imports"); var _type = require("../../type"); var _abortableRetry = require("../../utilities/abortable-retry"); var _logger = require("../../utilities/logger"); var _packageInfo = require("../../utilities/package-info"); var _config = require("./config"); var _type2 = require("../../chat-connector/type"); let _initProto; function _applyDecs(e, t, r, n, o, a) { function i(e, t, r) { return function (n, o) { return r && r(n), e[t].call(n, o); }; } function c(e, t) { for (var r = 0; r < e.length; r++) e[r].call(t); return t; } function s(e, t, r, n) { if ("function" != typeof e && (n || void 0 !== e)) throw new TypeError(t + " must " + (r || "be") + " a function" + (n ? "" : " or undefined")); return e; } function applyDec(e, t, r, n, o, a, c, u, l, f, p, d, h) { function m(e) { if (!h(e)) throw new TypeError("Attempted to access private element on non-instance"); } var y, v = t[0], g = t[3], b = !u; if (!b) { r || Array.isArray(v) || (v = [v]); var w = {}, S = [], A = 3 === o ? "get" : 4 === o || d ? "set" : "value"; f ? (p || d ? w = { get: _setFunctionName(function () { return g(this); }, n, "get"), set: function (e) { t[4](this, e); } } : w[A] = g, p || _setFunctionName(w[A], n, 2 === o ? "" : A)) : p || (w = Object.getOwnPropertyDescriptor(e, n)); } for (var P = e, j = v.length - 1; j >= 0; j -= r ? 2 : 1) { var D = v[j], E = r ? v[j - 1] : void 0, I = {}, O = { kind: ["field", "accessor", "method", "getter", "setter", "class"][o], name: n, metadata: a, addInitializer: function (e, t) { if (e.v) throw Error("attempted to call addInitializer after decoration was finished"); s(t, "An initializer", "be", !0), c.push(t); }.bind(null, I) }; try { if (b) (y = s(D.call(E, P, O), "class decorators", "return")) && (P = y);else { var k, F; O.static = l, O.private = f, f ? 2 === o ? k = function (e) { return m(e), w.value; } : (o < 4 && (k = i(w, "get", m)), 3 !== o && (F = i(w, "set", m))) : (k = function (e) { return e[n]; }, (o < 2 || 4 === o) && (F = function (e, t) { e[n] = t; })); var N = O.access = { has: f ? h.bind() : function (e) { return n in e; } }; if (k && (N.get = k), F && (N.set = F), P = D.call(E, d ? { get: w.get, set: w.set } : w[A], O), d) { if ("object" == typeof P && P) (y = s(P.get, "accessor.get")) && (w.get = y), (y = s(P.set, "accessor.set")) && (w.set = y), (y = s(P.init, "accessor.init")) && S.push(y);else if (void 0 !== P) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0"); } else s(P, (p ? "field" : "method") + " decorators", "return") && (p ? S.push(P) : w[A] = P); } } finally { I.v = !0; } } return (p || d) && u.push(function (e, t) { for (var r = S.length - 1; r >= 0; r--) t = S[r].call(e, t); return t; }), p || b || (f ? d ? u.push(i(w, "get"), i(w, "set")) : u.push(2 === o ? w[A] : i.call.bind(w[A])) : Object.defineProperty(e, n, w)), P; } function u(e, t) { return Object.defineProperty(e, Symbol.metadata || Symbol.for("Symbol.metadata"), { configurable: !0, enumerable: !0, value: t }); } if (arguments.length >= 6) var l = a[Symbol.metadata || Symbol.for("Symbol.metadata")]; var f = Object.create(null == l ? null : l), p = function (e, t, r, n) { var o, a, i = [], s = function (t) { return _checkInRHS(t) === e; }, u = new Map(); function l(e) { e && i.push(c.bind(null, e)); } for (var f = 0; f < t.length; f++) { var p = t[f]; if (Array.isArray(p)) { var d = p[1], h = p[2], m = p.length > 3, y = 16 & d, v = !!(8 & d), g = 0 == (d &= 7), b = h + "/" + v; if (!g && !m) { var w = u.get(b); if (!0 === w || 3 === w && 4 !== d || 4 === w && 3 !== d) throw Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + h); u.set(b, !(d > 2) || d); } applyDec(v ? e : e.prototype, p, y, m ? "#" + h : _toPropertyKey(h), d, n, v ? a = a || [] : o = o || [], i, v, m, g, 1 === d, v && m ? s : r); } } return l(o), l(a), i; }(e, t, o, f); return r.length || u(e, f), { e: p, get c() { var t = []; return r.length && [u(applyDec(e, [r], n, e.name, 5, f, t), f), c.bind(null, t, e)]; } }; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _setFunctionName(e, t, n) { "symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : ""); try { Object.defineProperty(e, "name", { configurable: !0, value: n ? n + " " + t : t }); } catch (e) {} return e; } function _checkInRHS(e) { if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null")); return e; } class FcrChatConnectorImpl { static { [_initProto] = _applyDecs(this, [[_imports.trace, 2, "login"], [_imports.trace, 2, "logout"]], []).e; } logger = (_initProto(this), (0, _logger.createLogger)({ prefix: 'FcrChatConnectorImpl' })); _observable = new _imports.AgoraObservable(); _loginPromise = null; _logoutPromise = null; _isLogoutRequested = false; _abortLogin = null; _conn = null; _connectionState = _type2.FcrChatConnectionState.DISCONNECTED; _reconnectionTimer = null; _reconnectPromise = null; constructor(_userId, _api, _chatIpList, _restIpList) { this._userId = _userId; this._api = _api; this._chatIpList = _chatIpList; this._restIpList = _restIpList; this.logger.info(`Chat Version: easemob-websdk@${(0, _packageInfo.getDependenciesInfo)('easemob-websdk')}`); this._addLogObserver(); } async login() { if (this._logoutPromise) { await this._logoutPromise; } if (this._connectionState === _type2.FcrChatConnectionState.CONNECTED) { return; } if (this._loginPromise) { return this._loginPromise; } this._isLogoutRequested = false; this._setConnectionState(_type2.FcrChatConnectionState.CONNECTING); this._loginPromise = this._performLogin(); try { await this._loginPromise; } finally { this._loginPromise = null; } } logout() { if (this._logoutPromise) { return _type.FcrReturnCode.SUCCESS; } this._isLogoutRequested = true; this._logoutPromise = this._performLogout(); return _type.FcrReturnCode.SUCCESS; } getConnectionState() { return this._connectionState; } getConnectionInstance() { return this._conn; } addObserver(observer) { this._observable.addObserver(observer); } removeObserver(observer) { this._observable.removeObserver(observer); } async _performLogin() { const privateConfig = this._getPrivateConfig(); if (privateConfig) { this.logger.info('use private config:', JSON.stringify(privateConfig)); } const [abort, promise] = (0, _abortableRetry.abortableRetry)(async signal => { const tokenResult = await this._api.getUserToken(this._userId); signal.throwIfAborted(); this._initConfig = tokenResult; const appKey = tokenResult.appKey; const connParams = { ..._config.agoraChatConfig, ...privateConfig, appKey }; _imports.EasemobChatSDK.logger.setConsoleLogVisibility(false); this._conn = new _imports.EasemobChatSDK.connection(connParams); this._addEventListeners(this._conn); try { await this._conn.open({ accessToken: this._initConfig.token, user: this._userId }); } catch (error) { this.logger.error('connection open error', error.message); throw error; } this.logger.info(`login success, token: ${this._initConfig.token}, userId: ${this._userId}`); }, { retriesMax: Infinity }); this._abortLogin = abort; const [error] = await promise; this._abortLogin = null; if (error) { await this._closeConnection(); if (error.name === 'AbortError' && this._isLogoutRequested) { return; } throw error; } if (this._isLogoutRequested) { await this._closeConnection(); } } _addEventListeners(conn) { conn.addEventHandler('connectionListener', { onError: e => { this.logger.error('onError', e.message); }, onConnected: () => { this.logger.info('onConnected'); this._handleConnected(); }, onOnline: () => { this.logger.info('onOnline'); this._handleConnected(); }, onOffline: () => { this.logger.info('onOffline'); this._handleDisconnected(); }, onDisconnected: () => { this.logger.info('onDisconnected'); this._handleDisconnected(); } }); } async _performLogout() { try { if (this._reconnectionTimer) { clearTimeout(this._reconnectionTimer); this._reconnectionTimer = null; } if (this._abortLogin) { this._abortLogin(); this._abortLogin = null; } await this._waitForLoginComplete(); await this._closeConnection(); } catch (error) { this.logger.error('logout error', error.message); } finally { this._logoutPromise = null; this._isLogoutRequested = false; } } async _waitForLoginComplete() { if (!this._loginPromise) { return; } try { await this._loginPromise; } catch (error) { this.logger.error('login error during logout', error.message); } } async _closeConnection() { const conn = this._conn; this._conn = null; if (conn) { try { conn.removeEventHandler('connectionListener'); await Promise.resolve(conn.close?.()); } catch (error) { this.logger.error('close connection error', error.message); } } this._setConnectionState(_type2.FcrChatConnectionState.DISCONNECTED); } _setConnectionState(state) { if (this._connectionState === state) { return; } this._connectionState = state; this._observable.notifyObservers('onConnectionStateUpdated', state); } _getPrivateConfig() { if (this._chatIpList?.length || this._restIpList?.length) { const privateConfig = { isHttpDNS: false // https: true, }; if (this._chatIpList?.length) { privateConfig.url = this._chatIpList[0]; } if (this._restIpList?.length) { privateConfig.apiUrl = this._restIpList[0]; } privateConfig.url = 'wss://apaas-private-im.agoralab.co:13003/websocket'; return privateConfig; } } _handleConnected() { if (this._isLogoutRequested) { return; } if (this._connectionState !== _type2.FcrChatConnectionState.CONNECTED) { this._setConnectionState(_type2.FcrChatConnectionState.CONNECTED); } else { // notify again in case of reconnection this._observable.notifyObservers('onConnectionStateUpdated', _type2.FcrChatConnectionState.CONNECTED); } } _handleDisconnected() { if (this._connectionState !== _type2.FcrChatConnectionState.DISCONNECTED) { this._setConnectionState(_type2.FcrChatConnectionState.DISCONNECTED); } if (this._isLogoutRequested) { return; } this._reconnect(); } async _reconnect() { if (this._loginPromise || this._reconnectPromise) { return; } const doReconnect = async () => { await this._closeConnection(); // add a short delay to avoid tight reconnect loops await new Promise(resolve => { this._reconnectionTimer = setTimeout(resolve, _imports.AgoraScheduler.Duration.second(1)); }); this._reconnectionTimer = null; if (this._isLogoutRequested) { return; } try { await this.login(); } catch (error) { this.logger.error('reconnect error', error.message); } }; this._reconnectPromise = doReconnect().finally(() => { this._reconnectPromise = null; }); } _addLogObserver() { this.addObserver((0, _logger.generateLogObserver)(this.logger, [['onConnectionStateUpdated', ['state']]])); } } exports.FcrChatConnectorImpl = FcrChatConnectorImpl;