Architecture Overview
ngx-task is architected as a clean, multi-layered system that cleanly decouples state machine execution from Angular signals and UI bindings.
┌────────────────────────────────────────────────────────┐│ Angular Template / Directives (ngx-task/directives) │├────────────────────────────────────────────────────────┤│ Angular Signals Facade (createTask / Task Signals) │├────────────────────────────────────────────────────────┤│ Operation Adapters (PromiseAdapter / ObservableAdapter) │├────────────────────────────────────────────────────────┤│ Schedulers (Drop, Restart, Enqueue, Latest, Parallel) │├────────────────────────────────────────────────────────┤│ Pure Execution State Machine & Outcomes │└────────────────────────────────────────────────────────┘Architecture Layers
1. State Machine & Execution Outcomes
At the lowest level, every invocation is managed by a deterministic state machine. An execution moves through states:
idle→queued→running→settled(resolved|rejected|cancelled)
2. Schedulers & Concurrency Policies
Schedulers enforce invocation rules based on selected policies (drop, restart, enqueue, latest, parallel). They decide whether a new execution starts immediately, cancels active ones, queues up, or drops.
3. Operation Adapters
ngx-task natively handles both Promises (async/await) and RxJS Observables via unified adapter contracts:
- Promise Adapter: Bridges
AbortController/AbortSignalwithPromiselifecycle. - Observable Adapter: Manages
Subscriptionlifecycle and converts emissions according toobservableResultpolicies (latest,first,last).
4. Angular Signals Facade
Wraps internal state changes into fine-grained Angular Signal objects:
status: Signal of aggregate task state ('idle' | 'pending' | 'settled')pending: Signal respectingpendingDelay&minimumPendingDurationrunning: Immediate signal of active executionresult&error: Signal holders for execution outputs
5. Template Directives
Lightweight Angular directives ([taskTrigger], [taskDisableWhilePending], [taskBusy]) providing declarative template bindings.
Cooperative Cancellation Contract
Cancellation in ngx-task is cooperative, meaning tasks are given clean cancellation primitives that integrate with web standards:
- Every task handler function receives a
TaskContextcontainingsignal: AbortSignal. - Network APIs (such as
fetch()or AngularHttpClientviaAbortSignal) inspectsignal. - When
task.cancel()or component cleanup triggers:signalemitsabortevent.- Active RxJS subscriptions call
unsubscribe(). context.throwIfCancelled()throws aTaskCancelledError.