Action
public final class Action<Input, Output, Error> where Error : Error
extension Action: BindingTargetProvider
Action represents a repeatable work like SignalProducer . But on top of the
isolation of produced Signal s from a SignalProducer , Action provides
higher-order features like availability and mutual exclusion.
Similar to a produced Signal from a SignalProducer , each unit of the repreatable
work may output zero or more values, and terminate with or without an error at some
point.
The core of Action is the execute closure it created with. For every execution
attempt with a varying input, if the Action is enabled, it would request from the
execute closure a customized unit of work — represented by a SignalProducer .
Specifically, the execute closure would be supplied with the latest state of
Action and the external input from apply().
Action enforces serial execution, and disables the Action during the execution.
-
The lifetime of the
Action.Declaration
Swift
public let lifetime: Lifetime -
A signal of all values generated from all units of work of the
Action.In other words, this sends every value from every unit of work that the
Actionexecutes.Declaration
Swift
public let values: Signal <Output, Never> -
A signal of all errors generated from all units of work of the
Action.In other words, this sends every error from every unit of work that the
Actionexecutes.Declaration
Swift
public let errors: Signal <Error, Never> -
A signal of all failed attempts to start a unit of work of the
Action.Declaration
Swift
public let disabledErrors: Signal <(), Never> -
A signal of all completed events generated from applications of the action.
In other words, this will send completed events from every signal generated by each SignalProducer returned from apply().
Declaration
Swift
public let completed: Signal <(), Never> -
Whether the action is currently executing.
Declaration
Swift
public let isExecuting: Property <Bool> -
Whether the action is currently enabled.
Declaration
Swift
public let isEnabled: Property <Bool> -
Initializes an
Actionthat would be conditionally enabled depending on its state.When the
Actionis asked to start the execution with an input value, a unit of work — represented by aSignalProducer— would be created by invokingexecutewith the latest state and the input value.Note
Actionguarantees that changes tostateare observed in a thread-safe way. Thus, the value passed toisEnabledwill always be identical to the value passed toexecute, for each application of the action.Note
This initializer should only be used if you need to provide custom input can also influence whether the action is enabled. The various convenience initializers should cover most use cases.
Declaration
Swift
public init<State: PropertyProtocol >(state: State, enabledIf isEnabled: @escaping (State.Value) -> Bool, execute: @escaping (State.Value, Input) -> SignalProducer <Output, Error>)Parameters
stateA property to be the state of the
Action.isEnabledA predicate which determines the availability of the
Action, given the latestActionstate.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat uses a property as its state.When the
Actionis asked to start the execution, a unit of work — represented by aSignalProducer— would be created by invokingexecutewith the latest value of the state.Declaration
Swift
public convenience init<P: PropertyProtocol >(state: P, execute: @escaping (P.Value, Input) -> SignalProducer <Output, Error>)Parameters
stateA property to be the state of the
Action.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat would be conditionally enabled.When the
Actionis asked to start the execution with an input value, a unit of work — represented by aSignalProducer— would be created by invokingexecutewith the input value.Declaration
Swift
public convenience init<P: PropertyProtocol >(enabledIf isEnabled: P, execute: @escaping (Input) -> SignalProducer <Output, Error>) where P.Value == BoolParameters
isEnabledA property which determines the availability of the
Action.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat uses a property of optional as its state.When the
Actionis asked to start executing, a unit of work (represented by aSignalProducer) is created by invokingexecutewith the latest value of the state and theinputthat was passed toapply().If the property holds a
nil, theActionwould be disabled until it is notnil.Declaration
Swift
public convenience init<P: PropertyProtocol , T>(unwrapping state: P, execute: @escaping (T, Input) -> SignalProducer <Output, Error>) where P.Value == T?Parameters
stateA property of optional to be the state of the
Action.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat uses aValidatingPropertyas its state.When the
Actionis asked to start executing, a unit of work (represented by aSignalProducer) is created by invokingexecutewith the latest value of the state and theinputthat was passed toapply().If the
ValidatingPropertydoes not hold a valid value, theActionwould be disabled until it’s valid.Declaration
Swift
public convenience init<T, E>(validated state: ValidatingProperty <T, E>, execute: @escaping (T, Input) -> SignalProducer <Output, Error>)Parameters
stateA
ValidatingPropertyto be the state of theAction.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat would always be enabled.When the
Actionis asked to start the execution with an input value, a unit of work — represented by aSignalProducer— would be created by invokingexecutewith the input value.Declaration
Swift
public convenience init(execute: @escaping (Input) -> SignalProducer <Output, Error>)Parameters
executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Create a
SignalProducerthat would attempt to create and start a unit of work of theAction. TheSignalProducerwould forward only events generated by the unit of work it created.If the execution attempt is failed, the producer would fail with
ActionError.disabled.Declaration
Swift
public func apply(_ input: Input) -> SignalProducer <Output, ActionError <Error>>Parameters
inputA value to be used to create the unit of work.
Return Value
A producer that forwards events generated by its started unit of work, or emits
ActionError.disabledif the execution attempt is failed.
-
Create a
SignalProducerthat would attempt to create and start a unit of work of theAction. TheSignalProducerwould forward only events generated by the unit of work it created.If the execution attempt is failed, the producer would fail with
ActionError.disabled.Declaration
Swift
public func apply() -> SignalProducer <Output, ActionError <Error>>Return Value
A producer that forwards events generated by its started unit of work, or emits
ActionError.disabledif the execution attempt is failed. -
Initializes an
Actionthat uses a property of optional as its state.When the
Actionis asked to start the execution, a unit of work — represented by aSignalProducer— would be created by invokingexecutewith the latest value of the state.If the property holds a
nil, theActionwould be disabled until it is notnil.Declaration
Swift
public convenience init<P: PropertyProtocol , T>(unwrapping state: P, execute: @escaping (T) -> SignalProducer <Output, Error>) where P.Value == T?Parameters
stateA property of optional to be the state of the
Action.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat uses aValidatingPropertyas its state.When the
Actionis asked to start executing, a unit of work (represented by aSignalProducer) is created by invokingexecutewith the latest value of the state and theinputthat was passed toapply().If the
ValidatingPropertydoes not hold a valid value, theActionwould be disabled until it’s valid.Declaration
Swift
public convenience init<T, E>(validated state: ValidatingProperty <T, E>, execute: @escaping (T) -> SignalProducer <Output, Error>)Parameters
stateA
ValidatingPropertyto be the state of theAction.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction. -
Initializes an
Actionthat uses a property as its state.When the
Actionis asked to start the execution, a unit of work — represented by aSignalProducer— would be created by invokingexecutewith the latest value of the state.Declaration
Swift
public convenience init<P: PropertyProtocol , T>(state: P, execute: @escaping (T) -> SignalProducer <Output, Error>) where P.Value == TParameters
stateA property to be the state of the
Action.executeA closure that produces a unit of work, as
SignalProducer, to be executed by theAction.