DEV Community

Cover image for Angular 19: linkedSignal & Resource API
Dennis Hering
Dennis Hering

Posted on

Angular 19: linkedSignal & Resource API

Angular 19 introduces two significant features aimed at enhancing reactive programming and data management within Angular applications: the linkedSignal function and the Resource API. These additions address existing challenges in state synchronization and asynchronous data handling, providing developers with more streamlined and efficient tools.

linkedSignal: Enhanced Signal Management

In previous Angular versions, managing state that depended on other signals often required complex workarounds. Developers typically used computed() signals to derive values based on other signals. However, computed() signals are read-only, limiting flexibility when a writable signal was needed that could both depend on other signals and be updated independently.

The linkedSignal function resolves this issue by creating a writable signal that automatically updates its value based on changes in a source signal. This functionality simplifies the synchronization between signals and facilitates the implementation of reset patterns, leading to more maintainable and predictable state management.

Example:

import { signal, linkedSignal } from '@angular/core';

const sourceSignal = signal(0);
const updatedSignal = linkedSignal({
  source: sourceSignal,
  computation: () => sourceSignal() * 5,
});
Enter fullscreen mode Exit fullscreen mode

In this example, updatedSignal will always be five times the value of sourceSignal and will automatically adjust as sourceSignal changes.

Addressing Existing Challenges:

Before the introduction of linkedSignal, developers faced difficulties in creating signals that were both dependent on other signals and writable. This often led to convoluted code structures and increased the potential for errors. By providing a straightforward method to create such signals, linkedSignal enhances code clarity and reduces the likelihood of bugs related to state management.

Resource API: Streamlined Data Loading

Managing asynchronous data loading, especially through HTTP requests, has been a complex task in Angular applications. Developers needed to handle various states of data fetching manually, including loading, success, and error states, which often resulted in verbose and error-prone code.

The Resource API in Angular 19 offers a reactive approach to loading resources, particularly for read operations like HTTP GET requests. It allows developers to define a loader function that asynchronously fetches data and provides signals to monitor the current status and handle errors effectively.

Example:

import { resource } from '@angular/core';

const productResource = resource({
  loader: async () => {
    const response = await fetch('https://api.example.com/products');
    return response.json();
  }
});
Enter fullscreen mode Exit fullscreen mode

In this example, productResource is initialized with a loader function that fetches data from the specified API. The Resource API manages the loading state and any potential errors, simplifying the data fetching process.

Key Features of the Resource API:

  • Status Tracking: Signals such as status, error, and isLoading allow developers to monitor the current state of the data loading process, facilitating better user feedback and error handling. The status signal can have the following values:
    • Idle (0): The resource is in its initial state and has not started loading.
    • Error (1): An error occurred during the loading process.
    • Loading (2): The resource is currently loading data.
    • Reloading (3): The resource is reloading data, typically after a previous load.
    • Resolved (4): The resource has successfully loaded data.
    • Local (5): The resource's data has been updated locally without a new load.
  • Local Updates: The update method enables local modifications to the loaded data without triggering a new loading process, providing flexibility in data manipulation.
  • Request Management: The Resource API automatically restarts the loading process when dependent signals change and can cancel ongoing requests to prevent race conditions, ensuring data consistency and integrity.

Addressing Existing Challenges:

Prior to the Resource API, developers had to implement manual logic to handle various states of data fetching, leading to increased complexity and potential for errors. The Resource API abstracts these concerns, offering a declarative and concise way to manage asynchronous data operations, thereby reducing boilerplate code and improving application reliability.

Conclusion

The introduction of linkedSignal and the Resource API in Angular 19 represents significant advancements in reactive programming and data management. These features address longstanding challenges by providing developers with more flexible, efficient, and maintainable tools for state synchronization and asynchronous data handling. By leveraging these new capabilities, developers can create modern web applications with enhanced performance and user experience.

Top comments (0)