By imnickvaughn


2019-10-09 14:53:53 8 Comments

I am refactoring and stumbled upon the samples below. I was wondering if I could condense this down as its mostly repeated code. As you can see the only real difference are the methods, executeTask, executeTaskFrom, acknowledgeTask, and killTask of the same class being conditionally called. My question is how you would refactor this and whether my solution or something like it is optimal.

This is what I have:

 taskGridOnCellclick(): void {
  if (datafield === "executeTask" && this.showButtons) {
      const taskName = this.taskGrid.getrowdata(this.editrow).taskName;

      // Call the service
      this.taskSvc.executeTask(this.dataForUi.processName, taskName).subscribe(
        data => {
          this.logger.debug(data);
        },
        error => {
          this.logger.error(JSON.stringify(error));
        }
      );
    } else if (datafield === "executeAll" && this.showButtons) {
      const taskName = this.taskGrid.getrowdata(this.editrow).taskName;

      // Call the service
      this.taskSvc
        .executeTaskFrom(this.dataForUi.processName, taskName)
        .subscribe(
          data => {
            this.logger.debug(data);
          },
          error => {
            this.logger.error(JSON.stringify(error));
          }
        );
    } else if (datafield === "acknowledge" && this.showButtons) {
      const taskName = this.taskGrid.getrowdata(this.editrow).taskName;
      const taskStatus = this.taskGrid.getrowdata(this.editrow).taskStateName;

      if (
        taskStatus !== "Killed" &&
        taskStatus !== "Failed" &&
        this.showButtons
      ) {
      } else {
        //Call the service
        this.taskSvc
          .acknowledgeTask(this.dataForUi.processName, taskName)
          .subscribe(
            data => {
              this.logger.debug(data);
            },
            error => {
              this.logger.error(JSON.stringify(error));
            }
          );
      }
    } else if (datafield === "kill" && this.showButtons) {
      const taskName = this.taskGrid.getrowdata(this.editrow).taskName;
      const taskStatus = this.taskGrid.getrowdata(this.editrow).taskStateName;

      if (taskStatus !== "Running") {
      } else {
        //Call the service
        this.taskSvc.killTask(this.dataForUi.processName, taskName).subscribe(
          data => {
            this.logger.debug(data);
          },
          error => {
            this.logger.error(JSON.stringify(error));
          }
        );
      }
    }
  }

This is what I want: Use:

taskProcessing(killTask())

Function:

  taskProcessing(fn:()=>{}){
  //Call the service
  this.taskSvc.fn(this.dataForUi.processName, taskName).subscribe(
    data => {
      this.logger.debug(data);
    },
    error => {
      this.logger.error(JSON.stringify(error));
    }
  );

}

0 comments

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] Is there any method to get the URL without query string?

  • 2011-04-28 10:58:31
  • saint
  • 141131 View
  • 266 Score
  • 14 Answer
  • Tags:   javascript url

2 Answered Questions

[SOLVED] Accessing member of base class

  • 2012-10-29 12:14:41
  • Jaap
  • 98699 View
  • 115 Score
  • 2 Answer
  • Tags:   typescript

1 Answered Questions

[SOLVED] Access template reference variables from component class

14 Answered Questions

[SOLVED] Class vs. static method in JavaScript

  • 2011-10-08 02:58:37
  • lostyzd
  • 206131 View
  • 260 Score
  • 14 Answer
  • Tags:   javascript oop

6 Answered Questions

[SOLVED] Is there a way to do method overloading in TypeScript?

6 Answered Questions

[SOLVED] Call child component method from parent class - Angular

6 Answered Questions

[SOLVED] jQuery counting elements by class - what is the best way to implement this?

6 Answered Questions

[SOLVED] Simple way to reorder methods of a Java class in IntelliJ?

1 Answered Questions

23 Answered Questions

[SOLVED] What's the best way to refactor a method that has too many (6+) parameters?

  • 2009-01-13 16:07:40
  • recursive
  • 53580 View
  • 100 Score
  • 23 Answer
  • Tags:   refactoring

Sponsored Content