AsyncAction<V> class
An action whose lifecycle states may be controlled and deferred by receivers.
The action moves through the following states.
- creation
- reception
- execution
- completion
Creation The action is created by the broadcaster.
Reception Event is added to a stream or otherwise broadcast to receivers for the chance to cancel or defer action execution.
Execution The broadcaster's execution closure is run, only if all
whetherToCancel
futures return false, and after all deferExecution
futures complete.
Completion The action has either executed or has been cancelled.
Receivers may cancel an action before execution and can do so asynchronously by registering a future via cancelIf (or synchronously via cancel).
The execution of the action can be delayed by registering a Future via defer. Deferring futures are only
Finally, receivers and the broadcaster may run code after the action has executed (or has been cancelled), via the onDone future.
Usage is as follows:
Component Firing Event:
var ctrl = new AsyncActionController();
streamCtrl.add(ctrl.action);
ctrl.execute((shouldProceed) {
// shouldProceed
is true only if whetherToCancel
futures all
// returned false.
}
return ctrl.action.onDone.then((completed) {
// Clean up.
if (completed) {
} else {
}
}
Subscribing Component
<my-component (saveEvent) = "onSave($event)">
class MyComponent {
void onSave(AsyncAction action) {
// possibly cancel the action.
action.cancelIf(whetherToCancelFuture));
// lazily defer the action.
action.defer(action.onDefer.then(someAsyncMethod));
// do something after the action has executed.
action.onDone.then(onEventComplete);
}
}
- Implementers
Constructors
Properties
- cancelled → bool
-
Indicates whether the action has been cancelled.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isDone → bool?
-
Indicates that the action has either completed or was cancelled.
no setter
-
onDefer
→ Future<
bool> -
A Future which resolves only if an action has not been cancelled, and
and before the action execution. Use this future to begin a chain for
#defer
. Beginning a chain with this future allows for lazy evaluation of the deferring future. This future resolves to false if the action has been cancelled. If the action is cancelled, no futures registered with defer will be awaited before resolving the onDone future.no setter -
onDone
→ Future<
V> -
Future which resolves to null if the action is cancelled. If the execution
is not cancelled, the future resolves to whatever the execution returns.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancel(
) → void - Synchronously cancel the action. Once called, all future calls to cancelIf and defer will have no effect, nor will any futures previously registered with cancelIf be awaited before resolving the onDefer and onDone futures.
-
cancelIf(
Future< bool> whetherToCancel) → void -
Register
whetherToCancel
to later decide whether to cancel. It is expected that whatever async processing required to determine whether to cancel the action is started by the action receiver. The resolved value will only be looked at once the action broadcaster has attempted to execute. -
defer(
Future executionDeferral) → void - Defer the action execution, if the action is not already executed or canceled. Use onDefer to delay starting your executionDeferral until the action is ready to execute.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited