UNPKG

@microsoft/omnichannel-chat-sdk

Version:
330 lines 17.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VoiceVideoCallingProxy = void 0; var SecondaryChannelType; (function (SecondaryChannelType) { SecondaryChannelType[SecondaryChannelType["Voice"] = 192370000] = "Voice"; SecondaryChannelType[SecondaryChannelType["Video"] = 192380000] = "Video"; SecondaryChannelType[SecondaryChannelType["Cobrowse"] = 192390000] = "Cobrowse"; })(SecondaryChannelType || (SecondaryChannelType = {})); var SecondaryChannelEvents; (function (SecondaryChannelEvents) { SecondaryChannelEvents["Accept"] = "accept"; SecondaryChannelEvents["Reject"] = "reject"; SecondaryChannelEvents["Connect"] = "connect"; SecondaryChannelEvents["End"] = "end"; })(SecondaryChannelEvents || (SecondaryChannelEvents = {})); var VoiceVideoCallingProxy = /** @class */ (function () { function VoiceVideoCallingProxy() { this.debug = false; this.proxy = window.Microsoft.OmniChannel.SDK.VoiceVideoCalling; this.proxyInstance = this.proxy.getInstance(); } VoiceVideoCallingProxy.getInstance = function () { if (!this._instance) { this._instance = new VoiceVideoCallingProxy(); } return this._instance; }; /* istanbul ignore next */ VoiceVideoCallingProxy.prototype.setDebug = function (flag) { this.debug = flag; }; VoiceVideoCallingProxy.prototype.load = function (params) { if (params === void 0) { params = {}; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { if (params.logger) { this.logger = params.logger; } this.proxyInstance.load(params); return [2 /*return*/]; }); }); }; VoiceVideoCallingProxy.prototype.isInitialized = function () { return this.proxyInstance.isInitialized(); }; VoiceVideoCallingProxy.prototype.initialize = function (params) { var _a, _b, _c, _d, _e, _f; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_g) { switch (_g.label) { case 0: /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy] VoiceVideoCallingParams: " + JSON.stringify(params)); this.callingParams = params; this.callId = (_a = this.callingParams) === null || _a === void 0 ? void 0 : _a.chatToken.chatId; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][initialize] _isLoaded: " + this.proxyInstance._isLoaded); if (!!this.proxyInstance._isLoaded) return [3 /*break*/, 2]; return [4 /*yield*/, this.proxyInstance.load(this.logger || {})]; case 1: _g.sent(); _g.label = 2; case 2: this.proxyInstance.initialize({ skypeid: (_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.chatToken.visitorId, accesstoken: (_c = this.callingParams) === null || _c === void 0 ? void 0 : _c.chatToken.token, environment: ((_d = this.callingParams) === null || _d === void 0 ? void 0 : _d.environment) || 'prod', selfVideoHTMLElementId: (_e = this.callingParams) === null || _e === void 0 ? void 0 : _e.selfVideoHTMLElementId, remoteVideoHTMLElementId: (_f = this.callingParams) === null || _f === void 0 ? void 0 : _f.remoteVideoHTMLElementId }); return [2 /*return*/]; } }); }); }; VoiceVideoCallingProxy.prototype.addEventListener = function (eventName, callback) { var _this = this; this.proxyInstance.registerEvent(eventName, function (params) { var _a; var callId = params.callId; /* istanbul ignore next */ _this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "] callId: " + callId); if (callId !== ((_a = _this.callingParams) === null || _a === void 0 ? void 0 : _a.chatToken.chatId)) { return; } callback(params); }); }; VoiceVideoCallingProxy.prototype.isMicrophoneMuted = function () { var callId = this.callId; return this.proxyInstance.isMicrophoneMuted({ callId: callId }); }; VoiceVideoCallingProxy.prototype.acceptCall = function (params) { var _a, _b; if (params === void 0) { params = {}; } return __awaiter(this, void 0, void 0, function () { var callId, body; return __generator(this, function (_c) { callId = this.callId; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][acceptCall] callId: " + callId); /* istanbul ignore next */ this.debug && console.debug(params); this.proxyInstance.acceptCall({ callId: callId, withVideo: params.withVideo || false }); body = { SecondaryChannelType: params.withVideo ? SecondaryChannelType.Video : SecondaryChannelType.Voice, SecondaryChannelEventType: SecondaryChannelEvents.Accept }; try { (_a = this.callingParams) === null || _a === void 0 ? void 0 : _a.OCClient.makeSecondaryChannelEventRequest((_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.chatToken.requestId, body); /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][acceptCall][makeSecondaryChannelEventRequest]"); } catch (e) { console.error("[VoiceVideoCallingProxy][acceptCall][makeSecondaryChannelEventRequest] Failure " + e); } return [2 /*return*/]; }); }); }; VoiceVideoCallingProxy.prototype.rejectCall = function () { var _a, _b; return __awaiter(this, void 0, void 0, function () { var callId, body; return __generator(this, function (_c) { callId = this.callId; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][rejectCall] callId: " + callId); this.proxyInstance.rejectCall({ callId: callId }); body = { SecondaryChannelType: this.isRemoteVideoEnabled() ? SecondaryChannelType.Video : SecondaryChannelType.Voice, SecondaryChannelEventType: SecondaryChannelEvents.Reject }; try { (_a = this.callingParams) === null || _a === void 0 ? void 0 : _a.OCClient.makeSecondaryChannelEventRequest((_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.chatToken.requestId, body); /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][rejectCall][makeSecondaryChannelEventRequest]"); } catch (e) { console.error("[VoiceVideoCallingProxy][rejectCall][makeSecondaryChannelEventRequest] Failure " + e); return [2 /*return*/, e]; } return [2 /*return*/]; }); }); }; VoiceVideoCallingProxy.prototype.stopCall = function () { return __awaiter(this, void 0, void 0, function () { var callId; return __generator(this, function (_a) { callId = this.callId; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][stopCall] callId: " + callId); this.clearRemoteVideoElementChildren(); return [2 /*return*/, this.proxyInstance.stopCall({ callId: callId })]; }); }); }; VoiceVideoCallingProxy.prototype.toggleMute = function () { return __awaiter(this, void 0, void 0, function () { var callId; return __generator(this, function (_a) { callId = this.callId; return [2 /*return*/, this.proxyInstance.toggleMute({ callId: callId })]; }); }); }; VoiceVideoCallingProxy.prototype.isRemoteVideoEnabled = function () { var callId = this.callId; return this.proxyInstance.isRemoteVideoEnabled({ callId: callId }); }; VoiceVideoCallingProxy.prototype.isLocalVideoEnabled = function () { var callId = this.callId; return this.proxyInstance.isLocalVideoEnabled({ callId: callId }); }; VoiceVideoCallingProxy.prototype.toggleLocalVideo = function () { return __awaiter(this, void 0, void 0, function () { var callId; return __generator(this, function (_a) { callId = this.callId; return [2 /*return*/, this.proxyInstance.toggleLocalVideo({ callId: callId })]; }); }); }; VoiceVideoCallingProxy.prototype.isInACall = function () { var callId = this.callId; return this.proxyInstance.isInACall({ callId: callId }); }; VoiceVideoCallingProxy.prototype.renderVideoStreams = function () { var callId = this.callId; return this.proxyInstance.renderVideoStreams({ callId: callId }); }; VoiceVideoCallingProxy.prototype.disposeVideoRenderers = function () { var callId = this.callId; return this.proxyInstance.disposeVideoRenderers({ callId: callId }); }; VoiceVideoCallingProxy.prototype.close = function () { /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][close]"); this.proxyInstance.dispose(); this.callingParams = undefined; this.callId = undefined; }; VoiceVideoCallingProxy.prototype.onCallAdded = function (callback) { var eventName = 'callAdded'; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "]"); this.addEventListener(eventName, callback); }; VoiceVideoCallingProxy.prototype.onLocalVideoStreamAdded = function (callback) { var eventName = 'localVideoStreamAdded'; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "]"); this.addEventListener(eventName, callback); }; VoiceVideoCallingProxy.prototype.onLocalVideoStreamRemoved = function (callback) { var eventName = 'localVideoStreamRemoved'; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "]"); this.addEventListener(eventName, callback); }; VoiceVideoCallingProxy.prototype.onRemoteVideoStreamAdded = function (callback) { var eventName = 'remoteVideoStreamAdded'; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "]"); this.addEventListener(eventName, callback); }; VoiceVideoCallingProxy.prototype.onRemoteVideoStreamRemoved = function (callback) { var eventName = 'remoteVideoStreamRemoved'; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "]"); this.addEventListener(eventName, callback); }; VoiceVideoCallingProxy.prototype.onCallDisconnected = function (callback) { var _this = this; var eventName = 'callDisconnected'; /* istanbul ignore next */ this.debug && console.debug("[VoiceVideoCallingProxy][" + eventName + "]"); this.addEventListener(eventName, function (params) { return __awaiter(_this, void 0, void 0, function () { var body; var _a, _b; return __generator(this, function (_c) { body = { SecondaryChannelType: SecondaryChannelType.Voice, SecondaryChannelEventType: SecondaryChannelEvents.End }; try { (_a = this.callingParams) === null || _a === void 0 ? void 0 : _a.OCClient.makeSecondaryChannelEventRequest((_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.chatToken.requestId, body); this.debug && console.debug("[VoiceVideoCallingProxy][onCallDisconnected][makeSecondaryChannelEventRequest]"); } catch (e) { console.error("[VoiceVideoCallingProxy][onCallDisconnected][makeSecondaryChannelEventRequest] Failure " + e); } this.clearRemoteVideoElementChildren(); callback(params); return [2 /*return*/]; }); }); }); }; VoiceVideoCallingProxy.prototype.clearRemoteVideoElementChildren = function () { var _a, _b; // Remove remoteVideoHTMLElement children if any or video won't be rendered properly if ((_a = this.callingParams) === null || _a === void 0 ? void 0 : _a.remoteVideoHTMLElementId) { var remoteVideoElement = document.getElementById((_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.remoteVideoHTMLElementId); while (remoteVideoElement === null || remoteVideoElement === void 0 ? void 0 : remoteVideoElement.firstChild) { /* istanbul ignore next */ this.debug && console.debug('[VoiceVideoCallingProxy][clearRemoteVideoElement]'); remoteVideoElement.firstChild.remove(); } } }; return VoiceVideoCallingProxy; }()); exports.VoiceVideoCallingProxy = VoiceVideoCallingProxy; var createVoiceVideoCalling = function (params) { if (params === void 0) { params = {}; } return __awaiter(void 0, void 0, void 0, function () { var proxy; return __generator(this, function (_a) { switch (_a.label) { case 0: proxy = VoiceVideoCallingProxy.getInstance(); return [4 /*yield*/, proxy.load(params.logger)]; case 1: _a.sent(); return [2 /*return*/, Promise.resolve(proxy)]; } }); }); }; exports.default = createVoiceVideoCalling; //# sourceMappingURL=createVoiceVideoCalling.js.map