UNPKG

prostgles-client

Version:

Reactive client for Postgres

36 lines (35 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSync = void 0; const reactImports_1 = require("../hooks/reactImports"); const { useState } = reactImports_1.reactImports; const useAsyncEffectQueue_1 = require("./useAsyncEffectQueue"); const useIsMounted_1 = require("./useIsMounted"); const useSync = (syncFunc, basicFilter, syncOptions, hookOptions) => { const { skip } = hookOptions !== null && hookOptions !== void 0 ? hookOptions : {}; const defaultLoadingResult = { data: undefined, error: undefined, isLoading: true }; const [{ data, error, isLoading }, setResult] = useState(defaultLoadingResult); const getIsMounted = (0, useIsMounted_1.useIsMounted)(); (0, useAsyncEffectQueue_1.useAsyncEffectQueue)(async () => { if (!getIsMounted() || skip) return; const setError = (newError) => { if (!getIsMounted()) return; setResult({ data: undefined, error: newError, isLoading: false }); }; try { const syncHandlers = await syncFunc(basicFilter, syncOptions, (newData) => { if (!getIsMounted()) return; setResult({ data: newData, error: undefined, isLoading: false }); }, setError); return syncHandlers.$unsync; } catch (error) { setError(error); } }, [syncFunc, basicFilter, syncOptions, skip]); return { data, error, isLoading }; }; exports.useSync = useSync;