UNPKG

fcr-core

Version:

Core APIs for building online scenes

272 lines (261 loc) 13.6 kB
import "core-js/modules/es.array.push.js"; import "core-js/modules/esnext.function.metadata.js"; import "core-js/modules/esnext.map.delete-all.js"; import "core-js/modules/esnext.map.emplace.js"; import "core-js/modules/esnext.map.every.js"; import "core-js/modules/esnext.map.filter.js"; import "core-js/modules/esnext.map.find.js"; import "core-js/modules/esnext.map.find-key.js"; import "core-js/modules/esnext.map.includes.js"; import "core-js/modules/esnext.map.key-of.js"; import "core-js/modules/esnext.map.map-keys.js"; import "core-js/modules/esnext.map.map-values.js"; import "core-js/modules/esnext.map.merge.js"; import "core-js/modules/esnext.map.reduce.js"; import "core-js/modules/esnext.map.some.js"; import "core-js/modules/esnext.map.update.js"; import "core-js/modules/esnext.symbol.metadata.js"; let _initProto, _initDecs, _joinRoomDecs; 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; } import { AgoraObservable, retryAttempt, to, trace } from '../imports'; import { RoomControlFactory } from '../room-control/room-control-factory'; import { FcrRoomType } from '../room-control/type'; import { fcrRoomJoinOptionsSchema, stringSchema } from '../schema'; import { FcrReturnCode } from '../type'; import { generateFcrCoreClientError, handleRequestError } from '../utilities/error'; import { FcrJoinHelper, handleJoinRetryFailure } from '../utilities/join-helper'; import { canRetryJoinError } from '../utilities/retry-helpers'; import { createLogger, generateLogObserver } from '../utilities/logger'; import validateParams from '../utilities/validate-params'; import { ROOM_ROUTER_CONSTANTS } from './type'; import { DetailErrorCode, ErrorModuleCode } from '../imports'; import { convertServerRoomTypeToFcrRoomType } from '../utilities/convertRoomType'; /** * @internal */ export class FcrRoomRouterImpl { static { [_initProto] = _applyDecs(this, [[_initDecs, 2, "_init"], [_joinRoomDecs, 2, "joinRoom"], [trace, 2, "leaveRoom"]], []).e; } [(_initDecs = [trace(['roomId']), validateParams(stringSchema)], _joinRoomDecs = [trace(['options']), validateParams(fcrRoomJoinOptionsSchema)], "logger")] = (_initProto(this), createLogger({ prefix: 'FcrRoomRouterImpl' })); _abortController = null; _observable = new AgoraObservable(); _roomControlObservable = { onRoomRouteSwitched: async event => { this._nextRoomRoute = event.targetRouting; this._observable.notifyObservers('onRoomRouteSwitched', this._nextRoomRoute); this.roomControl.removeObserver(this._roomControlObservable); this._roomControl = null; } }; _roomControl = null; _nextRoomRoute = { roomId: '', roomType: FcrRoomType.Mainroom, isJoined: false }; // 接口中新增一个bypass字段,在第一次调用joinRoom的时候不传,在后续收到房间切换后的joinRoom时传true _bypass = undefined; constructor(_rteEngine, _apiService, _config, _chatConnection, _sharedCache, roomId, _monitorControl) { this._rteEngine = _rteEngine; this._apiService = _apiService; this._config = _config; this._chatConnection = _chatConnection; this._sharedCache = _sharedCache; this._monitorControl = _monitorControl; this._init(roomId); this._addLogObserver(); } get roomControl() { if (!this._roomControl) { throw new Error('roomControl is not initialized'); } return this._roomControl; } _init(roomId) { this._nextRoomRoute = { roomId, roomType: FcrRoomType.Mainroom, isJoined: false }; this._bypass = undefined; return FcrReturnCode.SUCCESS; } async joinRoom(options) { this._abortController = new AbortController(); this._validateRoomJoinState(); const [error] = await to(this._executeJoinRoomWithRetry(options)); this._abortController = null; if (error) { this._handleJoinRoomError(error); return Promise.reject(error); } if (!this._roomControl) { return Promise.reject(new Error('Room control is not initialized')); } return Promise.resolve(this._roomControl); } async leaveRoom() { this._abortController?.abort(); this._abortController = null; this._roomControl?.leave(); this._roomControl = null; return FcrReturnCode.SUCCESS; } /** * 验证房间加入状态 * @private */ _validateRoomJoinState() { if (this._nextRoomRoute.isJoined) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_ROOM, DetailErrorCode.UNDEFINED_ERROR, 'Room join failed: The user has an active connection to the room.'); } } /** * 带重试机制的房间加入执行 * @private */ async _executeJoinRoomWithRetry(options) { const abortController = this._abortController; return retryAttempt(() => this._performSingleJoinAttempt(options), [], { retriesMax: ROOM_ROUTER_CONSTANTS.MAX_JOIN_ATTEMPTS }).fail(async ({ error, timeFn: waitBeforeRetry, currentRetry: attemptCount }) => { return handleJoinRetryFailure(error, waitBeforeRetry, attemptCount, canRetryJoinError, () => { this._throwIfAborted(abortController); }, 'join room via router'); }).exec(); } /** * 执行单次房间加入尝试 * @private */ async _performSingleJoinAttempt(options) { const abortController = this._abortController; const roomResponse = await this._performCheckIn(options); this._throwIfAborted(abortController); const roomControl = await this._joinRoomControl(options, roomResponse); this._throwIfAborted(abortController); this._updateRoomState(roomResponse, roomControl); } /** * 执行CheckIn API调用 * @private */ async _performCheckIn(options) { const { userId } = this._rteEngine.getConfig(); const checkInParams = FcrJoinHelper.buildCheckInParams(options, userId, this._nextRoomRoute.roomId, { bypass: this._bypass, avatar: options.avatar }); const { data, ts } = await handleRequestError(() => this._apiService.checkIn(checkInParams), ErrorModuleCode.FCR_ENGINE); return { data, ts }; } /** * 加入房间控制 * @private */ async _joinRoomControl(options, roomResponse) { const { data, ts } = roomResponse; const abortController = this._abortController; const roomControl = this._createRoomControl(data); abortController?.signal.addEventListener('abort', () => { roomControl.leave(); }); await roomControl.join({ ...options, snapshot: data, timestamp: ts, createStreamConfigs: options.createStreamConfigs, roomToken: options.roomToken || this._config.token }); return roomControl; } /** * 更新房间状态 * @private */ _updateRoomState(roomResponse, roomControl) { const { data } = roomResponse; const fcrRoomType = convertServerRoomTypeToFcrRoomType(data.room.roomProperties.roomType); if (fcrRoomType === null) { const fcrError = generateFcrCoreClientError(ErrorModuleCode.FCR_ROOM, DetailErrorCode.UNDEFINED_ERROR, `update room state failed: Unknown room type: ${data.room.roomProperties.roomType}`); throw fcrError; } this._nextRoomRoute = { roomType: fcrRoomType, roomId: data.room.roomInfo.roomUuid, isJoined: true }; this._bypass = 1; this._observable.notifyObservers('onRoomJoined', roomControl); this._roomControl = roomControl; this._roomControl.addObserver(this._roomControlObservable); } /** * 处理房间加入错误 * @private */ _handleJoinRoomError(error) { this.logger.error(`Failed to join the room: ${error.message}`); const fcrError = generateFcrCoreClientError(ErrorModuleCode.FCR_ROOM, DetailErrorCode.LOCAL_HTTP_REQUEST_FAILED, `join room failed: ${error.message}`); this._observable.notifyObservers('onErrorOccurred', fcrError); } /** * 创建房间控制实例 * @param roomResponse 房间响应数据 * @returns 房间控制实例 */ _createRoomControl(roomResponse) { const room = roomResponse.room; const scene = this._rteEngine.createScene({ sceneId: room.roomInfo.roomUuid }); return RoomControlFactory.create({ engine: this._rteEngine, scene, apiService: this._apiService, config: this._config, chatConnection: this._chatConnection, sharedCache: this._sharedCache, roomResponse, monitorControl: this._monitorControl }); } _throwIfAborted(abortController) { if (abortController?.signal.aborted) { throw new Error('Join room attempt aborted'); } } _addLogObserver() { this.addObserver(generateLogObserver(this.logger, [['onErrorOccurred', ['error']], ['onRoomJoined', ['roomControl']], ['onRoomRouteSwitched', ['nextRoomRoute']]])); } addObserver(observer) { this._observable.addObserver(observer); } removeObserver(observer) { this._observable.removeObserver(observer); } }