kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
64 lines (63 loc) • 2.07 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 KittenCloudListRemoveCommand extends KittenCloudListUpdateCommand {
constructor(source, list, index) {
super(source, list);
this.list = list;
this.index = index;
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.splice(this.index, 1)[0];
this.effective = true;
this.list.removed.emit({
source: this.source,
index: this.index,
item: this.backup
});
}
}
revoke() {
if (this.effective == None) {
throw new Error("无法撤销命令:命令尚未执行");
}
if (this.effective) {
this.list.value.splice(this.index, 0, this.backup);
this.list.added.emit({
source: KittenCloudDataUpdateSource.REVOKE,
index: this.index,
item: this.backup
});
}
this.effective = None;
}
isEffective() {
return this.effective == None ? 0 <= this.index && this.index < this.list.length : this.effective;
}
isLegal() {
return 0 <= this.index && this.index < this.data.listLengthLimit.value;
}
toJSON() {
return {
method: "remove",
index: this.index
};
}
toCloudJSON() {
return {
action: "delete",
cvid: this.list.cvid,
nth: this.index + 1
};
}
toString() {
return `移除云列表 ${this.data.name} 第 ${this.index + 1} 项`;
}
}