|
| 1 | +--- |
| 2 | +adr: "0029" |
| 3 | +status: "Accepted" |
| 4 | +date: 2025-11-24 |
| 5 | +tags: [clients, angular] |
| 6 | +--- |
| 7 | + |
| 8 | +# 0028 - Adopt Angular Signals for Component State |
| 9 | + |
| 10 | +<AdrTable frontMatter={frontMatter}></AdrTable> |
| 11 | + |
| 12 | +## Context and Problem Statement |
| 13 | + |
| 14 | +Angular has adopted a new reactive primitive, signals. Signals have various improvements over RxJS: |
| 15 | +performance, simplicity, and deeper integrations into the rest of the framework. |
| 16 | + |
| 17 | +RxJS will become an optional dependency of Angular. Certain asynchronous workflows will still |
| 18 | +benefit from RxJS (signals are synchronous). Furthermore, being a part of the core Angular library, |
| 19 | +Angular signals cannot readily be used in non-Angular environments. |
| 20 | + |
| 21 | +As such, Signals should be the default when operating _in the view layer_: components, directives, |
| 22 | +pipes, and services that are tightly coupled to the UI/Angular. Services that primarily deal with |
| 23 | +business logic should prefer RxJS to maximize portability (or, even better, be moved to the Rust |
| 24 | +SDK). |
| 25 | + |
| 26 | +## Decision |
| 27 | + |
| 28 | +Signal-based APIs (inputs, outputs, child queries) will be required in components and directives via |
| 29 | +linting: |
| 30 | + |
| 31 | +- `@Input()` → `input()` |
| 32 | +- `@Output()` → `output()` |
| 33 | +- `@ViewChild`/`@ContentChild` → `viewChild()`/`contentChild()` |
| 34 | + |
| 35 | +Services tightly coupled to Angular should use signals. Services with non-presentational business |
| 36 | +logic should prefer RxJS for portability. Use `toSignal()` and `toObservable()` to bridge between |
| 37 | +RxJS and signals when necessary. |
| 38 | + |
| 39 | +## Implementation Plan |
| 40 | + |
| 41 | +New code must use signal-based APIs; existing code will be migrated gradually. Angular provides |
| 42 | +automatic code migrations for signal |
| 43 | +[inputs](https://angular.dev/reference/migrations/signal-inputs) and |
| 44 | +[queries](https://angular.dev/reference/migrations/signal-queries). |
| 45 | + |
| 46 | +Much of `libs/components` was updated using these migrators: |
| 47 | +https://github.com/bitwarden/clients/pull/15340 |
| 48 | + |
| 49 | +See the |
| 50 | +[Angular Modernization Guide](https://contributing.bitwarden.com/contributing/code-style/web/angular-migration-guide/#signals) |
| 51 | +for more information. |
| 52 | + |
| 53 | +## Consequences |
| 54 | + |
| 55 | +**Positive:** |
| 56 | + |
| 57 | +- Improved performance and simpler change detection |
| 58 | +- Clear path to removing Zone.js dependency |
| 59 | +- Better debugging experience |
| 60 | +- Aligned with Angular's direction |
| 61 | +- Simpler than RxJS for many common use cases |
| 62 | + |
| 63 | +**Negative:** |
| 64 | + |
| 65 | +- Temporary complexity during migration with mixed RxJS/Signals patterns |
| 66 | +- Learning curve for team members unfamiliar with signals |
| 67 | +- Migration effort required for existing codebase |
| 68 | + |
| 69 | +## Reasons against other options |
| 70 | + |
| 71 | +- Disallow usage of signals and only use RxJS for reactivity. |
| 72 | + - This is a non-starter. Signals are being built into Angular. |
| 73 | +- Continue the status quo of ad hoc usage. |
| 74 | + - Having multiple ways to do the same thing leads to analysis paralysis and complicated code. |
| 75 | + - Signals + OnPush change detection provide a clear path to removing Zone.js. With that comes |
| 76 | + notable performance and debugging improvements. |
| 77 | + |
| 78 | +## Further reading |
| 79 | + |
| 80 | +- [Angular docs](https://angular.dev/guide/signals) |
0 commit comments