UNPKG

roadofcloud-playback-sdk

Version:

CloudHubPlaybackSdk

101 lines (100 loc) 3.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const global_1 = __importDefault(require("../global")); const utils_1 = __importDefault(require("../helper/utils")); const Logger_1 = __importDefault(require("../Logger")); class RecordController { /** * 通过 url root path 进行初始化,Record 会分析并合理化 record.json 文件 * @param filename */ constructor(filename = 'record.json') { this.filename = filename; this.actions = []; } get fileUrl() { return global_1.default.config.recordRootUrl + '/' + this.filename; } init() { return new Promise((resolve, reject) => { fetch(this.fileUrl).then(response => { if (!response.ok || response.status !== 200) { reject('record.json not found'); } // 解析 json 文件 response.json().then(data => { // 进行时间排序 data.sort((prev, next) => { return prev['ts'] - next['ts']; }); // 整理好的数据进行赋值 this.actions = this.setStartToFirst(data); // 返回结果 resolve(true); }).catch(error => { reject('record.json not json'); }); }).catch(error => { reject(error); }); }); } //部分 record中 start 的时间戳并不是最早的 需要兼容 setStartToFirst(data) { if (data[0].method === 'start') return data; const startIndex = data.findIndex((item) => item.method === 'start'); const item = utils_1.default.deepCopy(data[startIndex]); // 修正时间,让 start 在第一 item['ts'] = data[0].ts - 10; data.splice(startIndex, 1); data.unshift(item); return data; } //获取开始的时间戳 getStartTime() { if (this.actions.length <= 0) { Logger_1.default.error('please run init first'); } const startAction = this.actions.find((item) => item.method === 'start'); if (startAction) { return this.getTs(startAction); } } //获取结束的时间戳 getEndTime() { if (this.actions.length <= 0) { Logger_1.default.error('please run init first'); } const endAction = this.actions.find((item) => item.method === 'close'); if (endAction) { return this.getTs(endAction); } } //获取某次轮询的所有 action getActions(startIndex, timestamp) { let actions = [], index = startIndex, startTime = this.getTs(this.actions[index]); for (let i = startIndex; i < this.actions.length; i++) { const ts = this.getTs(this.actions[i]); if (startTime < ts && ts < timestamp) { actions.push(this.actions[i]); index = i; } else if (startTime !== ts) { break; } } return { index, actions }; } //获取 action 的时间戳 getTs(action) { return action.ts; } } exports.default = RecordController;