All files / blong-browser/src/hooks useHandler.ts

0% Statements 0/55
0% Branches 0/1
0% Functions 0/1
0% Lines 0/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
/**
 * useHandler / useHandlerMutation — low-level direct registry dispatch hooks.
 * Use these only when the action-based abstractions are not suitable.
 */
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query';
import {useCallback} from 'react';
import {useBlong} from '../context/BlongContext.js';
import type {IBlongError} from '../types/action.js';

/**
 * useHandler — subscribe to a registry method result.
 * Equivalent to useQuery but directly calling the registry.
 */
export function useHandler<TResult = unknown>(
    method: string,
    params?: Record<string, unknown>,
    options?: {enabled?: boolean; staleTime?: number},
) {
    const {handler} = useBlong();
    return useQuery<TResult, IBlongError>({
        queryKey: [method, params ?? {}],
        queryFn: () => handler[method](params ?? {}, {}) as Promise<TResult>,
        enabled: options?.enabled ?? true,
        staleTime: options?.staleTime,
    });
}

/**
 * useHandlerMutation — call a registry method as a mutation.
 */
export function useHandlerMutation<
    TResult = unknown,
    TParams extends Record<string, unknown> = Record<string, unknown>,
>(method: string, invalidateQueries?: string[]) {
    const {handler} = useBlong();
    const queryClient = useQueryClient();

    return useMutation<TResult, IBlongError, TParams>({
        mutationFn: params => handler[method](params, {}) as Promise<TResult>,
        onSuccess: () => {
            for (const key of invalidateQueries ?? []) {
                void queryClient.invalidateQueries({queryKey: [key]});
            }
        },
    });
}

/** useHandler as an imperative call (no subscription) */
export function useHandlerCall<TResult = unknown>(method: string) {
    const {handler} = useBlong();
    return useCallback(
        (params?: Record<string, unknown>) => handler[method](params ?? {}, {}) as Promise<TResult>,
        [handler, method],
    );
}