Command pattern for Angular applications
npm install ng-commanderNG Commander is a service designed to manage and execute commands in an Angular application using RxJS for reactive programming. It provides a robust framework for handling command execution, retries, and error management.
- Command Execution: Execute commands asynchronously and manage their state.
- Error Handling: Automatically retry commands that fail, with configurable retry limits.
- State Management: Track the state of command execution (IDLE, EXECUTING, DONE, ERROR).
- Reactive Programming: Utilize RxJS for reactive command management.
To install NG Commander, run the following command:
``bash`
npm install ng-commander
Import the Commander service into your Angular module or component:
`typescript`
import { Commander } from "ng-commander";
Initialize the service with a configuration object:
`typescript`
constructor(private commander: Commander) {
this.commander.init({
error: {
maxNumberOfRetries: 3,
},
});
}
Add commands to the commander for execution:
`typescript
const command: Command
execute: () => {
// Command execution logic
return of("Command executed successfully");
},
};
this.commander.addCommand(command);
`
Subscribe to command events to handle execution results:
`typescript
this.commander.commands$.subscribe((commands) => {
console.log("Commands:", commands);
});
this.commander.commandsDone$.subscribe((doneCommands) => {
console.log("Done Commands:", doneCommands);
});
this.commander.commandsInError$.subscribe((errorCommands) => {
console.log("Error Commands:", errorCommands);
});
this.commander.commandsDead$.subscribe((deadCommands) => {
console.log("Dead Commands:", deadCommands);
});
this.commander.state$.subscribe((state) => {
console.log("State:", state);
});
`
- init(configuration: CommanderConfiguration): Initialize the commander with a configuration.addCommand
- : Add a command to the commander.replayCommandsInError()
- : Replay commands that are in error.getCommands(type: CommandsType)
- : Get commands of a specific type.getState()
- : Get the current state of the commander.stop()
- : Stop the commander.
- commands$: Observable of all commands.commandsDone$
- : Observable of commands that have been executed successfully.commandsInError$
- : Observable of commands that have encountered errors.commandsDead$
- : Observable of commands that have been marked as dead.state$`: Observable of the current state of the commander.
-
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License.