podchat-browser
Version:
Javascript SDK to use POD's Chat Service - Browser Only
171 lines (133 loc) • 6.2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var MidManager = /*#__PURE__*/function () {
function MidManager(props) {
(0, _classCallCheck2["default"])(this, MidManager);
this._app = props.app;
this._mids = {};
}
(0, _createClass2["default"])(MidManager, [{
key: "pushMidToList",
value: function pushMidToList(topic, mids) {
this._mids[topic] = mids;
}
}, {
key: "removeTopic",
value: function removeTopic(topic) {
if (this._mids[topic]) {
delete this._mids[topic];
}
}
}, {
key: "getTopicsOfMids",
value: function getTopicsOfMids(midList) {
var topics = [];
for (var topic in this._mids) {
var _iterator = _createForOfIteratorHelper(midList),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var mid = _step.value;
if (this._mids[topic].includes(mid)) {
topics.push(topic);
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return topics;
}
/**
* We must close existing topic streams and open a new stream with the new mid
*/
}, {
key: "reOpenExistingDuplicateMids",
value: function () {
var _reOpenExistingDuplicateMids = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(list) {
var topics, call, _iterator2, _step2, topic, userId, userObj, i;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
/**
* If mid of a new track in the list, already exists, we should open that topic again
*/
topics = this.getTopicsOfMids(list.flatMap(function (item) {
return item.mids;
}));
call = this._app.call.currentCall();
_iterator2 = _createForOfIteratorHelper(topics);
_context.prev = 3;
_iterator2.s();
case 5:
if ((_step2 = _iterator2.n()).done) {
_context.next = 19;
break;
}
topic = _step2.value;
userId = call.users().findUserIdByTopic(topic);
userObj = call.users().get(userId);
if (!(topic.startsWith('Vi') || topic.startsWith('screen'))) {
_context.next = 14;
break;
}
_context.next = 12;
return userObj.destroyVideo();
case 12:
_context.next = 16;
break;
case 14:
_context.next = 16;
return userObj.destroyAudio();
case 16:
delete this._mids[topic];
case 17:
_context.next = 5;
break;
case 19:
_context.next = 24;
break;
case 21:
_context.prev = 21;
_context.t0 = _context["catch"](3);
_iterator2.e(_context.t0);
case 24:
_context.prev = 24;
_iterator2.f();
return _context.finish(24);
case 27:
for (i in list) {
this.pushMidToList(list[i].topic, list[i].mids);
}
call.requestReceivingMedia();
case 29:
case "end":
return _context.stop();
}
}
}, _callee, this, [[3, 21, 24, 27]]);
}));
function reOpenExistingDuplicateMids(_x) {
return _reOpenExistingDuplicateMids.apply(this, arguments);
}
return reOpenExistingDuplicateMids;
}()
}]);
return MidManager;
}();
var _default = MidManager;
exports["default"] = _default;