@skyway-sdk/core
Version:
The official Next Generation JavaScript SDK for SkyWay
146 lines • 6.46 kB
JavaScript
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemotePersonImpl = void 0;
const common_1 = require("@skyway-sdk/common");
const errors_1 = require("../../../errors");
const member_1 = require("../../../member");
const util_1 = require("../../../util");
const connection_1 = require("./connection");
const log = new common_1.Logger('packages/core/src/plugin/internal/person/member.ts');
/**@internal */
class RemotePersonImpl extends member_1.MemberImpl {
constructor(args) {
super(args);
this.args = args;
this.type = 'person';
this.subtype = 'person';
this.side = 'remote';
this._connections = {};
this._context = this.args.channel._context;
this._disposer = new common_1.EventDisposer();
this.onPublicationSubscribed = this._events.make();
this.onPublicationUnsubscribed = this._events.make();
this.onPublicationListChanged = this._events.make();
this.onSubscriptionListChanged = this._events.make();
this.subscribe = (publicationId) => new Promise((r, f) => {
let failed = false;
this.channel._subscribe(this.id, publicationId).catch((e) => {
failed = true;
f(e);
});
this.onPublicationSubscribed
.watch(({ subscription }) => subscription.publication.id === publicationId, this._context.config.rtcApi.timeout)
.then(({ subscription }) => {
r({ subscription });
})
.catch(() => {
if (!failed) {
f((0, util_1.createError)({
operationName: 'RemotePersonImpl.subscribe',
info: Object.assign(Object.assign({}, errors_1.errors.timeout), { detail: 'onPublicationSubscribed' }),
path: log.prefix,
context: this._context,
channel: this.channel,
}));
}
});
});
this.plugin = args.plugin;
this.channel.onPublicationUnsubscribed
.add(({ subscription }) => {
if (subscription.subscriber.id === this.id) {
this.onPublicationUnsubscribed.emit({ subscription });
this.onSubscriptionListChanged.emit();
}
})
.disposer(this._disposer);
this.channel.onPublicationSubscribed
.add(({ subscription }) => {
if (subscription.subscriber.id === this.id) {
this.onPublicationSubscribed.emit({ subscription });
this.onSubscriptionListChanged.emit();
}
})
.disposer(this._disposer);
this.channel.onStreamPublished
.add(({ publication }) => {
if (publication.publisher.id === this.id) {
this.onPublicationListChanged.emit();
}
})
.disposer(this._disposer);
this.channel.onStreamUnpublished
.add(({ publication }) => {
if (publication.publisher.id === this.id) {
this.onPublicationListChanged.emit();
}
})
.disposer(this._disposer);
this.onLeft.once(() => {
log.debug('RemotePerson left: ', this.toJSON());
Object.values(this._connections).forEach((connection) => {
connection.close({ reason: 'remote person left' });
});
this._connections = {};
});
}
/**@private */
_getConnection(localPersonId) {
return this._connections[localPersonId];
}
/**@private */
_getOrCreateConnection(localPerson) {
var _a;
const connection = (_a = this._getConnection(localPerson.id)) !== null && _a !== void 0 ? _a : this._createConnection(this.channel, localPerson, this);
return connection;
}
_createConnection(channel, localPerson, endpointMember) {
if (localPerson.side !== 'local') {
throw (0, util_1.createError)({
operationName: 'RemotePersonImpl._createConnection',
info: Object.assign(Object.assign({}, errors_1.errors.invalidArgumentValue), { detail: 'wrong localPerson type' }),
path: log.prefix,
context: this._context,
channel: this.channel,
});
}
if (!localPerson._signaling) {
throw (0, util_1.createError)({
operationName: 'RemotePersonImpl._createConnection',
info: Object.assign(Object.assign({}, errors_1.errors.missingProperty), { detail: 'signalingSession not exist' }),
path: log.prefix,
context: this._context,
channel: this.channel,
});
}
const connection = new connection_1.P2PConnection(localPerson.iceManager, localPerson._signaling, localPerson._analytics, this._context, channel.id, localPerson, endpointMember);
this.plugin._messageBuffers[localPerson.id].resolveMessagingBuffer(endpointMember);
connection.onClose.once(() => {
log.debug('connection closed', this.toJSON(), {
connectionId: connection.id,
});
delete this._connections[localPerson.id];
});
this._connections[localPerson.id] = connection;
return connection;
}
unsubscribe(subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.channel._unsubscribe(subscriptionId);
});
}
_dispose() {
this._disposer.dispose();
}
}
exports.RemotePersonImpl = RemotePersonImpl;
//# sourceMappingURL=member.js.map