UNPKG

kitten-cloud-function

Version:

用于编程猫源码云功能(云变量、云列表等)的客户端工具

159 lines (158 loc) 9.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KittenCloudListGroup = void 0; const other_1 = require("../../../utils/other"); const kitten_cloud_send_message_type_1 = require("../../network/kitten-cloud-send-message-type"); const kitten_cloud_list_1 = require("../kitten-cloud-list"); const kitten_cloud_data_update_command_group_1 = require("../update/command/kitten-cloud-data-update-command-group"); const kitten_cloud_list_add_command_1 = require("../update/command/kitten-cloud-list-add-command"); const kitten_cloud_list_empty_command_1 = require("../update/command/kitten-cloud-list-empty-command"); const kitten_cloud_list_pop_command_1 = require("../update/command/kitten-cloud-list-pop-command"); const kitten_cloud_list_push_command_1 = require("../update/command/kitten-cloud-list-push-command"); const kitten_cloud_list_remove_command_1 = require("../update/command/kitten-cloud-list-remove-command"); const kitten_cloud_list_replace_command_1 = require("../update/command/kitten-cloud-list-replace-command"); const kitten_cloud_list_replace_last_command_1 = require("../update/command/kitten-cloud-list-replace-last-command"); const kitten_cloud_list_unshift_command_1 = require("../update/command/kitten-cloud-list-unshift-command"); const kitten_cloud_data_update_source_1 = require("../update/kitten-cloud-data-update-source"); const kitten_cloud_data_group_1 = require("./kitten-cloud-data-group"); /** * 云列表组。 */ class KittenCloudListGroup extends kitten_cloud_data_group_1.KittenCloudDataGroup { constructor(connection) { super(connection, { localPreupdate: false }); this.dataTypeName = "云列表"; this.dataUpdateSendMessageType = kitten_cloud_send_message_type_1.KittenCloudSendMessageType.UPDATE_LIST; } createData(cvid, name, value) { return new kitten_cloud_list_1.KittenCloudList(this.connection, this, cvid, name, value); } toCloudUploadMessage(message) { var _a, _b; const newMessage = {}; for (const cvid in message) { if ((_a = message[cvid]) === null || _a === void 0 ? void 0 : _a.isEmpty()) { continue; } newMessage[cvid] = (_b = message[cvid]) === null || _b === void 0 ? void 0 : _b.toCloudJSON(); } return newMessage; } toUploadMessage(message) { if (message == other_1.None) { throw new Error("更新数据为空"); } if (!(typeof message == "object")) { throw new Error(`无法识别更新数据格式:${message}`); } const result = {}; const errorArray = []; for (const cvid in message) { const data = this.dataRecord[cvid]; if (data == other_1.None) { errorArray.push(new Error(`未找到数据:${cvid}`)); continue; } const item = message[cvid]; result[cvid] = new kitten_cloud_data_update_command_group_1.KittenCloudDataUpdateCommandGroup(); if (item == other_1.None) { errorArray.push(new Error("更新数据为空")); } else if (!Array.isArray(item)) { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(item)}`)); } else { for (const singleMessage of item) { if (singleMessage == other_1.None) { errorArray.push(new Error("更新数据为空")); } else if (!(typeof singleMessage == "object" && "action" in singleMessage && typeof singleMessage.action == "string")) { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); } else { switch (singleMessage.action) { case "append": if ("value" in singleMessage && (typeof singleMessage.value == "number" || typeof singleMessage.value == "string")) { result[cvid].add(new kitten_cloud_list_push_command_1.KittenCloudListPushCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data, singleMessage.value)); } else { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); } break; case "unshift": if ("value" in singleMessage && (typeof singleMessage.value == "number" || typeof singleMessage.value == "string")) { result[cvid].add(new kitten_cloud_list_unshift_command_1.KittenCloudListUnshiftCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data, singleMessage.value)); } else { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); } break; case "insert": if ("nth" in singleMessage && typeof singleMessage.nth == "number" && "value" in singleMessage && (typeof singleMessage.value == "number" || typeof singleMessage.value == "string")) { result[cvid].add(new kitten_cloud_list_add_command_1.KittenCloudListAddCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data, singleMessage.nth - 1, singleMessage.value)); } else { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); } break; case "delete": if (!("nth" in singleMessage)) { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); } else if (singleMessage.nth == "last") { result[cvid].add(new kitten_cloud_list_pop_command_1.KittenCloudListPopCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data)); } else if (singleMessage.nth == "all") { result[cvid].add(new kitten_cloud_list_empty_command_1.KittenCloudListEmptyCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data)); } else { if (typeof singleMessage.nth != "number") { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); } result[cvid].add(new kitten_cloud_list_remove_command_1.KittenCloudListRemoveCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data, singleMessage.nth - 1)); } break; case "replace": if (!("nth" in singleMessage)) { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); break; } if (!("value" in singleMessage && (typeof singleMessage.value == "number" || typeof singleMessage.value == "string"))) { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); break; } if (singleMessage.nth == "last") { result[cvid].add(new kitten_cloud_list_replace_last_command_1.KittenCloudListReplaceLastCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data, singleMessage.value)); } else { if (typeof singleMessage.nth != "number") { errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); break; } result[cvid].add(new kitten_cloud_list_replace_command_1.KittenCloudListReplaceCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, data, singleMessage.nth - 1, singleMessage.value)); } break; default: errorArray.push(new Error(`无法识别更新数据格式:${JSON.stringify(singleMessage)}`)); break; } } } } } return result; } } exports.KittenCloudListGroup = KittenCloudListGroup;