@skyway-sdk/core
Version:
The official Next Generation JavaScript SDK for SkyWay
91 lines • 3.31 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.MemberImpl = void 0;
const common_1 = require("@skyway-sdk/common");
const errors_1 = require("../errors");
const util_1 = require("../util");
const log = new common_1.Logger('packages/core/src/member/index.ts');
/**@internal */
class MemberImpl {
constructor(args) {
this._state = 'joined';
/**@internal */
this._events = new common_1.Events();
this.onLeft = this._events.make();
this.onMetadataUpdated = this._events.make();
this.channel = args.channel;
this.id = args.id;
this.name = args.name;
this._metadata = args.metadata;
this.context = args.context;
}
get metadata() {
return this._metadata;
}
get state() {
return this._state;
}
get publications() {
return this.channel.publications.filter((p) => p.publisher.id === this.id);
}
get subscriptions() {
return this.channel.subscriptions.filter((p) => p.subscriber.id === this.id);
}
/**@internal */
toJSON() {
return {
id: this.id,
name: this.name,
type: this.type,
subtype: this.subtype,
metadata: this.metadata,
};
}
/** @private*/
_left() {
this._state = 'left';
this.onLeft.emit();
this._events.dispose();
}
/** @private*/
_metadataUpdated(metadata) {
this._metadata = metadata;
this.onMetadataUpdated.emit(metadata);
}
updateMetadata(metadata) {
return __awaiter(this, void 0, void 0, function* () {
yield this.channel._updateMemberMetadata(this.id, metadata);
});
}
/**@throws {@link SkyWayError} */
leave() {
return __awaiter(this, void 0, void 0, function* () {
const timestamp = log.info('[start] leave', yield (0, util_1.createLogPayload)({
operationName: 'localPerson.leave',
channel: this.channel,
}));
if (this.state === 'left') {
throw (0, util_1.createError)({
operationName: 'localPerson.leave',
info: errors_1.errors.localPersonNotJoinedChannel,
path: log.prefix,
context: this.context,
channel: this.channel,
});
}
yield this.channel.leave(this);
log.elapsed(timestamp, '[end] leave');
});
}
}
exports.MemberImpl = MemberImpl;
//# sourceMappingURL=index.js.map