kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
132 lines (131 loc) • 4.69 kB
JavaScript
import { None } from "../../../utils/other";
import { KittenCloudFunctionConfigLayer } from "../../kitten-cloud-function-config-layer";
/**
* 云数据组,用于管理云数据实例。具有收集和分发云数据更新数据的功能。
*/
export class KittenCloudDataGroup extends KittenCloudFunctionConfigLayer {
constructor(connection, config) {
super(connection, config);
this.connection = connection;
this.connecting = true;
this.dataArray = [];
this.dataRecord = {};
this.array = this.dataArray;
this.uploadCount = [];
}
update(data) {
this.connecting = false;
for (const item of data) {
const data = this.dataRecord[item.cvid];
if (data == null) {
const newData = this.createData(item.cvid, item.name, item.value);
this.dataArray.push(newData);
this.dataRecord[item.name] = newData;
this.dataRecord[item.cvid] = newData;
newData.updateManager.neededToUpload.connect(() => {
this.handleUpload();
});
}
else {
data.update(item.value);
}
}
}
/**
* 获取该云数据组指定的云数据实例。
*
* @param index 索引,可以是云数据的名称或 cvid
* @returns 对应的云数据实例
* @throws 如果不存在该云数据实例,则抛出异常
*/
async get(index) {
if (this.connecting) {
await this.connection.waitOpen();
}
const data = this.dataRecord[index];
if (data == null) {
throw new Error(`获取${this.dataTypeName}失败:${this.dataTypeName} ${index} 不存在`);
}
return data;
}
/**
* 获取该云数据组中的所有云数据。
*
* @returns 由所有云数据组成的数组
*/
async getAll() {
if (this.connecting) {
await this.connection.waitOpen();
}
return this.dataArray;
}
handleUpload() {
const uploadCount = [];
const uploadMessage = {};
for (const data of this.dataArray) {
const singleUploadMessage = data.updateManager.upload();
uploadMessage[data.cvid] = singleUploadMessage;
uploadCount.push(singleUploadMessage.length);
}
this.uploadCount.push(uploadCount);
this.connection.send(this.dataUpdateSendMessageType, this.toCloudUploadMessage(uploadMessage));
}
handleCloudUpdate(cloudMessage) {
const errorArray = [];
let message;
try {
message = this.toUploadMessage(cloudMessage);
}
catch (error) {
if (!Array.isArray(error)) {
this.handleCloudUpdateError();
let message;
if (error instanceof Error) {
message = error.message;
}
else if (typeof error == "string") {
message = error;
}
else {
message = JSON.stringify(error);
}
throw new Error(`更新${this.dataTypeName}失败:${message}`);
}
for (const singleError of error) {
errorArray.push(singleError);
}
if (typeof error != "object" || !("message" in error)) {
errorArray.push(new Error(`更新${this.dataTypeName}失败:找不到更新数据`));
}
message = error.message;
}
for (const data of this.dataArray) {
const singleMessage = message[data.cvid];
if (singleMessage == None) {
continue;
}
let updateCommand;
while ((updateCommand = singleMessage.shift()) != None) {
data.updateManager.addUpdateCommand(updateCommand);
}
}
this.uploadCount.shift();
}
handleCloudUpdateError() {
var _a;
const firstUploadCount = this.uploadCount.shift();
if (firstUploadCount == None) {
throw new Error("不存在上传数据");
}
for (let i = 0; i < this.dataArray.length; i++) {
for (;;) {
const count = firstUploadCount[i];
if (count == None || count <= 0) {
break;
}
(_a = this.dataArray[i]) === null || _a === void 0 ? void 0 : _a.updateManager.handleUploadingError();
firstUploadCount[i] = count - 1;
}
}
}
}