polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
224 lines • 7.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewerServiceSdk = void 0;
const polyv_live_api_sdk_1 = require("polyv-live-api-sdk");
const errors_1 = require("../utils/errors");
const BATCH_CONCURRENCY = 5;
const BATCH_DELAY_MS = 100;
class ViewerServiceSdk {
constructor(authConfig, _serviceConfig) {
this.client = new polyv_live_api_sdk_1.PolyVClient(authConfig);
this.v4User = this.client.v4User;
}
async getViewerRecord(params) {
try {
const result = await this.v4User.getViewerRecord(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'getViewerRecord');
}
}
async listViewerRecords(params) {
try {
const result = await this.v4User.listViewerRecords(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'listViewerRecords');
}
}
async createViewerRecord(params) {
try {
const result = await this.v4User.createViewerRecord(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'createViewerRecord');
}
}
async updateViewerRecord(params) {
try {
await this.v4User.updateViewerRecord(params);
}
catch (error) {
throw this.wrapError(error, 'updateViewerRecord');
}
}
async deleteViewerRecord(params) {
try {
await this.v4User.deleteViewerRecord(params);
}
catch (error) {
throw this.wrapError(error, 'deleteViewerRecord');
}
}
async importExternalViewer(params) {
try {
const result = await this.v4User.importExternalViewer(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'importExternalViewer');
}
}
async updateViewerUserSystemConfig(params) {
try {
await this.v4User.updateViewerUserSystemConfig(params);
}
catch (error) {
throw this.wrapError(error, 'updateViewerUserSystemConfig');
}
}
async listViewerLabels() {
try {
const result = await this.v4User.listViewerLabels();
return result;
}
catch (error) {
throw this.wrapError(error, 'listViewerLabels');
}
}
async createViewerLabel(params) {
try {
const result = await this.v4User.createViewerLabel(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'createViewerLabel');
}
}
async updateViewerLabel(params) {
try {
await this.v4User.updateViewerLabel(params);
}
catch (error) {
throw this.wrapError(error, 'updateViewerLabel');
}
}
async deleteViewerLabel(params) {
try {
await this.v4User.deleteViewerLabel(params);
}
catch (error) {
throw this.wrapError(error, 'deleteViewerLabel');
}
}
async listLabels(params) {
try {
const result = await this.v4User.listLabels(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'listLabels');
}
}
async createLabel(params) {
try {
const result = await this.v4User.createLabel(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'createLabel');
}
}
async updateLabel(params) {
try {
await this.v4User.updateLabel(params);
}
catch (error) {
throw this.wrapError(error, 'updateLabel');
}
}
async deleteLabel(params) {
try {
await this.v4User.deleteLabel(params);
}
catch (error) {
throw this.wrapError(error, 'deleteLabel');
}
}
async addChannelLabelRefs(params) {
try {
await this.v4User.addChannelLabelRefs(params);
}
catch (error) {
throw this.wrapError(error, 'addChannelLabelRefs');
}
}
async viewerLotteryWin(params) {
try {
const result = await this.v4User.viewerLotteryWin(params);
return result;
}
catch (error) {
throw this.wrapError(error, 'viewerLotteryWin');
}
}
async addViewersLabels(viewerUnionIds, labelIds, onProgress) {
return this.executeBatchOperation(viewerUnionIds, labelIds, (viewerUnionId, labelId) => this.v4User.addViewerLabel({ viewerUnionId, labelId }), 'addViewersLabels', onProgress);
}
async removeViewersLabels(viewerUnionIds, labelIds, onProgress) {
return this.executeBatchOperation(viewerUnionIds, labelIds, (viewerUnionId, labelId) => this.v4User.deleteViewerLabelRef({ viewerUnionId, labelId }), 'removeViewersLabels', onProgress);
}
async executeBatchOperation(viewerUnionIds, labelIds, operation, operationName, onProgress) {
const results = [];
const combinations = [];
for (const viewerUnionId of viewerUnionIds) {
for (const labelId of labelIds) {
combinations.push({ viewerUnionId, labelId });
}
}
const total = combinations.length;
let completed = 0;
for (let i = 0; i < combinations.length; i += BATCH_CONCURRENCY) {
const chunk = combinations.slice(i, i + BATCH_CONCURRENCY);
const chunkResults = await Promise.allSettled(chunk.map(async ({ viewerUnionId, labelId }) => {
await operation(viewerUnionId, labelId);
return { viewerUnionId, labelId, success: true };
}));
chunkResults.forEach((settledResult, j) => {
const { viewerUnionId, labelId } = chunk[j];
if (settledResult.status === 'fulfilled') {
results.push(settledResult.value);
}
else {
const reason = settledResult.reason;
results.push({
viewerUnionId,
labelId,
success: false,
error: reason instanceof Error ? reason.message : String(reason),
});
}
completed++;
onProgress?.(completed, total);
});
if (i + BATCH_CONCURRENCY < combinations.length) {
await this.delay(BATCH_DELAY_MS);
}
}
const summary = {
total,
succeeded: results.filter(r => r.success).length,
failed: results.filter(r => !r.success).length,
results,
};
if (summary.failed > 0 && summary.succeeded === 0) {
throw this.wrapError(new Error(`All ${summary.failed} operations failed`), operationName);
}
return summary;
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
wrapError(error, operation) {
if (error instanceof errors_1.PolyVError) {
return error;
}
const message = error instanceof Error ? error.message : String(error);
return new errors_1.PolyVError(`${operation} failed: ${message}`, 'VIEWER_API_ERROR', 1, { originalError: error });
}
}
exports.ViewerServiceSdk = ViewerServiceSdk;
//# sourceMappingURL=viewer-service.js.map