fcr-core
Version:
Core APIs for building online scenes
350 lines (343 loc) • 18 kB
JavaScript
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;
import "core-js/modules/es.json.stringify.js";
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 { Room } from '@netless/forge-room';
import { RTMProvider_2_2 } from '@netless/forge-rtm';
import { WhiteboardApplication } from '@netless/forge-whiteboard';
import { DetailErrorCode, ErrorModuleCode } from '../../../imports';
import { AgoraObservable, AgoraScheduler, jsonstring, retryAttempt, to, trace } from '../../../imports';
import { FcrConnectionState, FcrReturnCode } from '../../../type';
import { generateFcrCoreClientError, FcrErrorModuleCode, FcrErrorCode } from '../../../utilities/error';
import { createLogger, generateLogObserver } from '../../../utilities/logger';
import { INIT_CONFIG_FETCH_INTERVAL, WHITEBOARD_APP_ID } from '../constant';
import { FcrBoardMainWindowImpl } from './main-window';
/** @param isNeedLaunch 是否需要调用白板 luanch 函数 */
const WHITEBOARD_ROOM_JOIN_TIMEOUT = 20000;
export class FcrBaseWhiteboardControlImpl {
static {
[_initProto] = _applyDecs(this, [[trace, 2, "open"], [trace, 2, "close"], [trace, 2, "release"], [trace, 2, "getConnectionState"], [trace, 2, "getMainWindow"]], []).e;
}
logger = (_initProto(this), createLogger({
prefix: 'FcrBaseWhiteboardControl'
}));
observable = new AgoraObservable();
boardView = null;
boardRoom = null;
whiteboard = null;
connectState = FcrConnectionState.DISCONNECTED;
boardRoomConfig = null;
_waitPromise = null;
_waitPromiseResolve = null;
_waitPromiseReject = null;
_openAbortController = null;
_openState = {
isOpen: false,
isNeedLaunch: undefined
};
constructor(rtmClient, config, boardRoomConfigFetcher, api) {
this.rtmClient = rtmClient;
this.config = config;
this.api = api;
this.boardRoomConfigFetcherTask = AgoraScheduler.shared.addPollingTask(async () => {
const boardRoomConfig = await boardRoomConfigFetcher();
this.boardRoomConfig = boardRoomConfig;
this.logger.info(`fetch board room config success, config: ${jsonstring(boardRoomConfig)}`);
this.boardRoomConfigFetcherTask.stop();
if (this._openState.isOpen) {
this.connect(this._openState.isNeedLaunch);
}
}, INIT_CONFIG_FETCH_INTERVAL);
this.addLogObserver();
}
async open() {
this.logger.error('subclass must implement open method, not call base class method');
throw generateFcrCoreClientError(FcrErrorModuleCode.FCR_ROOM_WHITEBOARD, FcrErrorCode.UNDEFINED_ERROR, 'subclass must implement open method, not call base class method');
}
async close() {
this._openState = {
isOpen: false,
isNeedLaunch: undefined
};
if (!this.boardRoomConfigFetcherTask.isStopped) {
this.boardRoomConfigFetcherTask.stop();
}
if (this._waitPromiseReject) {
this._waitPromiseReject(new Error('closed before connection established'));
this._clearWaitPromise();
}
try {
if (this._openAbortController) {
this._openAbortController.abort();
this._openAbortController = null;
}
const boardRoom = this.boardRoom;
this.boardRoom = null;
this.boardView = null;
this.whiteboard = null;
if (boardRoom) {
this.logger.info('ApplicationManager.removeAllListeners calling');
boardRoom.applicationManager.removeAllListeners();
this.logger.info('Room.leaveRoom calling');
await boardRoom.leaveRoom();
}
} catch (e) {
this.logger.error('leave board room failed', e);
} finally {
this.updateConnectionState(FcrConnectionState.DISCONNECTED);
}
return FcrReturnCode.SUCCESS;
}
async release() {
return this.close();
}
getConnectionState() {
return this.connectState;
}
getMainWindow() {
return this.boardView;
}
addObserver(observer) {
this.observable.addObserver(observer);
}
removeObserver(observer) {
this.observable.removeObserver(observer);
}
internalOpen(isNeedLaunch) {
this._openState = {
isOpen: true,
isNeedLaunch: isNeedLaunch
};
const isReadyToConnect = this._checkForConnect();
if (isReadyToConnect) {
return this.connect(isNeedLaunch);
}
if (!this._waitPromise) {
this._waitPromise = new Promise((resolve, reject) => {
this._waitPromiseResolve = resolve;
this._waitPromiseReject = reject;
this.logger.info('waiting for forge config to be ready');
});
}
return this._waitPromise;
}
updateConnectionState(state) {
if (this.connectState === state) {
return;
}
const oldState = this.connectState;
this.connectState = state;
this.logger.info(`Connection state updated: ${FcrConnectionState[oldState]} -> ${FcrConnectionState[state]}`);
this.observable.notifyObservers('onConnectionStateUpdated', state);
}
async connect(isNeedLaunch) {
if (this.boardView) {
this.logger.info('board already connected, returning existing board view');
return this.boardView;
}
const abortController = new AbortController();
this._openAbortController = abortController;
return new Promise(async (resolve, reject) => {
// 监听中止信号
abortController.signal.addEventListener('abort', () => {
reject(new Error('join board aborted'));
});
const {
userId,
userName,
ipList
} = this.config;
const {
boardAppId,
boardId,
boardRegion,
boardToken
// boardPerformance = false,
} = this.boardRoomConfig || {};
if (!boardAppId) {
throw generateFcrCoreClientError(ErrorModuleCode.FCR_ROOM_WHITEBOARD, DetailErrorCode.UNDEFINED_ERROR, 'boardAppId is required');
}
if (!boardId) {
throw generateFcrCoreClientError(ErrorModuleCode.FCR_ROOM_WHITEBOARD, DetailErrorCode.UNDEFINED_ERROR, 'boardId is required');
}
if (!boardToken) {
throw generateFcrCoreClientError(ErrorModuleCode.FCR_ROOM_WHITEBOARD, DetailErrorCode.UNDEFINED_ERROR, 'boardToken is required');
}
const whiteboardOption = this.getWhiteboardOption();
let width = whiteboardOption.width;
let height = whiteboardOption.height;
if (!width) {
this.logger.warn(`board width is a invalid vlaue: ${width}, using default value 800`);
whiteboardOption.width = 800;
}
if (!height) {
this.logger.warn(`board height is a invalid value: ${height}, using default value 600`);
whiteboardOption.height = 600;
}
this.logger.info(`open board with boardId: ${boardId}, boardToken: ${boardToken}, region: ${boardRegion}, appIdentifier: ${boardAppId} whiteboardOption: ${jsonstring(whiteboardOption)}`);
let endpoint = undefined;
if (ipList !== undefined) {
if (Array.isArray(ipList) && ipList[0]) {
this.logger.info(`use ipList from config: ${jsonstring(ipList)}`);
endpoint = ipList[0];
} else {
this.logger.error(`ipList in config is empty or invalid: ${jsonstring(ipList)}, ignore ipList config`);
}
}
const rtmProvider = new RTMProvider_2_2(this.rtmClient);
const wbRoom = new Room(boardId, rtmProvider);
this.logger.info('ApplicationManager.registerApplication calling');
wbRoom.applicationManager.registerApplication(WhiteboardApplication);
try {
this.updateConnectionState(FcrConnectionState.CONNECTING);
let error = null;
let boardView = null;
let whiteboard = null;
let boardRoom = wbRoom;
const appId = this.getApplicationId();
let getWhiteboard;
// 第一个开启白板的人需要调用 launch 方法, 其他人监听 launch 事件获取白板实例
if (isNeedLaunch) {
getWhiteboard = () => {
this.logger.info(`ApplicationManager.launchApplication calling, appId: ${appId}, option: ${jsonstring(whiteboardOption)}`);
return wbRoom.applicationManager.launchApplication(WhiteboardApplication, whiteboardOption, appId);
};
} else {
const launchPromise = new Promise((launchPromseResolve, launchPromseReject) => {
this.logger.info('ApplicationManager.once launch event listening');
wbRoom.applicationManager.once('launch', (appId, app) => {
this.logger.info(`ApplicationManager.launch event received, appId: ${appId}`);
launchPromseResolve(app);
});
});
getWhiteboard = () => {
return launchPromise;
};
}
[error] = await to(retryAttempt(async () => {
const joinRoomParams = {
userId,
nickName: userName || userId,
roomToken: boardToken,
sdkConfig: {
region: endpoint ? 'private' : boardRegion,
appIdentifier: boardAppId
},
endpoint,
writable: true
// verboseLog: true,
};
this.logger.info(`Room.joinRoom calling, params: ${jsonstring(joinRoomParams)}, timeout: ${WHITEBOARD_ROOM_JOIN_TIMEOUT}`);
// 20 秒超时
await wbRoom.joinRoom(joinRoomParams, WHITEBOARD_ROOM_JOIN_TIMEOUT);
boardRoom = wbRoom;
whiteboard = await getWhiteboard();
if (isNeedLaunch) {
// 在同一个房间内只有第一次调用 launchApplication 时传入的宽高参数会生效, 后续的调用不会生效, 所以需要在这里更新宽高
// 只有 launch 的用户需要更新宽、高, 白板会自动同步给后加入的用户
this.logger.info(`Whiteboard.updateViewport calling, width: ${width}, height: ${height}`);
whiteboard.updateViewport(width, height);
}
}, [], {
retriesMax: 0
}).fail(async ({
error,
timeFn,
currentRetry
}) => {
if (abortController.signal.aborted) {
throw new Error('join board aborted');
}
this.logger.error(`failed to join board, error: ${error.message}, current retry: ${currentRetry}`, error);
await timeFn();
return true;
}).exec());
if (abortController.signal.aborted) {
throw new Error('join board aborted');
}
if (error) {
this.logger.error(`join board failed, ${error.message}-${JSON.stringify(error)}`);
throw error;
}
this.logger.info('Whiteboard camera settings: enableCameraByMouse=false, enableCameraByTouch=false');
whiteboard.enableCameraByMouse = false;
whiteboard.enableCameraByTouch = false;
this.logger.info('Whiteboard.setViewModeToMain calling');
whiteboard.setViewModeToMain();
boardView = new FcrBoardMainWindowImpl(whiteboard, wbRoom, appId, this.config.roomId, this.config.userId, this.api);
boardView.setBackgroundColor(this.getBackgroundColor());
this.whiteboard = whiteboard;
this.boardView = boardView;
this.boardRoom = boardRoom;
// if (isNeedLaunch) {
// // 所有人开启白板时都默认没有开启写权限, 需要写权限的地方需要自己手动开启
// boardView.internalSetWritable(false);
// }
// boardView.setPerformanceMode(boardPerformance);
this.updateConnectionState(FcrConnectionState.CONNECTED);
resolve(boardView);
if (this._waitPromiseResolve) {
this._waitPromiseResolve(boardView);
this._clearWaitPromise();
}
} catch (e) {
this.logger.error(`join board failed`);
this.updateConnectionState(FcrConnectionState.DISCONNECTED);
reject(e);
if (this._waitPromiseReject) {
this._waitPromiseReject(e);
this._clearWaitPromise();
}
} finally {
this._openAbortController = null;
}
});
}
getApplicationId() {
return WHITEBOARD_APP_ID;
}
getWhiteboardOption() {
return {
width: 800,
height: 600,
defaultToolbarStyle: {
tool: 'laser'
},
maxScaleRatio: 1
};
}
getBackgroundColor() {
return 'rgba(0, 0, 0, 0)';
}
addLogObserver() {
this.addObserver(generateLogObserver(this.logger, [['onConnectionStateUpdated', ['state']]]));
}
_checkForConnect() {
return !!this.boardRoomConfig;
}
_clearWaitPromise() {
this._waitPromiseResolve = null;
this._waitPromiseReject = null;
this._waitPromise = null;
}
}