@skyway-sdk/sfu-bot
Version:
The official Next Generation JavaScript SDK for SkyWay
123 lines • 6.55 kB
JavaScript
;
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.SfuBotPlugin = void 0;
const common_1 = require("@skyway-sdk/common");
const core_1 = require("@skyway-sdk/core");
const sfu_api_client_1 = require("@skyway-sdk/sfu-api-client");
const transportRepository_1 = require("./connection/transport/transportRepository");
const errors_1 = require("./errors");
const member_1 = require("./member");
const option_1 = require("./option");
const version_1 = require("./version");
const log = new common_1.Logger('packages/sfu-bot/src/plugin.ts');
class SfuBotPlugin extends core_1.SkyWayPlugin {
constructor(_options = {}) {
super();
this.subtype = SfuBotPlugin.subtype;
/**@private */
this._createRemoteMember = (channel, sfuBot) => {
const member = new member_1.SfuBotMember(Object.assign(Object.assign({}, this._context), { channel, id: sfuBot.id, name: sfuBot.name, metadata: sfuBot.metadata, plugin: this, api: this._api, context: this._context, transportRepository: this._transportRepository, options: this.options }));
return member;
};
/**
* @description [japanese] SFU BotをChannelに呼び出す
*/
this.createBot = (channel) => __awaiter(this, void 0, void 0, function* () {
var _a;
const timestamp = log.info('[start] createBot', yield (0, core_1.createLogPayload)({
operationName: 'SfuBotPlugin.createBot',
channel: channel,
}));
const appId = this._context.authToken.getAppId();
const botId = yield this._api.createBot({
appId,
channelId: channel.id,
});
const member = (_a = channel._getMember(botId)) !== null && _a !== void 0 ? _a : (yield channel.onMemberJoined
.watch((e) => e.member.id === botId, this._context.config.rtcApi.timeout)
.catch((error) => {
throw (0, core_1.createError)({
operationName: 'SfuBotPlugin.createBot',
info: Object.assign(Object.assign({}, errors_1.errors.timeout), { detail: 'onMemberJoined' }),
path: log.prefix,
error,
context: this._context,
});
})).member;
log.elapsed(timestamp, '[end] createBot', yield (0, core_1.createLogPayload)({
operationName: 'SfuBotPlugin.createBot',
channel,
}));
return member;
});
/**
* @description [japanese] SFU BotをChannelから削除する。
* @remarks SkyWayAuthToken v3 を利用した場合はこのメソッドを使うことができません。代替手段として Channel.leave メソッドまたは Member.leave メソッドを使用して SFU Bot を Channel から退出させてください。
*/
this.deleteBot = (channel, botId) => __awaiter(this, void 0, void 0, function* () {
return new Promise((r, f) => __awaiter(this, void 0, void 0, function* () {
const timestamp = log.info('[start] deleteBot', yield (0, core_1.createLogPayload)({
operationName: 'SfuBotPlugin.deleteBot',
channel,
}));
let failed = false;
this._api.deleteBot({ botId }).catch((e) => {
failed = true;
f(e);
});
channel.onMemberLeft
.watch((e) => e.member.id === botId, this._context.config.rtcApi.timeout)
.then(() => __awaiter(this, void 0, void 0, function* () {
log.elapsed(timestamp, '[end] deleteBot', yield (0, core_1.createLogPayload)({
operationName: 'SfuBotPlugin.deleteBot',
channel,
}));
r();
}))
.catch((error) => {
if (!failed)
f((0, core_1.createError)({
operationName: 'SfuBotPlugin.deleteBot',
info: Object.assign(Object.assign({}, errors_1.errors.timeout), { detail: 'onMemberLeft' }),
path: log.prefix,
channel,
error,
context: this._context,
}));
});
}));
});
this.options = Object.assign(Object.assign({}, option_1.defaultSfuBotPluginOptions), _options);
this._onContextAttached.once((context) => {
common_1.Logger.level = context.config.log.level;
common_1.Logger.format = context.config.log.format;
log.info('SfuBotPlugin spawned', {
operationName: 'SfuBotPlugin.constructor',
endpoint: { sfu: this.options.domain },
options: this.options,
sdkName: 'sfu-bot',
sdkVersion: version_1.PACKAGE_VERSION,
});
this._api = new sfu_api_client_1.SfuRestApiClient(context.authTokenString, Object.assign(Object.assign({}, this.options), { log: context.config.log }));
this._transportRepository = new transportRepository_1.TransportRepository(context, this._api);
context._onTokenUpdated.add((token) => {
this._api.updateToken(token);
});
});
this._whenDisposeLocalPerson = (person) => __awaiter(this, void 0, void 0, function* () {
this._transportRepository.deleteTransports(person.id);
});
}
}
exports.SfuBotPlugin = SfuBotPlugin;
SfuBotPlugin.subtype = member_1.SfuBotMember.subtype;
//# sourceMappingURL=plugin.js.map