prostgles-client 4.0.264 → 4.0.265
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/FunctionQueuer.d.ts +2 -1
- package/dist/FunctionQueuer.d.ts.map +1 -1
- package/dist/FunctionQueuer.js +34 -30
- package/dist/SyncedTable/SyncedTable.d.ts +6 -11
- package/dist/SyncedTable/SyncedTable.d.ts.map +1 -1
- package/dist/SyncedTable/SyncedTable.js +44 -79
- package/dist/getDbHandler.d.ts.map +1 -1
- package/dist/getDbHandler.js +2 -2
- package/dist/getSyncHandler.d.ts +1 -1
- package/dist/getSyncHandler.d.ts.map +1 -1
- package/dist/getSyncHandler.js +1 -1
- package/dist/{react-hooks.d.ts → hooks/react-hooks.d.ts} +3 -7
- package/dist/hooks/react-hooks.d.ts.map +1 -0
- package/dist/{react-hooks.js → hooks/react-hooks.js} +15 -77
- package/dist/hooks/reactImports.d.ts +7 -0
- package/dist/hooks/reactImports.d.ts.map +1 -0
- package/dist/hooks/reactImports.js +31 -0
- package/dist/{useProstglesClient.d.ts → hooks/useProstglesClient.d.ts} +2 -2
- package/dist/hooks/useProstglesClient.d.ts.map +1 -0
- package/dist/{useProstglesClient.js → hooks/useProstglesClient.js} +7 -3
- package/dist/hooks/useSubscribe.d.ts +5 -0
- package/dist/hooks/useSubscribe.d.ts.map +1 -0
- package/dist/hooks/useSubscribe.js +45 -0
- package/dist/index.js +1 -1
- package/dist/index.no-sync.js +1 -1
- package/dist/prostgles.d.ts +2 -2
- package/dist/prostgles.d.ts.map +1 -1
- package/dist/prostgles.js +2 -2
- package/package.json +2 -2
- package/dist/ProstglesLoginProvider.d.ts +0 -11
- package/dist/ProstglesLoginProvider.d.ts.map +0 -1
- package/dist/ProstglesLoginProvider.js +0 -26
- package/dist/react-hooks.d.ts.map +0 -1
- package/dist/useProstglesClient.d.ts.map +0 -1
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _a;
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.__prglReactInstalled = exports.useFetch = exports.useSync = exports.
|
|
3
|
+
exports.__prglReactInstalled = exports.useFetch = exports.useSync = exports.usePromise = exports.useIsMounted = exports.useEffectAsync = exports.useAsyncEffectQueue = exports.useEffectDeep = exports.useMemoDeep = exports.useDeepCompareMemoize = exports.getIO = void 0;
|
|
5
4
|
const prostgles_types_1 = require("prostgles-types");
|
|
6
|
-
|
|
7
|
-
const alertNoReact = (...args) => {
|
|
8
|
-
throw "Must install react";
|
|
9
|
-
};
|
|
10
|
-
const alertNoReactT = (...args) => {
|
|
11
|
-
throw "Must install react";
|
|
12
|
-
};
|
|
13
|
-
const getReact = (throwError) => {
|
|
14
|
-
try {
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-require-imports
|
|
16
|
-
React !== null && React !== void 0 ? React : (React = require("react"));
|
|
17
|
-
}
|
|
18
|
-
catch (err) { }
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
20
|
-
if (throwError && !React)
|
|
21
|
-
throw new Error("Must install react");
|
|
22
|
-
return React;
|
|
23
|
-
};
|
|
24
|
-
exports.getReact = getReact;
|
|
25
|
-
(0, exports.getReact)();
|
|
26
|
-
const { useEffect = alertNoReact, useCallback = alertNoReact, useRef, useState = alertNoReactT,
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
28
|
-
} = (_a = React) !== null && _a !== void 0 ? _a : {};
|
|
5
|
+
const reactImports_1 = require("./reactImports");
|
|
29
6
|
const getIO = (throwError = false) => {
|
|
30
7
|
try {
|
|
31
8
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
@@ -39,7 +16,7 @@ const getIO = (throwError = false) => {
|
|
|
39
16
|
};
|
|
40
17
|
exports.getIO = getIO;
|
|
41
18
|
const useDeepCompareMemoize = (value) => {
|
|
42
|
-
const ref = useRef();
|
|
19
|
+
const ref = (0, reactImports_1.useRef)();
|
|
43
20
|
if (!(0, prostgles_types_1.isEqual)(value, ref.current)) {
|
|
44
21
|
ref.current = value;
|
|
45
22
|
}
|
|
@@ -48,19 +25,19 @@ const useDeepCompareMemoize = (value) => {
|
|
|
48
25
|
exports.useDeepCompareMemoize = useDeepCompareMemoize;
|
|
49
26
|
exports.useMemoDeep = ((callback, deps) => {
|
|
50
27
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
51
|
-
return React.useMemo(callback, deps === null || deps === void 0 ? void 0 : deps.map(exports.useDeepCompareMemoize));
|
|
28
|
+
return reactImports_1.React.useMemo(callback, deps === null || deps === void 0 ? void 0 : deps.map(exports.useDeepCompareMemoize));
|
|
52
29
|
});
|
|
53
30
|
exports.useEffectDeep = ((callback, deps) => {
|
|
54
31
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
-
useEffect(callback, deps === null || deps === void 0 ? void 0 : deps.map(exports.useDeepCompareMemoize));
|
|
32
|
+
(0, reactImports_1.useEffect)(callback, deps === null || deps === void 0 ? void 0 : deps.map(exports.useDeepCompareMemoize));
|
|
56
33
|
});
|
|
57
34
|
/**
|
|
58
35
|
* Debounce with execute first
|
|
59
36
|
* Used to ensure subscriptions are always cleaned up
|
|
60
37
|
*/
|
|
61
38
|
const useAsyncEffectQueue = (effect, deps) => {
|
|
62
|
-
const newEffect = useRef();
|
|
63
|
-
const activeEffect = useRef();
|
|
39
|
+
const newEffect = (0, reactImports_1.useRef)();
|
|
40
|
+
const activeEffect = (0, reactImports_1.useRef)();
|
|
64
41
|
const onRender = async () => {
|
|
65
42
|
var _a;
|
|
66
43
|
/**
|
|
@@ -107,7 +84,7 @@ const useAsyncEffectQueue = (effect, deps) => {
|
|
|
107
84
|
};
|
|
108
85
|
exports.useAsyncEffectQueue = useAsyncEffectQueue;
|
|
109
86
|
const useEffectAsync = (effect, inputs) => {
|
|
110
|
-
const onCleanup = useRef({
|
|
87
|
+
const onCleanup = (0, reactImports_1.useRef)({
|
|
111
88
|
cleanup: undefined,
|
|
112
89
|
effect,
|
|
113
90
|
cleanupEffect: undefined,
|
|
@@ -132,9 +109,9 @@ const useEffectAsync = (effect, inputs) => {
|
|
|
132
109
|
};
|
|
133
110
|
exports.useEffectAsync = useEffectAsync;
|
|
134
111
|
const useIsMounted = () => {
|
|
135
|
-
const isMountedRef = useRef(true);
|
|
136
|
-
const isMounted = useCallback(() => isMountedRef.current, []);
|
|
137
|
-
useEffect(() => {
|
|
112
|
+
const isMountedRef = (0, reactImports_1.useRef)(true);
|
|
113
|
+
const isMounted = (0, reactImports_1.useCallback)(() => isMountedRef.current, []);
|
|
114
|
+
(0, reactImports_1.useEffect)(() => {
|
|
138
115
|
/** React 18 Strict Mode fix (new strict mode restores the previous state on the second mount) */
|
|
139
116
|
isMountedRef.current = true;
|
|
140
117
|
return () => {
|
|
@@ -177,7 +154,7 @@ const usePromise = (f, deps = []) => {
|
|
|
177
154
|
}
|
|
178
155
|
return data;
|
|
179
156
|
};
|
|
180
|
-
const [result, setResult] = useState(isNamedObj(f) ? {} : undefined);
|
|
157
|
+
const [result, setResult] = (0, reactImports_1.useState)(isNamedObj(f) ? {} : undefined);
|
|
181
158
|
const getIsMounted = (0, exports.useIsMounted)();
|
|
182
159
|
(0, exports.useAsyncEffectQueue)(async () => {
|
|
183
160
|
let promiseResult;
|
|
@@ -204,49 +181,10 @@ const usePromise = (f, deps = []) => {
|
|
|
204
181
|
return result;
|
|
205
182
|
};
|
|
206
183
|
exports.usePromise = usePromise;
|
|
207
|
-
const useSubscribe = (subFunc, expectsOne, filter, options, hookOptions) => {
|
|
208
|
-
const { skip } = hookOptions !== null && hookOptions !== void 0 ? hookOptions : {};
|
|
209
|
-
const defaultLoadingResult = { data: undefined, error: undefined, isLoading: true };
|
|
210
|
-
const [hookResult, setHookResult] = useState(defaultLoadingResult);
|
|
211
|
-
const hookResultRef = useRef(hookResult);
|
|
212
|
-
hookResultRef.current = hookResult;
|
|
213
|
-
const getIsMounted = (0, exports.useIsMounted)();
|
|
214
|
-
(0, exports.useAsyncEffectQueue)(async () => {
|
|
215
|
-
if (!getIsMounted() || skip)
|
|
216
|
-
return;
|
|
217
|
-
if (!(0, prostgles_types_1.isEqual)(hookResultRef.current, defaultLoadingResult)) {
|
|
218
|
-
setHookResult(defaultLoadingResult);
|
|
219
|
-
}
|
|
220
|
-
const setError = (newError) => {
|
|
221
|
-
if (!getIsMounted())
|
|
222
|
-
return;
|
|
223
|
-
setHookResult({ data: undefined, error: newError, isLoading: false });
|
|
224
|
-
};
|
|
225
|
-
try {
|
|
226
|
-
const sub = await subFunc(filter, options, (newData) => {
|
|
227
|
-
if (!getIsMounted())
|
|
228
|
-
return;
|
|
229
|
-
setHookResult({
|
|
230
|
-
data: expectsOne ? newData[0] : newData,
|
|
231
|
-
error: undefined,
|
|
232
|
-
isLoading: false,
|
|
233
|
-
});
|
|
234
|
-
}, setError);
|
|
235
|
-
return () => {
|
|
236
|
-
sub.unsubscribe();
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
setError(error);
|
|
241
|
-
}
|
|
242
|
-
}, [subFunc, filter, options, skip]);
|
|
243
|
-
return hookResult;
|
|
244
|
-
};
|
|
245
|
-
exports.useSubscribe = useSubscribe;
|
|
246
184
|
const useSync = (syncFunc, basicFilter, syncOptions, hookOptions) => {
|
|
247
185
|
const { skip } = hookOptions !== null && hookOptions !== void 0 ? hookOptions : {};
|
|
248
186
|
const defaultLoadingResult = { data: undefined, error: undefined, isLoading: true };
|
|
249
|
-
const [{ data, error, isLoading }, setResult] = useState(defaultLoadingResult);
|
|
187
|
+
const [{ data, error, isLoading }, setResult] = (0, reactImports_1.useState)(defaultLoadingResult);
|
|
250
188
|
const getIsMounted = (0, exports.useIsMounted)();
|
|
251
189
|
(0, exports.useAsyncEffectQueue)(async () => {
|
|
252
190
|
if (!getIsMounted() || skip)
|
|
@@ -274,7 +212,7 @@ exports.useSync = useSync;
|
|
|
274
212
|
const useFetch = (fetchFunc, args = [], hookOptions) => {
|
|
275
213
|
const { skip, deps = [] } = hookOptions !== null && hookOptions !== void 0 ? hookOptions : {};
|
|
276
214
|
const defaultLoadingResult = { data: undefined, error: undefined, isLoading: true };
|
|
277
|
-
const [{ data, error, isLoading }, setResult] = useState(defaultLoadingResult);
|
|
215
|
+
const [{ data, error, isLoading }, setResult] = (0, reactImports_1.useState)(defaultLoadingResult);
|
|
278
216
|
const getIsMounted = (0, exports.useIsMounted)();
|
|
279
217
|
(0, exports.useAsyncEffectQueue)(async () => {
|
|
280
218
|
if (!getIsMounted() || skip)
|
|
@@ -295,5 +233,5 @@ const useFetch = (fetchFunc, args = [], hookOptions) => {
|
|
|
295
233
|
return { data, error, isLoading };
|
|
296
234
|
};
|
|
297
235
|
exports.useFetch = useFetch;
|
|
298
|
-
const __prglReactInstalled = () => Boolean(React && useRef);
|
|
236
|
+
const __prglReactInstalled = () => Boolean(reactImports_1.React && reactImports_1.useRef);
|
|
299
237
|
exports.__prglReactInstalled = __prglReactInstalled;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export type ReactT = typeof import("react");
|
|
3
|
+
declare let React: ReactT;
|
|
4
|
+
export declare const getReact: (throwError?: boolean) => ReactT;
|
|
5
|
+
declare const useEffect: typeof import("react").useEffect, useCallback: typeof import("react").useCallback, useRef: typeof import("react").useRef, useState: typeof import("react").useState;
|
|
6
|
+
export { useEffect, useCallback, useRef, useState, React };
|
|
7
|
+
//# sourceMappingURL=reactImports.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactImports.d.ts","sourceRoot":"","sources":["../../lib/hooks/reactImports.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,MAAM,GAAG,cAAc,OAAO,CAAC,CAAC;AAC5C,QAAA,IAAI,KAAK,EAAE,MAAM,CAAC;AAQlB,eAAO,MAAM,QAAQ,gBAAiB,OAAO,KAAG,MAQ/C,CAAC;AAEF,QAAA,MACE,SAAS,oCACT,WAAW,sCACX,MAAM,iCACN,QAAQ,iCAEM,CAAC;AAEjB,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.React = exports.useState = exports.useRef = exports.useCallback = exports.useEffect = exports.getReact = void 0;
|
|
5
|
+
let React;
|
|
6
|
+
const alertNoReact = (...args) => {
|
|
7
|
+
throw "Must install react";
|
|
8
|
+
};
|
|
9
|
+
const alertNoReactT = (...args) => {
|
|
10
|
+
throw "Must install react";
|
|
11
|
+
};
|
|
12
|
+
const getReact = (throwError) => {
|
|
13
|
+
try {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-require-imports
|
|
15
|
+
React !== null && React !== void 0 ? React : (exports.React = React = require("react"));
|
|
16
|
+
}
|
|
17
|
+
catch (err) { }
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
19
|
+
if (throwError && !React)
|
|
20
|
+
throw new Error("Must install react");
|
|
21
|
+
return React;
|
|
22
|
+
};
|
|
23
|
+
exports.getReact = getReact;
|
|
24
|
+
(0, exports.getReact)();
|
|
25
|
+
const { useEffect = alertNoReact, useCallback = alertNoReact, useRef, useState = alertNoReactT,
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
27
|
+
} = (_a = React) !== null && _a !== void 0 ? _a : {};
|
|
28
|
+
exports.useEffect = useEffect;
|
|
29
|
+
exports.useCallback = useCallback;
|
|
30
|
+
exports.useRef = useRef;
|
|
31
|
+
exports.useState = useState;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type DBSchema, type DBSchemaTable, type MethodHandler, type UserLike } from "prostgles-types";
|
|
2
2
|
import type { ManagerOptions, Socket, SocketOptions } from "socket.io-client";
|
|
3
|
-
import type { AuthHandler } from "
|
|
4
|
-
import { type DBHandlerClient, type InitOptions } from "
|
|
3
|
+
import type { AuthHandler } from "../getAuthHandler";
|
|
4
|
+
import { type DBHandlerClient, type InitOptions } from "../prostgles";
|
|
5
5
|
export type OnReadyParams<DBSchema, U extends UserLike = UserLike> = {
|
|
6
6
|
dbo: DBHandlerClient<DBSchema>;
|
|
7
7
|
methods: MethodHandler | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProstglesClient.d.ts","sourceRoot":"","sources":["../../lib/hooks/useProstglesClient.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,QAAQ,EACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,EAAa,KAAK,eAAe,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAIjF,MAAM,MAAM,aAAa,CAAC,QAAQ,EAAE,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI;IACnE,GAAG,EAAE,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC/B,OAAO,EAAE,aAAa,GAAG,SAAS,CAAC;IACnC,WAAW,EAAE,aAAa,EAAE,GAAG,SAAS,CAAC;IACzC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACrB,WAAW,EAAE,OAAO,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,mBAAmB,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,cAAc,GAAG,aAAa,CAAC,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAEjG,MAAM,MAAM,uBAAuB,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,SAAS,GAAG,QAAQ,CAAC,GAAG;IACxF;;OAEG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAC;IACpC,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AACF,MAAM,MAAM,oBAAoB,CAAC,GAAG,IAChC;IAAE,SAAS,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;IAAC,KAAK,CAAC,EAAE,SAAS,CAAA;CAAE,GAC5D,CAAC;IAAE,SAAS,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,KAAK,CAAC;IAAC,KAAK,CAAC,EAAE,SAAS,CAAA;CAAE,GAAG,GAAG,CAAC,GACjE;IAAE,SAAS,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAEzD,eAAO,MAAM,kBAAkB,0GAI5B,uBAAuB,uDA2DzB,CAAC"}
|
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.useProstglesClient = void 0;
|
|
8
8
|
const prostgles_types_1 = require("prostgles-types");
|
|
9
|
-
const SyncedTable_1 = require("
|
|
10
|
-
const prostgles_1 = require("
|
|
9
|
+
const SyncedTable_1 = require("../SyncedTable/SyncedTable");
|
|
10
|
+
const prostgles_1 = require("../prostgles");
|
|
11
11
|
const react_hooks_1 = require("./react-hooks");
|
|
12
|
+
const reactImports_1 = require("./reactImports");
|
|
12
13
|
const useProstglesClient = ({ skip, socketOptions: socketPathOrOptions, ...initOpts } = {}) => {
|
|
13
|
-
const { useRef, useState } = (0,
|
|
14
|
+
const { useRef, useState } = (0, reactImports_1.getReact)(true);
|
|
14
15
|
const [onReadyArgs, setOnReadyArgs] = useState({
|
|
15
16
|
isLoading: true,
|
|
16
17
|
});
|
|
@@ -58,6 +59,9 @@ const useProstglesClient = ({ skip, socketOptions: socketPathOrOptions, ...initO
|
|
|
58
59
|
});
|
|
59
60
|
return () => {
|
|
60
61
|
socket.disconnect();
|
|
62
|
+
socket.emit = () => {
|
|
63
|
+
throw "Socket disconnected";
|
|
64
|
+
};
|
|
61
65
|
};
|
|
62
66
|
}, [initOpts, socketPathOrOptions, skip]);
|
|
63
67
|
return onReadyArgs;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { HookOptions } from "../prostgles";
|
|
2
|
+
import { type DataFetchHookResult } from "./react-hooks";
|
|
3
|
+
import { type SubscriptionHandler } from "prostgles-types";
|
|
4
|
+
export declare const useSubscribe: (subFunc: (filter: any, options: any, onData: any, onError: any) => Promise<SubscriptionHandler>, expectsOne: boolean, filter: any, options: any, hookOptions?: HookOptions) => DataFetchHookResult;
|
|
5
|
+
//# sourceMappingURL=useSubscribe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSubscribe.d.ts","sourceRoot":"","sources":["../../lib/hooks/useSubscribe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAqC,KAAK,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAC5F,OAAO,EAAW,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAGpE,eAAO,MAAM,YAAY,qBACL,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,GAAG,KAAK,QAAQ,mBAAmB,CAAC,cACnF,OAAO,UACX,GAAG,WACF,GAAG,gBACE,WAAW,wBAyC1B,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSubscribe = void 0;
|
|
4
|
+
const react_hooks_1 = require("./react-hooks");
|
|
5
|
+
const prostgles_types_1 = require("prostgles-types");
|
|
6
|
+
const reactImports_1 = require("./reactImports");
|
|
7
|
+
const useSubscribe = (subFunc, expectsOne, filter, options, hookOptions) => {
|
|
8
|
+
const { skip } = hookOptions !== null && hookOptions !== void 0 ? hookOptions : {};
|
|
9
|
+
const defaultLoadingResult = { data: undefined, error: undefined, isLoading: true };
|
|
10
|
+
const [hookResult, setHookResult] = (0, reactImports_1.useState)(defaultLoadingResult);
|
|
11
|
+
const hookResultRef = (0, reactImports_1.useRef)(hookResult);
|
|
12
|
+
hookResultRef.current = hookResult;
|
|
13
|
+
const getIsMounted = (0, react_hooks_1.useIsMounted)();
|
|
14
|
+
(0, react_hooks_1.useAsyncEffectQueue)(async () => {
|
|
15
|
+
if (!getIsMounted() || skip)
|
|
16
|
+
return;
|
|
17
|
+
if (!(0, prostgles_types_1.isEqual)(hookResultRef.current, defaultLoadingResult)) {
|
|
18
|
+
setHookResult(defaultLoadingResult);
|
|
19
|
+
}
|
|
20
|
+
const setError = (newError) => {
|
|
21
|
+
if (!getIsMounted())
|
|
22
|
+
return;
|
|
23
|
+
setHookResult({ data: undefined, error: newError, isLoading: false });
|
|
24
|
+
};
|
|
25
|
+
try {
|
|
26
|
+
const sub = await subFunc(filter, options, (newData) => {
|
|
27
|
+
if (!getIsMounted())
|
|
28
|
+
return;
|
|
29
|
+
setHookResult({
|
|
30
|
+
data: expectsOne ? newData[0] : newData,
|
|
31
|
+
error: undefined,
|
|
32
|
+
isLoading: false,
|
|
33
|
+
});
|
|
34
|
+
}, setError);
|
|
35
|
+
return () => {
|
|
36
|
+
sub.unsubscribe();
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
setError(error);
|
|
41
|
+
}
|
|
42
|
+
}, [subFunc, filter, options, skip]);
|
|
43
|
+
return hookResult;
|
|
44
|
+
};
|
|
45
|
+
exports.useSubscribe = useSubscribe;
|