UNPKG

kitten-cloud-function

Version:

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

63 lines (62 loc) 1.97 kB
import { None } from "../../../../utils/other"; import { KittenCloudDataUpdateSource } from "../kitten-cloud-data-update-source"; import { KittenCloudListUpdateCommand } from "./kitten-cloud-list-update-command"; export class KittenCloudListReplaceLastCommand extends KittenCloudListUpdateCommand { constructor(source, list, value) { super(source, list); this.list = list; this.value = value; this.effective = None; this.backup = 0; } execute() { if (this.effective != None) { throw new Error("无法执行命令:命令已被执行,不能重复执行"); } this.effective = this.list.length > 0; if (this.effective) { this.backup = this.list.value.splice(-1, 1, this.value)[0]; this.list.replacedLast.emit({ source: this.source, originalItem: this.backup, newItem: this.value }); } } revoke() { if (this.effective == None) { throw new Error("无法撤销命令:命令尚未执行"); } if (this.effective) { this.list.value.splice(-1, 1, this.backup); this.list.replacedLast.emit({ source: KittenCloudDataUpdateSource.REVOKE, originalItem: this.value, newItem: this.backup }); } this.effective = None; } isEffective() { return this.effective == None ? this.list.length > 0 : this.effective; } isLegal() { return true; } toJSON() { return { method: "replaceLast", value: this.value }; } toCloudJSON() { return { action: "replace", nth: "last", value: this.value }; } toString() { return `替换云列表 ${this.data.name} 最后一项为 ${JSON.stringify(this.value)} `; } }