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

12 Answered Questions

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

  • 2011-04-28 10:58:31
  • saint
  • 129211 View
  • 240 Score
  • 12 Answer
  • Tags:   javascript url

5 Answered Questions

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

2 Answered Questions

[SOLVED] Accessing member of base class

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

14 Answered Questions

[SOLVED] Class vs. static method in JavaScript

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

6 Answered Questions

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

1 Answered Questions

[SOLVED] Access template reference variables from component class

5 Answered Questions

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

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
  • 48922 View
  • 91 Score
  • 23 Answer
  • Tags:   refactoring

Sponsored Content