What is an Action and how does it work?
masterAn Action is an abstraction built on top of RxSwift that manages the execution of asynchronous work.
An Action is initialized with a workFactory: a closure that accepts an input and returns an Observable. When execute() is called, the workFactory is invoked with the provided input, and the Action subscribes to the resulting observable.
Key behaviors:
- Single Execution: An action can only execute one task at a time. Attempting to execute an action that is already running will result in an error.
- State Management: It tracks whether it is currently
executing(via anexecutingproperty) and whether it isenabled. - Event Aggregation: It aggregates
NextandErrorevents from all individual executions across itselementsanderrorsproperties. - Enabled State: An action can only be executed while it is "enabled". You can control this via the
enabledproperty or by providing anenabledIfobservable during initialization.
action: Action<String, Bool> = Action(workFactory: { input in
return networkLibrary.checkEmailExists(input)
})
// To run the work:
action.execute("ash@ashfurrow.com")