|
|
@@ -1,59 +1,92 @@
|
|
|
import {
|
|
|
- Deserialize,
|
|
|
- Describe,
|
|
|
TransformUtils,
|
|
|
Operation,
|
|
|
+ Describe,
|
|
|
+ Deserialize,
|
|
|
+ Consumer,
|
|
|
} from "../internal";
|
|
|
|
|
|
export class Command {
|
|
|
+ public static setOperationsCommand(operations: Array<Operation>, command: Command): Array<Operation> {
|
|
|
+ let length = operations.length;
|
|
|
+ for (let i = 0; i < length; i++) {
|
|
|
+ Command.setOperationCommand(operations[i], command);
|
|
|
+ }
|
|
|
+ return operations;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static setOperationCommand(operation: Operation, command: Command): Operation {
|
|
|
+ operation.setCommand(command);
|
|
|
+ return operation;
|
|
|
+ }
|
|
|
+
|
|
|
protected _describe: string = 'Command';
|
|
|
protected _groupId: string;
|
|
|
- protected _operation: Array<Operation>;
|
|
|
+ protected _operations: Array<Operation>;
|
|
|
|
|
|
public constructor()
|
|
|
- public constructor(serialize: Command.Serialize)
|
|
|
public constructor(groupId: string)
|
|
|
+ public constructor(serialize: Command.Serialize)
|
|
|
+ public constructor(command: Command)
|
|
|
public constructor(groupId: string, operation: Array<Operation>)
|
|
|
public constructor(...parameter: any) {
|
|
|
if (TransformUtils.hasLength(parameter, 0)) {
|
|
|
this._groupId = 'default';
|
|
|
- this._operation = [];
|
|
|
+ this._operations = [];
|
|
|
return;
|
|
|
}
|
|
|
if (TransformUtils.hasLength(parameter, 1)) {
|
|
|
if (TransformUtils.isString(parameter[0])) {
|
|
|
const groupId = parameter[0];
|
|
|
- this._operation = [];
|
|
|
+ this._operations = [];
|
|
|
this._groupId = groupId;
|
|
|
return;
|
|
|
}
|
|
|
+ if (TransformUtils.isAssignableFrom(parameter[0], Command)) {
|
|
|
+ const command = parameter[0];
|
|
|
+ const clone = command.clone();
|
|
|
+ this._describe = clone._describe;
|
|
|
+ this._groupId = clone._groupId;
|
|
|
+ this._operations = [];
|
|
|
+ this.addOperations(clone._operations);
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (TransformUtils.isObject(parameter[0])) {
|
|
|
- const serialize = <Command.Serialize>parameter[0];
|
|
|
+ const serialize = <Command.Serialize> parameter[0];
|
|
|
+ const operations = Deserialize.getInstance().formArray(serialize.operations);
|
|
|
this._groupId = serialize.groupId;
|
|
|
- this._operation = Deserialize.getInstance().formArray(serialize.operation);
|
|
|
+ this._operations = [];
|
|
|
+ this.addOperations(operations);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
if (TransformUtils.hasLength(parameter, 2)) {
|
|
|
const groupId = parameter[0];
|
|
|
- const operation = parameter[1];
|
|
|
+ const operations = parameter[1];
|
|
|
this._groupId = groupId;
|
|
|
- this._operation = operation;
|
|
|
+ this._operations = [];
|
|
|
+ this.addOperations(operations);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public concat(target: Command): Command {
|
|
|
- const result = new Command({
|
|
|
- groupId: this._groupId,
|
|
|
- describe: this._describe,
|
|
|
- operation: [],
|
|
|
- });
|
|
|
- result._operation = this._operation.concat(target._operation);
|
|
|
+ const result = new Command();
|
|
|
+ result.setGroupId(this.getGroupId());
|
|
|
+ result.addOperations(this.getOperations());
|
|
|
+ result.addOperations(target.getOperations());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ public forEach(consumer: Consumer<Operation>): void {
|
|
|
+ let operations = this._operations;
|
|
|
+ let loopLimit = operations.length;
|
|
|
+ for (let i = 0; i < loopLimit; i++) {
|
|
|
+ consumer(operations[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public apply(resource: unknown): void {
|
|
|
- const operations = this._operation;
|
|
|
+ const operations = this._operations;
|
|
|
const sizeof = operations.length;
|
|
|
for (let i = 0; i < sizeof; i++) {
|
|
|
const action = operations[i];
|
|
|
@@ -61,30 +94,96 @@ export class Command {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public addOperations(operations: Operation[]): void {
|
|
|
+ Command.setOperationsCommand(operations, this);
|
|
|
+ this._operations = this._operations.concat(operations);
|
|
|
+ }
|
|
|
+
|
|
|
+ public addOperation(operation: Operation): void {
|
|
|
+ Command.setOperationCommand(operation, this);
|
|
|
+ this._operations.push(operation);
|
|
|
+ }
|
|
|
+
|
|
|
+ public clearOperations(): void {
|
|
|
+ Command.setOperationsCommand(this._operations, null);
|
|
|
+ this._operations = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ public removeOperation(index: number): void {
|
|
|
+ Command.setOperationsCommand(this._operations.splice(index, 1), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public replaceOperations(index: number, operations: Operation[])
|
|
|
+ public replaceOperations(operation: Operation, operations: Operation[])
|
|
|
+ public replaceOperations(...parameters: any) {
|
|
|
+ if (TransformUtils.hasLength(parameters, 2)) {
|
|
|
+ if (TransformUtils.isNumber(parameters[0])) {
|
|
|
+ const index = parameters[0];
|
|
|
+ const operations = parameters[1];
|
|
|
+ Command.setOperationsCommand(operations, this);
|
|
|
+ this.removeOperation(index);
|
|
|
+ this._operations.splice(index, 0, ...operations);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (TransformUtils.isAssignableFrom(parameters[0], Operation)) {
|
|
|
+ const operation = parameters[0];
|
|
|
+ const operations = parameters[1];
|
|
|
+ const index = this._operations.indexOf(operation);
|
|
|
+ if (index > -1) {
|
|
|
+ Command.setOperationsCommand(operations, this);
|
|
|
+ this.removeOperation(index);
|
|
|
+ this._operations.splice(index, 0, ...operations);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public setGroupId(groupId: string): void {
|
|
|
+ this._groupId = groupId;
|
|
|
+ }
|
|
|
+
|
|
|
public getOperations(): Array<Operation> {
|
|
|
- return this._operation;
|
|
|
+ return this._operations;
|
|
|
}
|
|
|
|
|
|
public getGroupId(): string {
|
|
|
return this._groupId;
|
|
|
}
|
|
|
|
|
|
- public addOperation(operation: Operation): void {
|
|
|
- this._operation.push(operation);
|
|
|
+ public getOperationSize(): number {
|
|
|
+ return this._operations.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ public getOperation(index: number): Operation {
|
|
|
+ return this._operations[index];
|
|
|
+ }
|
|
|
+
|
|
|
+ public clone(): Command {
|
|
|
+ const clone = new Command();
|
|
|
+ const describe = this._describe;
|
|
|
+ const groupId = this._groupId;
|
|
|
+ const operations = this._operations.map((o) => o.clone());
|
|
|
+ clone._groupId = groupId;
|
|
|
+ clone._describe = describe;
|
|
|
+ clone._operations = Command.setOperationsCommand(operations, clone);
|
|
|
+ return clone;
|
|
|
}
|
|
|
|
|
|
public toJSON(): Command.Serialize {
|
|
|
+ const describe = this._describe;
|
|
|
+ const groupId = this._groupId;
|
|
|
+ const operations = this._operations.map((o) => o.toJSON());
|
|
|
return {
|
|
|
- describe: this._describe,
|
|
|
- groupId: this._groupId,
|
|
|
- operation: this._operation.map((o) => o.toJSON()),
|
|
|
+ describe,
|
|
|
+ groupId,
|
|
|
+ operations,
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export namespace Command {
|
|
|
export interface Serialize extends Describe {
|
|
|
- operation: Array<Operation.Serialize>,
|
|
|
- groupId: string
|
|
|
+ groupId: string,
|
|
|
+ operations: Array<Operation.Serialize>,
|
|
|
}
|
|
|
}
|