kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
70 lines (69 loc) • 2.29 kB
JavaScript
import { None } from "../../../../utils/other";
import { KittenCloudDataUpdateSource } from "../kitten-cloud-data-update-source";
import { KittenCloudListUpdateCommand } from "./kitten-cloud-list-update-command";
export class KittenCloudListReplaceCommand extends KittenCloudListUpdateCommand {
constructor(source, list, index, value) {
super(source, list);
this.list = list;
this.index = index;
this.value = value;
this.effective = None;
this.backup = 0;
}
execute() {
if (this.effective != None) {
throw new Error("无法执行命令:命令已被执行,不能重复执行");
}
this.effective = 0 <= this.index && this.index < this.list.length;
if (this.effective) {
this.backup = this.list.value[this.index];
this.list.value[this.index] = this.value;
this.effective = true;
this.list.replaced.emit({
source: this.source,
index: this.index,
originalItem: this.backup,
newItem: this.value
});
}
}
revoke() {
if (this.effective == None) {
throw new Error("无法撤销命令:命令尚未执行");
}
if (this.effective) {
this.list.value[this.index] = this.backup;
this.list.replaced.emit({
source: KittenCloudDataUpdateSource.REVOKE,
index: this.index,
originalItem: this.value,
newItem: this.backup
});
}
this.effective = None;
}
isEffective() {
return 0 <= this.index && this.index < this.list.length;
}
isLegal() {
return 0 <= this.index && this.index < this.data.listLengthLimit.value;
}
toJSON() {
return {
method: "replace",
index: this.index,
value: this.value
};
}
toCloudJSON() {
return {
action: "replace",
cvid: this.list.cvid,
nth: this.index + 1,
value: this.value
};
}
toString() {
return `替换云列表 ${this.data.name} 第 ${this.index + 1} 项为 ${JSON.stringify(this.value)} `;
}
}