async<T> method

AsyncSignal<T> async<T>(
  1. AsyncSource<T> source, {
  2. AsyncState<T>? initialValue,
  3. JoltDebugFn? onDebug,
})

Creates an async signal hook for managing asynchronous operations.

An async signal manages the lifecycle of asynchronous operations, providing loading, success, and error states. Perfect for API calls, data fetching, etc.

Parameters:

  • source: The async source that provides the data
  • initialValue: Optional initial async state
  • onDebug: Optional debug callback for reactive system debugging

Returns: An AsyncSignal that manages async state transitions

Example:

setup(context, props) {
  final userData = useSignal.async(
    FutureSource(() async {
      final response = await http.get('/api/user');
      return User.fromJson(response.data);
    }),
  );

  return () => userData.value.map(
    loading: () => CircularProgressIndicator(),
    success: (user) => Text('Welcome, ${user.name}'),
    error: (error, _) => Text('Error: $error'),
  ) ?? SizedBox();
}

Implementation

AsyncSignal<T> async<T>(
  AsyncSource<T> source, {
  AsyncState<T>? initialValue,
  JoltDebugFn? onDebug,
}) {
  return useAutoDispose(() => AsyncSignal(
      source: source, initialValue: initialValue, onDebug: onDebug));
}