/**
* @license
* Copyright 2025-2026 Open Home Foundation
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Wraps an async function to be used as an event handler.
* Catches any errors and logs them, optionally calling an error callback.
*
* Usage in Lit templates:
* ```
*
* ```
*
* With custom error handling:
* ```
*
* ```
*
* @param fn - Async function to execute
* @param onError - Optional error callback
* @returns Event handler function
*/
export function handleAsync(fn: () => Promise, onError?: (error: Error) => void): () => void {
return () => {
fn().catch((error: Error) => {
console.error("Async operation failed:", error);
onError?.(error);
});
};
}
/**
* Wraps an async function that takes an event parameter.
*
* Usage:
* ```
* this._handleChange(e))}>
* ```
*/
export function handleAsyncEvent(
fn: (event: E) => Promise,
onError?: (error: Error) => void,
): (event: E) => void {
return (event: E) => {
fn(event).catch((error: Error) => {
console.error("Async operation failed:", error);
onError?.(error);
});
};
}
/**
* Fire-and-forget helper for calling async methods from sync contexts.
* Use this when you need to call an async method but can't await it.
*
* Usage in lifecycle methods:
* ```
* connectedCallback() {
* super.connectedCallback();
* fireAndForget(this._loadData());
* }
* ```
*
* @param promise - Promise to execute
* @param onError - Optional error callback
*/
export function fireAndForget(promise: Promise, onError?: (error: Error) => void): void {
promise.catch((error: Error) => {
console.error("Async operation failed:", error);
onError?.(error);
});
}