UNPKG

zby-live-sdk

Version:

This is a live SDK for weclassroom.

99 lines (89 loc) 2.54 kB
import defaultApi from '../default'; export class DataStore extends Map { /** * * @param {number} dataValidityPeriod 数据有效期,毫秒,默认1000ms */ constructor(dataValidityPeriod = 1000) { super(); this.dataValidityPeriod = dataValidityPeriod; defaultApi.writeLog(`DataStore dataValidityPeriod: ${dataValidityPeriod}`); } setData(key, data) { let obj = this.get(key); if (!obj) { obj = {}; this.set(key, obj); } obj.timestamp = Date.now(); obj.data = data; } getData(key) { const obj = this.get(key); if (!obj) { defaultApi.writeLog(`DataStore getData key: ${key}, data is null`); return null; } // obj设置时的时间戳与当前时间戳的差值 const timeCost = Date.now() - obj.timestamp; // 没有data或者data已经超过有效期,返回空 if (timeCost > this.dataValidityPeriod) { defaultApi.writeLog(`DataStore getData key: ${key}, cost: ${timeCost}, dataValidityPeriod: ${this.dataValidityPeriod}, data: ${obj.data}`); this.delete(key); return null; } else { return obj.data; } } /** * 检查数据是否过期 * @param {*} data * @returns */ checkData(data) { if (!data || !data.timestamp) { throw new Error('DataStore checkData data is null or data.timestamp is null'); } const timeCost = Date.now() - data.timestamp; if (timeCost > this.dataValidityPeriod) { return false; } else { return true; } } restore() { this.clear(); } /** * 更新数据有效期 * @param {number} value */ updateValidityPeriod(value) { defaultApi.writeLog(`DataStore updateValidityPeriod value: ${value}`); if (typeof value !== 'number') { return; } this.dataValidityPeriod = value; } } export const userAndDeviceStatusInfo = { microPhone: undefined, camera: undefined, isPushingFlow: undefined, }; // https://yach-doc-shimo.zhiyinlou.com/docs/loqeW4ME41ie1Anz/ 《学习报告在线时长优化》 let background = 0; // 0:否(默认值),1:转到后台 /** * 设置前后台状态 * @param {boolean} isBackground */ export const setBackgroundStatus = (isBackground) => { background = isBackground ? 1 : 0; }; /** * 获取前后台状态 * @returns {number} 0:否(默认值),1:转到后台 */ export const getBackgroundStatus = () => { return background; };