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: A function that returns an async source providing the data
  • initialValue: Optional function that returns the 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> Function() source, {
  AsyncState<T> Function()? initialValue,
  JoltDebugFn? onDebug,
}) {
  return useAutoDispose(() => AsyncSignal(
      source: source(),
      initialValue: initialValue?.call(),
      onDebug: onDebug));
}