UNPKG

kitten-cloud-function

Version:

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

68 lines (67 loc) 2.05 kB
import { None } from "../../../../utils/other"; import { KittenCloudDataUpdateSource } from "../kitten-cloud-data-update-source"; import { KittenCloudListUpdateCommand } from "./kitten-cloud-list-update-command"; export class KittenCloudListUnshiftCommand extends KittenCloudListUpdateCommand { constructor(source, list, value) { super(source, list); this.list = list; this.value = value; this.overflow = None; this.overflowValue = 0; } execute() { if (this.overflow != None) { throw new Error("无法执行命令:命令已被执行,不能重复执行"); } this.list.value.unshift(this.value); this.overflow = this.list.length > this.list.listLengthLimit.value; if (this.overflow) { this.overflowValue = this.list.value.pop(); } this.list.unshifted.emit({ source: this.source, item: this.value }); } revoke() { if (this.overflow == None) { throw new Error("无法撤销命令:命令尚未执行"); } this.list.value.shift(); this.overflow = None; this.list.removed.emit({ source: KittenCloudDataUpdateSource.REVOKE, index: 0, item: this.overflowValue }); if (this.overflow) { this.list.value.push(this.overflowValue); this.list.pushed.emit({ source: KittenCloudDataUpdateSource.REVOKE, item: this.overflowValue }); } } isEffective() { return true; } isLegal() { return true; } toJSON() { return { method: "unshift", value: this.value }; } toCloudJSON() { return { action: "unshift", cvid: this.list.cvid, value: this.value }; } toString() { return `添加 ${JSON.stringify(this.value)} 到云列表 ${this.data.name} 首项`; } }