kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
56 lines (55 loc) • 1.83 kB
JavaScript
import { None } from "../../../../utils/other";
import { KittenCloudDataUpdateSource } from "../kitten-cloud-data-update-source";
import { KittenCloudVariableUpdateCommand } from "./kitten-cloud-variable-update-command";
export class KittenCloudVariableSetCommand extends KittenCloudVariableUpdateCommand {
constructor(source, variable, value) {
super(source, variable);
this.value = value;
this.backup = None;
}
execute() {
if (this.backup != None) {
throw new Error(`无法执行命令:命令已被执行,不能重复执行`);
}
this.backup = this.variable.value;
this.variable.value = this.value;
this.variable.changed.emit({
source: this.source,
originalValue: this.backup,
newValue: this.value
});
}
revoke() {
if (this.backup == None) {
throw new Error(`无法撤销命令:命令尚未执行`);
}
this.variable.value = this.backup;
this.variable.changed.emit({
source: KittenCloudDataUpdateSource.REVOKE,
originalValue: this.value,
newValue: this.backup
});
this.backup = None;
}
isEffective() {
return this.backup == None ? this.value !== this.variable.value : this.value !== this.backup;
}
toJSON() {
return {
method: "set",
value: this.value
};
}
isLegal() {
return true;
}
merge(that) {
if ((this.backup == None) != (that.backup == None)) {
throw new Error("命令执行状态不一致,不能合并");
}
this.value = that.value;
}
toString() {
return `设置云变量 ${this.data.name} 的值为 ${JSON.stringify(this.value)} `;
}
}