akeyless-client-commons 1.1.37 → 1.1.39
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/components/index.js +18 -13
- package/dist/components/index.mjs +18 -13
- package/dist/helpers/index.js +18 -13
- package/dist/helpers/index.mjs +18 -13
- package/dist/hooks/index.d.mts +11 -1
- package/dist/hooks/index.d.ts +11 -1
- package/dist/hooks/index.js +252 -59
- package/dist/hooks/index.mjs +248 -59
- package/package.json +1 -1
package/dist/components/index.js
CHANGED
|
@@ -1157,32 +1157,37 @@ function cn() {
|
|
|
1157
1157
|
var import_firestore2 = require("firebase/firestore");
|
|
1158
1158
|
var import_moment_timezone = __toESM(require("moment-timezone"));
|
|
1159
1159
|
function timestamp_to_string(firebaseTimestamp, options) {
|
|
1160
|
+
var _options_defaultReturnedValue, _options_format, _options_fromFormat;
|
|
1161
|
+
var _ref = {
|
|
1162
|
+
defaultReturnedValue: (_options_defaultReturnedValue = options === null || options === void 0 ? void 0 : options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-",
|
|
1163
|
+
format: (_options_format = options === null || options === void 0 ? void 0 : options.format) !== null && _options_format !== void 0 ? _options_format : "DD/MM/YYYY HH:mm:ss",
|
|
1164
|
+
fromFormat: (_options_fromFormat = options === null || options === void 0 ? void 0 : options.fromFormat) !== null && _options_fromFormat !== void 0 ? _options_fromFormat : "DD/MM/YYYY HH:mm:ss",
|
|
1165
|
+
tz: options === null || options === void 0 ? void 0 : options.tz
|
|
1166
|
+
}, defaultReturnedValue = _ref.defaultReturnedValue, format = _ref.format, fromFormat = _ref.fromFormat, tz = _ref.tz;
|
|
1160
1167
|
if (!firebaseTimestamp) {
|
|
1161
|
-
|
|
1162
|
-
return (_options_defaultReturnedValue = options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-";
|
|
1168
|
+
return defaultReturnedValue;
|
|
1163
1169
|
}
|
|
1164
1170
|
var date;
|
|
1165
1171
|
if (_instanceof(firebaseTimestamp, import_firestore2.Timestamp)) {
|
|
1166
1172
|
date = firebaseTimestamp.toDate();
|
|
1167
1173
|
} else if (_instanceof(firebaseTimestamp, Date)) {
|
|
1168
1174
|
date = firebaseTimestamp;
|
|
1169
|
-
} else if (firebaseTimestamp._seconds && firebaseTimestamp._nanoseconds) {
|
|
1170
|
-
date = new Date(firebaseTimestamp._seconds * 1e3 + firebaseTimestamp._nanoseconds / 1e6);
|
|
1171
1175
|
} else if (typeof firebaseTimestamp === "string") {
|
|
1172
|
-
date = import_moment_timezone.default.utc(firebaseTimestamp,
|
|
1176
|
+
date = import_moment_timezone.default.utc(firebaseTimestamp, fromFormat).toDate();
|
|
1173
1177
|
if (isNaN(date.getTime())) {
|
|
1174
|
-
|
|
1175
|
-
return (_options_defaultReturnedValue1 = options.defaultReturnedValue) !== null && _options_defaultReturnedValue1 !== void 0 ? _options_defaultReturnedValue1 : "-";
|
|
1178
|
+
return defaultReturnedValue;
|
|
1176
1179
|
}
|
|
1180
|
+
} else if (firebaseTimestamp._seconds) {
|
|
1181
|
+
var _firebaseTimestamp__nanoseconds;
|
|
1182
|
+
date = new Date(firebaseTimestamp._seconds * 1e3 + ((_firebaseTimestamp__nanoseconds = firebaseTimestamp._nanoseconds) !== null && _firebaseTimestamp__nanoseconds !== void 0 ? _firebaseTimestamp__nanoseconds : 0) / 1e6);
|
|
1177
1183
|
} else {
|
|
1178
|
-
|
|
1179
|
-
return
|
|
1184
|
+
console.error("Invalid timestamp format: ", firebaseTimestamp);
|
|
1185
|
+
return defaultReturnedValue;
|
|
1180
1186
|
}
|
|
1181
|
-
if (
|
|
1182
|
-
|
|
1183
|
-
return result;
|
|
1187
|
+
if (tz) {
|
|
1188
|
+
return (0, import_moment_timezone.default)(date).tz(tz).format(format);
|
|
1184
1189
|
}
|
|
1185
|
-
return import_moment_timezone.default.utc(date).format(
|
|
1190
|
+
return import_moment_timezone.default.utc(date).format(format);
|
|
1186
1191
|
}
|
|
1187
1192
|
// src/helpers/api.ts
|
|
1188
1193
|
var import_axios2 = __toESM(require("axios"));
|
|
@@ -935,32 +935,37 @@ function cn() {
|
|
|
935
935
|
import { Timestamp as Timestamp2 } from "firebase/firestore";
|
|
936
936
|
import moment2 from "moment-timezone";
|
|
937
937
|
function timestamp_to_string(firebaseTimestamp, options) {
|
|
938
|
+
var _options_defaultReturnedValue, _options_format, _options_fromFormat;
|
|
939
|
+
var _ref = {
|
|
940
|
+
defaultReturnedValue: (_options_defaultReturnedValue = options === null || options === void 0 ? void 0 : options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-",
|
|
941
|
+
format: (_options_format = options === null || options === void 0 ? void 0 : options.format) !== null && _options_format !== void 0 ? _options_format : "DD/MM/YYYY HH:mm:ss",
|
|
942
|
+
fromFormat: (_options_fromFormat = options === null || options === void 0 ? void 0 : options.fromFormat) !== null && _options_fromFormat !== void 0 ? _options_fromFormat : "DD/MM/YYYY HH:mm:ss",
|
|
943
|
+
tz: options === null || options === void 0 ? void 0 : options.tz
|
|
944
|
+
}, defaultReturnedValue = _ref.defaultReturnedValue, format = _ref.format, fromFormat = _ref.fromFormat, tz = _ref.tz;
|
|
938
945
|
if (!firebaseTimestamp) {
|
|
939
|
-
|
|
940
|
-
return (_options_defaultReturnedValue = options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-";
|
|
946
|
+
return defaultReturnedValue;
|
|
941
947
|
}
|
|
942
948
|
var date;
|
|
943
949
|
if (_instanceof(firebaseTimestamp, Timestamp2)) {
|
|
944
950
|
date = firebaseTimestamp.toDate();
|
|
945
951
|
} else if (_instanceof(firebaseTimestamp, Date)) {
|
|
946
952
|
date = firebaseTimestamp;
|
|
947
|
-
} else if (firebaseTimestamp._seconds && firebaseTimestamp._nanoseconds) {
|
|
948
|
-
date = new Date(firebaseTimestamp._seconds * 1e3 + firebaseTimestamp._nanoseconds / 1e6);
|
|
949
953
|
} else if (typeof firebaseTimestamp === "string") {
|
|
950
|
-
date = moment2.utc(firebaseTimestamp,
|
|
954
|
+
date = moment2.utc(firebaseTimestamp, fromFormat).toDate();
|
|
951
955
|
if (isNaN(date.getTime())) {
|
|
952
|
-
|
|
953
|
-
return (_options_defaultReturnedValue1 = options.defaultReturnedValue) !== null && _options_defaultReturnedValue1 !== void 0 ? _options_defaultReturnedValue1 : "-";
|
|
956
|
+
return defaultReturnedValue;
|
|
954
957
|
}
|
|
958
|
+
} else if (firebaseTimestamp._seconds) {
|
|
959
|
+
var _firebaseTimestamp__nanoseconds;
|
|
960
|
+
date = new Date(firebaseTimestamp._seconds * 1e3 + ((_firebaseTimestamp__nanoseconds = firebaseTimestamp._nanoseconds) !== null && _firebaseTimestamp__nanoseconds !== void 0 ? _firebaseTimestamp__nanoseconds : 0) / 1e6);
|
|
955
961
|
} else {
|
|
956
|
-
|
|
957
|
-
return
|
|
962
|
+
console.error("Invalid timestamp format: ", firebaseTimestamp);
|
|
963
|
+
return defaultReturnedValue;
|
|
958
964
|
}
|
|
959
|
-
if (
|
|
960
|
-
|
|
961
|
-
return result;
|
|
965
|
+
if (tz) {
|
|
966
|
+
return moment2(date).tz(tz).format(format);
|
|
962
967
|
}
|
|
963
|
-
return moment2.utc(date).format(
|
|
968
|
+
return moment2.utc(date).format(format);
|
|
964
969
|
}
|
|
965
970
|
// src/helpers/api.ts
|
|
966
971
|
import axios2 from "axios";
|
package/dist/helpers/index.js
CHANGED
|
@@ -2335,32 +2335,37 @@ function cn() {
|
|
|
2335
2335
|
var import_firestore2 = require("firebase/firestore");
|
|
2336
2336
|
var import_moment_timezone = __toESM(require("moment-timezone"));
|
|
2337
2337
|
function timestamp_to_string(firebaseTimestamp, options) {
|
|
2338
|
+
var _options_defaultReturnedValue, _options_format, _options_fromFormat;
|
|
2339
|
+
var _ref = {
|
|
2340
|
+
defaultReturnedValue: (_options_defaultReturnedValue = options === null || options === void 0 ? void 0 : options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-",
|
|
2341
|
+
format: (_options_format = options === null || options === void 0 ? void 0 : options.format) !== null && _options_format !== void 0 ? _options_format : "DD/MM/YYYY HH:mm:ss",
|
|
2342
|
+
fromFormat: (_options_fromFormat = options === null || options === void 0 ? void 0 : options.fromFormat) !== null && _options_fromFormat !== void 0 ? _options_fromFormat : "DD/MM/YYYY HH:mm:ss",
|
|
2343
|
+
tz: options === null || options === void 0 ? void 0 : options.tz
|
|
2344
|
+
}, defaultReturnedValue = _ref.defaultReturnedValue, format = _ref.format, fromFormat = _ref.fromFormat, tz = _ref.tz;
|
|
2338
2345
|
if (!firebaseTimestamp) {
|
|
2339
|
-
|
|
2340
|
-
return (_options_defaultReturnedValue = options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-";
|
|
2346
|
+
return defaultReturnedValue;
|
|
2341
2347
|
}
|
|
2342
2348
|
var date;
|
|
2343
2349
|
if (_instanceof(firebaseTimestamp, import_firestore2.Timestamp)) {
|
|
2344
2350
|
date = firebaseTimestamp.toDate();
|
|
2345
2351
|
} else if (_instanceof(firebaseTimestamp, Date)) {
|
|
2346
2352
|
date = firebaseTimestamp;
|
|
2347
|
-
} else if (firebaseTimestamp._seconds && firebaseTimestamp._nanoseconds) {
|
|
2348
|
-
date = new Date(firebaseTimestamp._seconds * 1e3 + firebaseTimestamp._nanoseconds / 1e6);
|
|
2349
2353
|
} else if (typeof firebaseTimestamp === "string") {
|
|
2350
|
-
date = import_moment_timezone.default.utc(firebaseTimestamp,
|
|
2354
|
+
date = import_moment_timezone.default.utc(firebaseTimestamp, fromFormat).toDate();
|
|
2351
2355
|
if (isNaN(date.getTime())) {
|
|
2352
|
-
|
|
2353
|
-
return (_options_defaultReturnedValue1 = options.defaultReturnedValue) !== null && _options_defaultReturnedValue1 !== void 0 ? _options_defaultReturnedValue1 : "-";
|
|
2356
|
+
return defaultReturnedValue;
|
|
2354
2357
|
}
|
|
2358
|
+
} else if (firebaseTimestamp._seconds) {
|
|
2359
|
+
var _firebaseTimestamp__nanoseconds;
|
|
2360
|
+
date = new Date(firebaseTimestamp._seconds * 1e3 + ((_firebaseTimestamp__nanoseconds = firebaseTimestamp._nanoseconds) !== null && _firebaseTimestamp__nanoseconds !== void 0 ? _firebaseTimestamp__nanoseconds : 0) / 1e6);
|
|
2355
2361
|
} else {
|
|
2356
|
-
|
|
2357
|
-
return
|
|
2362
|
+
console.error("Invalid timestamp format: ", firebaseTimestamp);
|
|
2363
|
+
return defaultReturnedValue;
|
|
2358
2364
|
}
|
|
2359
|
-
if (
|
|
2360
|
-
|
|
2361
|
-
return result;
|
|
2365
|
+
if (tz) {
|
|
2366
|
+
return (0, import_moment_timezone.default)(date).tz(tz).format(format);
|
|
2362
2367
|
}
|
|
2363
|
-
return import_moment_timezone.default.utc(date).format(
|
|
2368
|
+
return import_moment_timezone.default.utc(date).format(format);
|
|
2364
2369
|
}
|
|
2365
2370
|
function timestamp_to_millis(firebaseTimestamp) {
|
|
2366
2371
|
if (!firebaseTimestamp) {
|
package/dist/helpers/index.mjs
CHANGED
|
@@ -1956,32 +1956,37 @@ function cn() {
|
|
|
1956
1956
|
import { Timestamp as Timestamp2 } from "firebase/firestore";
|
|
1957
1957
|
import moment2 from "moment-timezone";
|
|
1958
1958
|
function timestamp_to_string(firebaseTimestamp, options) {
|
|
1959
|
+
var _options_defaultReturnedValue, _options_format, _options_fromFormat;
|
|
1960
|
+
var _ref = {
|
|
1961
|
+
defaultReturnedValue: (_options_defaultReturnedValue = options === null || options === void 0 ? void 0 : options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-",
|
|
1962
|
+
format: (_options_format = options === null || options === void 0 ? void 0 : options.format) !== null && _options_format !== void 0 ? _options_format : "DD/MM/YYYY HH:mm:ss",
|
|
1963
|
+
fromFormat: (_options_fromFormat = options === null || options === void 0 ? void 0 : options.fromFormat) !== null && _options_fromFormat !== void 0 ? _options_fromFormat : "DD/MM/YYYY HH:mm:ss",
|
|
1964
|
+
tz: options === null || options === void 0 ? void 0 : options.tz
|
|
1965
|
+
}, defaultReturnedValue = _ref.defaultReturnedValue, format = _ref.format, fromFormat = _ref.fromFormat, tz = _ref.tz;
|
|
1959
1966
|
if (!firebaseTimestamp) {
|
|
1960
|
-
|
|
1961
|
-
return (_options_defaultReturnedValue = options.defaultReturnedValue) !== null && _options_defaultReturnedValue !== void 0 ? _options_defaultReturnedValue : "-";
|
|
1967
|
+
return defaultReturnedValue;
|
|
1962
1968
|
}
|
|
1963
1969
|
var date;
|
|
1964
1970
|
if (_instanceof(firebaseTimestamp, Timestamp2)) {
|
|
1965
1971
|
date = firebaseTimestamp.toDate();
|
|
1966
1972
|
} else if (_instanceof(firebaseTimestamp, Date)) {
|
|
1967
1973
|
date = firebaseTimestamp;
|
|
1968
|
-
} else if (firebaseTimestamp._seconds && firebaseTimestamp._nanoseconds) {
|
|
1969
|
-
date = new Date(firebaseTimestamp._seconds * 1e3 + firebaseTimestamp._nanoseconds / 1e6);
|
|
1970
1974
|
} else if (typeof firebaseTimestamp === "string") {
|
|
1971
|
-
date = moment2.utc(firebaseTimestamp,
|
|
1975
|
+
date = moment2.utc(firebaseTimestamp, fromFormat).toDate();
|
|
1972
1976
|
if (isNaN(date.getTime())) {
|
|
1973
|
-
|
|
1974
|
-
return (_options_defaultReturnedValue1 = options.defaultReturnedValue) !== null && _options_defaultReturnedValue1 !== void 0 ? _options_defaultReturnedValue1 : "-";
|
|
1977
|
+
return defaultReturnedValue;
|
|
1975
1978
|
}
|
|
1979
|
+
} else if (firebaseTimestamp._seconds) {
|
|
1980
|
+
var _firebaseTimestamp__nanoseconds;
|
|
1981
|
+
date = new Date(firebaseTimestamp._seconds * 1e3 + ((_firebaseTimestamp__nanoseconds = firebaseTimestamp._nanoseconds) !== null && _firebaseTimestamp__nanoseconds !== void 0 ? _firebaseTimestamp__nanoseconds : 0) / 1e6);
|
|
1976
1982
|
} else {
|
|
1977
|
-
|
|
1978
|
-
return
|
|
1983
|
+
console.error("Invalid timestamp format: ", firebaseTimestamp);
|
|
1984
|
+
return defaultReturnedValue;
|
|
1979
1985
|
}
|
|
1980
|
-
if (
|
|
1981
|
-
|
|
1982
|
-
return result;
|
|
1986
|
+
if (tz) {
|
|
1987
|
+
return moment2(date).tz(tz).format(format);
|
|
1983
1988
|
}
|
|
1984
|
-
return moment2.utc(date).format(
|
|
1989
|
+
return moment2.utc(date).format(format);
|
|
1985
1990
|
}
|
|
1986
1991
|
function timestamp_to_millis(firebaseTimestamp) {
|
|
1987
1992
|
if (!firebaseTimestamp) {
|
package/dist/hooks/index.d.mts
CHANGED
|
@@ -9,11 +9,21 @@ declare function useSafeEffect(callback: () => void, dependencies: any[], error_
|
|
|
9
9
|
declare const useDeepCompareMemo: <T>(factory: () => T, dependencies: any[]) => T;
|
|
10
10
|
declare function useDeepCompareEffect(effect: EffectCallback, dependencies: any[]): void;
|
|
11
11
|
|
|
12
|
+
type WebWorkerFunction<TInput, TOutput> = (data: TInput) => TOutput | Promise<TOutput>;
|
|
12
13
|
interface UseWebWorkerOptions {
|
|
13
14
|
debug?: boolean;
|
|
14
15
|
name?: string;
|
|
15
16
|
onError?: (error: Error) => void;
|
|
17
|
+
warmStart?: boolean;
|
|
18
|
+
recreateOnError?: boolean;
|
|
16
19
|
}
|
|
20
|
+
type RunWorkerFunction<TInput, TOutput> = ((data: TInput, transfer?: Transferable[]) => Promise<TOutput>) & {
|
|
21
|
+
isSupported: () => boolean;
|
|
22
|
+
isUsingWorker: () => boolean;
|
|
23
|
+
pendingJobs: () => number;
|
|
24
|
+
lastError: () => Error | null;
|
|
25
|
+
};
|
|
26
|
+
declare const useWebWorker: <TInput = unknown, TOutput = unknown>(fn: WebWorkerFunction<TInput, TOutput>, options?: UseWebWorkerOptions) => RunWorkerFunction<TInput, TOutput>;
|
|
17
27
|
|
|
18
28
|
type OnSnapshotCallback = (documents: any[], config: OnSnapshotConfig) => void;
|
|
19
29
|
interface OnSnapshotParsers {
|
|
@@ -56,4 +66,4 @@ declare const useSocketSnapshots: (configs: OnSnapshotConfig[], label?: string,
|
|
|
56
66
|
socketConnected: boolean;
|
|
57
67
|
};
|
|
58
68
|
|
|
59
|
-
export { type UseWebWorkerOptions, useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots };
|
|
69
|
+
export { type RunWorkerFunction, type UseWebWorkerOptions, type WebWorkerFunction, useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots, useWebWorker };
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -9,11 +9,21 @@ declare function useSafeEffect(callback: () => void, dependencies: any[], error_
|
|
|
9
9
|
declare const useDeepCompareMemo: <T>(factory: () => T, dependencies: any[]) => T;
|
|
10
10
|
declare function useDeepCompareEffect(effect: EffectCallback, dependencies: any[]): void;
|
|
11
11
|
|
|
12
|
+
type WebWorkerFunction<TInput, TOutput> = (data: TInput) => TOutput | Promise<TOutput>;
|
|
12
13
|
interface UseWebWorkerOptions {
|
|
13
14
|
debug?: boolean;
|
|
14
15
|
name?: string;
|
|
15
16
|
onError?: (error: Error) => void;
|
|
17
|
+
warmStart?: boolean;
|
|
18
|
+
recreateOnError?: boolean;
|
|
16
19
|
}
|
|
20
|
+
type RunWorkerFunction<TInput, TOutput> = ((data: TInput, transfer?: Transferable[]) => Promise<TOutput>) & {
|
|
21
|
+
isSupported: () => boolean;
|
|
22
|
+
isUsingWorker: () => boolean;
|
|
23
|
+
pendingJobs: () => number;
|
|
24
|
+
lastError: () => Error | null;
|
|
25
|
+
};
|
|
26
|
+
declare const useWebWorker: <TInput = unknown, TOutput = unknown>(fn: WebWorkerFunction<TInput, TOutput>, options?: UseWebWorkerOptions) => RunWorkerFunction<TInput, TOutput>;
|
|
17
27
|
|
|
18
28
|
type OnSnapshotCallback = (documents: any[], config: OnSnapshotConfig) => void;
|
|
19
29
|
interface OnSnapshotParsers {
|
|
@@ -56,4 +66,4 @@ declare const useSocketSnapshots: (configs: OnSnapshotConfig[], label?: string,
|
|
|
56
66
|
socketConnected: boolean;
|
|
57
67
|
};
|
|
58
68
|
|
|
59
|
-
export { type UseWebWorkerOptions, useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots };
|
|
69
|
+
export { type RunWorkerFunction, type UseWebWorkerOptions, type WebWorkerFunction, useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots, useWebWorker };
|
package/dist/hooks/index.js
CHANGED
|
@@ -335,6 +335,9 @@ __export(index_exports, {
|
|
|
335
335
|
},
|
|
336
336
|
useSocketSnapshots: function() {
|
|
337
337
|
return useSocketSnapshots;
|
|
338
|
+
},
|
|
339
|
+
useWebWorker: function() {
|
|
340
|
+
return useWebWorker;
|
|
338
341
|
}
|
|
339
342
|
});
|
|
340
343
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -1036,6 +1039,37 @@ var useWebWorker = function(fn, options) {
|
|
|
1036
1039
|
if (!isSupportedRef.current) {
|
|
1037
1040
|
return function() {};
|
|
1038
1041
|
}
|
|
1042
|
+
if (options === null || options === void 0 ? void 0 : options.warmStart) {
|
|
1043
|
+
try {
|
|
1044
|
+
ensureWorker();
|
|
1045
|
+
} catch (error) {
|
|
1046
|
+
console.error("Error creating worker", error);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return function() {
|
|
1050
|
+
if (workerRef.current) {
|
|
1051
|
+
workerRef.current.terminate();
|
|
1052
|
+
workerRef.current = null;
|
|
1053
|
+
}
|
|
1054
|
+
if (workerUrlRef.current) {
|
|
1055
|
+
URL.revokeObjectURL(workerUrlRef.current);
|
|
1056
|
+
workerUrlRef.current = null;
|
|
1057
|
+
}
|
|
1058
|
+
pendingJobsRef.current.forEach(function(job) {
|
|
1059
|
+
job.reject(new Error("Worker terminated"));
|
|
1060
|
+
});
|
|
1061
|
+
pendingJobsRef.current.clear();
|
|
1062
|
+
isReadyRef.current = false;
|
|
1063
|
+
};
|
|
1064
|
+
}, [
|
|
1065
|
+
fn,
|
|
1066
|
+
options === null || options === void 0 ? void 0 : options.debug,
|
|
1067
|
+
options === null || options === void 0 ? void 0 : options.name,
|
|
1068
|
+
options === null || options === void 0 ? void 0 : options.onError
|
|
1069
|
+
]);
|
|
1070
|
+
var ensureWorker = (0, import_react4.useCallback)(function() {
|
|
1071
|
+
if (!isSupportedRef.current) return;
|
|
1072
|
+
if (workerRef.current) return;
|
|
1039
1073
|
var functionString = fn.toString();
|
|
1040
1074
|
var workerSource = [
|
|
1041
1075
|
"const userFn = (".concat(functionString, ");"),
|
|
@@ -1089,30 +1123,23 @@ var useWebWorker = function(fn, options) {
|
|
|
1089
1123
|
job.reject(err);
|
|
1090
1124
|
});
|
|
1091
1125
|
pendingJobsRef.current.clear();
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
URL.revokeObjectURL(workerUrlRef.current);
|
|
1098
|
-
}
|
|
1099
|
-
workerRef.current = worker;
|
|
1100
|
-
workerUrlRef.current = url;
|
|
1101
|
-
return function() {
|
|
1102
|
-
if (workerRef.current) {
|
|
1103
|
-
workerRef.current.terminate();
|
|
1126
|
+
if (options === null || options === void 0 ? void 0 : options.recreateOnError) {
|
|
1127
|
+
try {
|
|
1128
|
+
var _workerRef_current;
|
|
1129
|
+
(_workerRef_current = workerRef.current) === null || _workerRef_current === void 0 ? void 0 : _workerRef_current.terminate();
|
|
1130
|
+
} catch (_) {}
|
|
1104
1131
|
workerRef.current = null;
|
|
1132
|
+
if (workerUrlRef.current) {
|
|
1133
|
+
try {
|
|
1134
|
+
URL.revokeObjectURL(workerUrlRef.current);
|
|
1135
|
+
} catch (_) {}
|
|
1136
|
+
workerUrlRef.current = null;
|
|
1137
|
+
}
|
|
1138
|
+
isReadyRef.current = false;
|
|
1105
1139
|
}
|
|
1106
|
-
if (workerUrlRef.current) {
|
|
1107
|
-
URL.revokeObjectURL(workerUrlRef.current);
|
|
1108
|
-
workerUrlRef.current = null;
|
|
1109
|
-
}
|
|
1110
|
-
pendingJobsRef.current.forEach(function(job) {
|
|
1111
|
-
job.reject(new Error("Worker terminated"));
|
|
1112
|
-
});
|
|
1113
|
-
pendingJobsRef.current.clear();
|
|
1114
|
-
isReadyRef.current = false;
|
|
1115
1140
|
};
|
|
1141
|
+
workerRef.current = worker;
|
|
1142
|
+
workerUrlRef.current = url;
|
|
1116
1143
|
}, [
|
|
1117
1144
|
fn,
|
|
1118
1145
|
options === null || options === void 0 ? void 0 : options.debug,
|
|
@@ -1121,6 +1148,7 @@ var useWebWorker = function(fn, options) {
|
|
|
1121
1148
|
]);
|
|
1122
1149
|
var runWorkerBase = (0, import_react4.useCallback)(function(data, transfer) {
|
|
1123
1150
|
if (!workerRef.current) {
|
|
1151
|
+
ensureWorker();
|
|
1124
1152
|
if (options === null || options === void 0 ? void 0 : options.debug) {
|
|
1125
1153
|
console.warn("[WebWorker".concat((options === null || options === void 0 ? void 0 : options.name) ? ":" + options.name : "", "] fallback to main thread"));
|
|
1126
1154
|
}
|
|
@@ -1179,11 +1207,24 @@ var useWebWorker = function(fn, options) {
|
|
|
1179
1207
|
reject: reject
|
|
1180
1208
|
});
|
|
1181
1209
|
try {
|
|
1182
|
-
|
|
1210
|
+
var shouldTransfer = Array.isArray(transfer) ? transfer : [];
|
|
1211
|
+
if (!shouldTransfer.length) {
|
|
1212
|
+
var maybeArrayBuffer = _instanceof(data === null || data === void 0 ? void 0 : data.buffer, ArrayBuffer) ? data.buffer : void 0;
|
|
1213
|
+
if (maybeArrayBuffer) {
|
|
1214
|
+
workerRef.current.postMessage({
|
|
1215
|
+
id: id,
|
|
1216
|
+
payload: data
|
|
1217
|
+
}, [
|
|
1218
|
+
maybeArrayBuffer
|
|
1219
|
+
]);
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
if (shouldTransfer.length) {
|
|
1183
1224
|
workerRef.current.postMessage({
|
|
1184
1225
|
id: id,
|
|
1185
1226
|
payload: data
|
|
1186
|
-
},
|
|
1227
|
+
}, shouldTransfer);
|
|
1187
1228
|
} else {
|
|
1188
1229
|
workerRef.current.postMessage({
|
|
1189
1230
|
id: id,
|
|
@@ -1203,7 +1244,8 @@ var useWebWorker = function(fn, options) {
|
|
|
1203
1244
|
fn,
|
|
1204
1245
|
options === null || options === void 0 ? void 0 : options.debug,
|
|
1205
1246
|
options === null || options === void 0 ? void 0 : options.name,
|
|
1206
|
-
options === null || options === void 0 ? void 0 : options.onError
|
|
1247
|
+
options === null || options === void 0 ? void 0 : options.onError,
|
|
1248
|
+
ensureWorker
|
|
1207
1249
|
]);
|
|
1208
1250
|
var runWorker = runWorkerBase;
|
|
1209
1251
|
runWorker.isSupported = function() {
|
|
@@ -1218,18 +1260,89 @@ var useWebWorker = function(fn, options) {
|
|
|
1218
1260
|
runWorker.lastError = function() {
|
|
1219
1261
|
return lastErrorRef.current;
|
|
1220
1262
|
};
|
|
1263
|
+
runWorker.terminate = function() {
|
|
1264
|
+
if (workerRef.current) {
|
|
1265
|
+
try {
|
|
1266
|
+
workerRef.current.terminate();
|
|
1267
|
+
} catch (error) {
|
|
1268
|
+
console.error("Error terminating worker", error);
|
|
1269
|
+
}
|
|
1270
|
+
workerRef.current = null;
|
|
1271
|
+
}
|
|
1272
|
+
if (workerUrlRef.current) {
|
|
1273
|
+
try {
|
|
1274
|
+
URL.revokeObjectURL(workerUrlRef.current);
|
|
1275
|
+
} catch (error) {
|
|
1276
|
+
console.error("Error revoking worker URL", error);
|
|
1277
|
+
}
|
|
1278
|
+
workerUrlRef.current = null;
|
|
1279
|
+
}
|
|
1280
|
+
pendingJobsRef.current.forEach(function(job) {
|
|
1281
|
+
return job.reject(new Error("Worker terminated"));
|
|
1282
|
+
});
|
|
1283
|
+
pendingJobsRef.current.clear();
|
|
1284
|
+
isReadyRef.current = false;
|
|
1285
|
+
};
|
|
1221
1286
|
return runWorker;
|
|
1222
1287
|
};
|
|
1223
|
-
|
|
1224
|
-
// src/hooks/snapshots.ts
|
|
1288
|
+
// src/hooks/snapshots/index.ts
|
|
1225
1289
|
var import_react5 = require("react");
|
|
1226
|
-
// src/hooks/snapshotWorker.ts
|
|
1227
|
-
var wrapConfigsWithWorker = function(cfgs, runProcessor) {
|
|
1290
|
+
// src/hooks/snapshots/snapshotWorker.ts
|
|
1291
|
+
var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
|
|
1292
|
+
var recentEvents = /* @__PURE__ */ new Map();
|
|
1293
|
+
var _options_eventTtlMs;
|
|
1294
|
+
var EVENT_TTL_MS = (_options_eventTtlMs = options === null || options === void 0 ? void 0 : options.eventTtlMs) !== null && _options_eventTtlMs !== void 0 ? _options_eventTtlMs : 1500;
|
|
1295
|
+
var _options_maxRecentEvents;
|
|
1296
|
+
var MAX_RECENT = (_options_maxRecentEvents = options === null || options === void 0 ? void 0 : options.maxRecentEvents) !== null && _options_maxRecentEvents !== void 0 ? _options_maxRecentEvents : 500;
|
|
1297
|
+
var now = function() {
|
|
1298
|
+
return Date.now();
|
|
1299
|
+
};
|
|
1300
|
+
var makeKey = function(op, docs, collectionName) {
|
|
1301
|
+
try {
|
|
1302
|
+
var ids = (docs || []).map(function(d) {
|
|
1303
|
+
return d === null || d === void 0 ? void 0 : d.id;
|
|
1304
|
+
}).filter(Boolean).sort().join(",");
|
|
1305
|
+
return "".concat(collectionName || "unknown", ":").concat(op, ":").concat(ids);
|
|
1306
|
+
} catch (e) {
|
|
1307
|
+
return "".concat(collectionName || "unknown", ":").concat(op, ":*");
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
var shouldProcess = function(key) {
|
|
1311
|
+
var ts = recentEvents.get(key);
|
|
1312
|
+
if (ts && now() - ts < EVENT_TTL_MS) {
|
|
1313
|
+
return false;
|
|
1314
|
+
}
|
|
1315
|
+
if (recentEvents.size > MAX_RECENT) {
|
|
1316
|
+
var cutoff = now() - EVENT_TTL_MS;
|
|
1317
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
1318
|
+
try {
|
|
1319
|
+
for(var _iterator = Array.from(recentEvents.entries())[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
1320
|
+
var _step_value = _sliced_to_array(_step.value, 2), k = _step_value[0], v = _step_value[1];
|
|
1321
|
+
if (v < cutoff) recentEvents.delete(k);
|
|
1322
|
+
}
|
|
1323
|
+
} catch (err) {
|
|
1324
|
+
_didIteratorError = true;
|
|
1325
|
+
_iteratorError = err;
|
|
1326
|
+
} finally{
|
|
1327
|
+
try {
|
|
1328
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1329
|
+
_iterator.return();
|
|
1330
|
+
}
|
|
1331
|
+
} finally{
|
|
1332
|
+
if (_didIteratorError) {
|
|
1333
|
+
throw _iteratorError;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
recentEvents.set(key, now());
|
|
1339
|
+
return true;
|
|
1340
|
+
};
|
|
1228
1341
|
var wrapCb = function(cb, op, run) {
|
|
1229
1342
|
if (!cb) return void 0;
|
|
1230
1343
|
return /*#__PURE__*/ function() {
|
|
1231
1344
|
var _ref = _async_to_generator(function(docs, config) {
|
|
1232
|
-
var safeDocs, _ref, processed, e;
|
|
1345
|
+
var key, safeDocs, _ref, processed, e;
|
|
1233
1346
|
return _ts_generator(this, function(_state) {
|
|
1234
1347
|
switch(_state.label){
|
|
1235
1348
|
case 0:
|
|
@@ -1239,7 +1352,13 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor) {
|
|
|
1239
1352
|
,
|
|
1240
1353
|
3
|
|
1241
1354
|
]);
|
|
1242
|
-
|
|
1355
|
+
key = makeKey(op, docs, config === null || config === void 0 ? void 0 : config.collectionName);
|
|
1356
|
+
if (!shouldProcess(key)) {
|
|
1357
|
+
return [
|
|
1358
|
+
2
|
|
1359
|
+
];
|
|
1360
|
+
}
|
|
1361
|
+
safeDocs = (options === null || options === void 0 ? void 0 : options.jsonClone) === false ? docs : JSON.parse(JSON.stringify(docs));
|
|
1243
1362
|
return [
|
|
1244
1363
|
4,
|
|
1245
1364
|
run({
|
|
@@ -1292,16 +1411,17 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor) {
|
|
|
1292
1411
|
return wrapped;
|
|
1293
1412
|
});
|
|
1294
1413
|
};
|
|
1295
|
-
// src/hooks/snapshots.ts
|
|
1414
|
+
// src/hooks/snapshots/index.ts
|
|
1296
1415
|
var useDbSnapshots = function(configs, label, settings) {
|
|
1297
1416
|
var snapshotsFirstTime = (0, import_react5.useRef)([]);
|
|
1298
1417
|
var unsubscribeFunctions = (0, import_react5.useRef)([]);
|
|
1418
|
+
var lastDbCollectionsRef = (0, import_react5.useRef)([]);
|
|
1299
1419
|
var workerProcessorDb = (0, import_react5.useCallback)(function(payload) {
|
|
1300
1420
|
return {
|
|
1301
1421
|
docs: payload.docs
|
|
1302
1422
|
};
|
|
1303
1423
|
}, []);
|
|
1304
|
-
var runProcessor =
|
|
1424
|
+
var runProcessor = useWebWorker(workerProcessorDb, settings === null || settings === void 0 ? void 0 : settings.worker);
|
|
1305
1425
|
var wrapConfigsForWorker = function(cfgs) {
|
|
1306
1426
|
return wrapConfigsWithWorker(cfgs, function(payload) {
|
|
1307
1427
|
return runProcessor(payload);
|
|
@@ -1313,6 +1433,9 @@ var useDbSnapshots = function(configs, label, settings) {
|
|
|
1313
1433
|
console.log("==> ".concat(label || "DB snapshots", " started from db... "));
|
|
1314
1434
|
}
|
|
1315
1435
|
var wrappedConfigs = wrapConfigsForWorker(configs);
|
|
1436
|
+
lastDbCollectionsRef.current = configs.map(function(c) {
|
|
1437
|
+
return c.collectionName;
|
|
1438
|
+
});
|
|
1316
1439
|
var snapshotResults = wrappedConfigs.map(function(config) {
|
|
1317
1440
|
return snapshot(config, snapshotsFirstTime.current, settings);
|
|
1318
1441
|
});
|
|
@@ -1328,11 +1451,15 @@ var useDbSnapshots = function(configs, label, settings) {
|
|
|
1328
1451
|
});
|
|
1329
1452
|
if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
|
|
1330
1453
|
return function() {
|
|
1454
|
+
var _settings_worker;
|
|
1331
1455
|
unsubscribeFunctions.current.forEach(function(unsubscribe) {
|
|
1332
1456
|
if (unsubscribe) {
|
|
1333
1457
|
unsubscribe();
|
|
1334
1458
|
}
|
|
1335
1459
|
});
|
|
1460
|
+
if ((settings === null || settings === void 0 ? void 0 : (_settings_worker = settings.worker) === null || _settings_worker === void 0 ? void 0 : _settings_worker.debug) && lastDbCollectionsRef.current.length) {
|
|
1461
|
+
console.log("==> ".concat(label || "DB snapshots", " cleanup: cleaned previous subscriptions for [").concat(lastDbCollectionsRef.current.join(", "), "]"));
|
|
1462
|
+
}
|
|
1336
1463
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
|
|
1337
1464
|
console.log("==> ".concat(label || "DB snapshots", " unsubscribed from db"));
|
|
1338
1465
|
}
|
|
@@ -1345,11 +1472,15 @@ var useDbSnapshots = function(configs, label, settings) {
|
|
|
1345
1472
|
]);
|
|
1346
1473
|
(0, import_react5.useEffect)(function() {
|
|
1347
1474
|
return function() {
|
|
1475
|
+
var _settings_worker;
|
|
1348
1476
|
unsubscribeFunctions.current.forEach(function(unsubscribe) {
|
|
1349
1477
|
if (unsubscribe) {
|
|
1350
1478
|
unsubscribe();
|
|
1351
1479
|
}
|
|
1352
1480
|
});
|
|
1481
|
+
if ((settings === null || settings === void 0 ? void 0 : (_settings_worker = settings.worker) === null || _settings_worker === void 0 ? void 0 : _settings_worker.debug) && lastDbCollectionsRef.current.length) {
|
|
1482
|
+
console.log("==> ".concat(label || "DB snapshots", " cleanup: cleaned previous subscriptions for [").concat(lastDbCollectionsRef.current.join(", "), "]"));
|
|
1483
|
+
}
|
|
1353
1484
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1354
1485
|
console.log("==> ".concat(label || "DB snapshots", " unsubscribed"));
|
|
1355
1486
|
}
|
|
@@ -1383,6 +1514,10 @@ var useSmartSnapshots = function(configs, label, settings) {
|
|
|
1383
1514
|
configForDb.push(cfg);
|
|
1384
1515
|
}
|
|
1385
1516
|
});
|
|
1517
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1518
|
+
console.log("configForDb", configForDb);
|
|
1519
|
+
console.log("configForCache", configForCache);
|
|
1520
|
+
}
|
|
1386
1521
|
return {
|
|
1387
1522
|
configForDb: configForDb,
|
|
1388
1523
|
configForCache: configForCache
|
|
@@ -1403,40 +1538,68 @@ var useSocketSnapshots = function(configs, label, settings) {
|
|
|
1403
1538
|
var _ref = _sliced_to_array((0, import_react5.useState)(socketServiceInstance.isConnected()), 2), socketConnected = _ref[0], setSocketConnected = _ref[1];
|
|
1404
1539
|
var cleanupSubscriptionsRef = (0, import_react5.useRef)([]);
|
|
1405
1540
|
var socketStarted = (0, import_react5.useRef)(false);
|
|
1541
|
+
var activeSubscriptionKeyRef = (0, import_react5.useRef)(null);
|
|
1542
|
+
var activeCollectionsRef = (0, import_react5.useRef)(/* @__PURE__ */ new Set());
|
|
1543
|
+
var offConnectRef = (0, import_react5.useRef)(null);
|
|
1544
|
+
var offDisconnectRef = (0, import_react5.useRef)(null);
|
|
1406
1545
|
var workerProcessorSocket = (0, import_react5.useCallback)(function(payload) {
|
|
1407
1546
|
return {
|
|
1408
1547
|
docs: payload.docs
|
|
1409
1548
|
};
|
|
1410
1549
|
}, []);
|
|
1411
|
-
var runProcessor =
|
|
1550
|
+
var runProcessor = useWebWorker(workerProcessorSocket, settings === null || settings === void 0 ? void 0 : settings.worker);
|
|
1412
1551
|
var wrapConfigsForWorker = function(cfgs) {
|
|
1413
1552
|
return wrapConfigsWithWorker(cfgs, function(payload) {
|
|
1414
1553
|
return runProcessor(payload);
|
|
1415
1554
|
});
|
|
1416
1555
|
};
|
|
1417
1556
|
useDeepCompareEffect(function() {
|
|
1557
|
+
var _offConnectRef_current, _offDisconnectRef_current;
|
|
1418
1558
|
if (!auth.currentUser) {
|
|
1419
1559
|
return;
|
|
1420
1560
|
}
|
|
1421
|
-
var clearSubscriptions = function() {
|
|
1422
|
-
if (cleanupSubscriptionsRef.current.length) {
|
|
1423
|
-
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1424
|
-
return cleanup();
|
|
1425
|
-
});
|
|
1426
|
-
cleanupSubscriptionsRef.current = [];
|
|
1427
|
-
}
|
|
1428
|
-
};
|
|
1429
1561
|
var subscribe = function() {
|
|
1430
|
-
|
|
1562
|
+
var desiredNames = new Set(configs.map(function(c) {
|
|
1563
|
+
return c.collectionName;
|
|
1564
|
+
}));
|
|
1565
|
+
var key = JSON.stringify(Array.from(desiredNames).sort());
|
|
1566
|
+
if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
|
|
1567
|
+
if (activeSubscriptionKeyRef.current === key) return;
|
|
1568
|
+
if (cleanupSubscriptionsRef.current.length) {
|
|
1569
|
+
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1570
|
+
return cleanup();
|
|
1571
|
+
});
|
|
1572
|
+
cleanupSubscriptionsRef.current = [];
|
|
1573
|
+
}
|
|
1574
|
+
if (configs.length === 0) return;
|
|
1575
|
+
var disposer2 = socketServiceInstance.subscribeToCollections(wrapConfigsForWorker(configs));
|
|
1576
|
+
cleanupSubscriptionsRef.current.push(disposer2);
|
|
1577
|
+
activeCollectionsRef.current = new Set(desiredNames);
|
|
1578
|
+
activeSubscriptionKeyRef.current = key;
|
|
1579
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1580
|
+
console.log("==> ".concat(label || "Cache snapshots", " subscribed to ").concat(configs.map(function(c) {
|
|
1581
|
+
return c.collectionName;
|
|
1582
|
+
}).join(", ")));
|
|
1583
|
+
}
|
|
1431
1584
|
return;
|
|
1432
1585
|
}
|
|
1433
|
-
|
|
1434
|
-
|
|
1586
|
+
var toAdd = Array.from(desiredNames).filter(function(name) {
|
|
1587
|
+
return !activeCollectionsRef.current.has(name);
|
|
1588
|
+
});
|
|
1589
|
+
if (toAdd.length === 0) {
|
|
1590
|
+
return;
|
|
1591
|
+
}
|
|
1592
|
+
var configsToAdd = configs.filter(function(c) {
|
|
1593
|
+
return toAdd.includes(c.collectionName);
|
|
1594
|
+
});
|
|
1595
|
+
var disposer = socketServiceInstance.subscribeToCollections(wrapConfigsForWorker(configsToAdd));
|
|
1435
1596
|
cleanupSubscriptionsRef.current.push(disposer);
|
|
1597
|
+
toAdd.forEach(function(n) {
|
|
1598
|
+
return activeCollectionsRef.current.add(n);
|
|
1599
|
+
});
|
|
1600
|
+
activeSubscriptionKeyRef.current = JSON.stringify(Array.from(activeCollectionsRef.current).sort());
|
|
1436
1601
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1437
|
-
console.log("==> ".concat(label || "Cache snapshots", "
|
|
1438
|
-
return c.collectionName;
|
|
1439
|
-
}).join(", ")));
|
|
1602
|
+
console.log("==> ".concat(label || "Cache snapshots", " appended subscriptions for ").concat(toAdd.join(", ")));
|
|
1440
1603
|
}
|
|
1441
1604
|
};
|
|
1442
1605
|
if (socketServiceInstance.isConnected()) {
|
|
@@ -1449,35 +1612,64 @@ var useSocketSnapshots = function(configs, label, settings) {
|
|
|
1449
1612
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1450
1613
|
console.log("==> ".concat(label || "Cache snapshots", " started... "));
|
|
1451
1614
|
}
|
|
1615
|
+
}).catch(function(err) {
|
|
1616
|
+
socketStarted.current = false;
|
|
1617
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1618
|
+
console.error("==> ".concat(label || "Cache snapshots", " failed to acquire token:"), err);
|
|
1619
|
+
}
|
|
1452
1620
|
});
|
|
1453
1621
|
}
|
|
1454
|
-
|
|
1622
|
+
(_offConnectRef_current = offConnectRef.current) === null || _offConnectRef_current === void 0 ? void 0 : _offConnectRef_current.call(offConnectRef);
|
|
1623
|
+
(_offDisconnectRef_current = offDisconnectRef.current) === null || _offDisconnectRef_current === void 0 ? void 0 : _offDisconnectRef_current.call(offDisconnectRef);
|
|
1624
|
+
offConnectRef.current = socketServiceInstance.onConnect(function() {
|
|
1455
1625
|
setSocketConnected(true);
|
|
1456
1626
|
socketStarted.current = false;
|
|
1457
1627
|
subscribe();
|
|
1458
1628
|
});
|
|
1459
|
-
|
|
1629
|
+
offDisconnectRef.current = socketServiceInstance.onDisconnect(function() {
|
|
1460
1630
|
setSocketConnected(false);
|
|
1461
|
-
|
|
1631
|
+
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1632
|
+
return cleanup();
|
|
1633
|
+
});
|
|
1634
|
+
cleanupSubscriptionsRef.current = [];
|
|
1635
|
+
activeSubscriptionKeyRef.current = null;
|
|
1636
|
+
activeCollectionsRef.current = /* @__PURE__ */ new Set();
|
|
1462
1637
|
});
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1638
|
+
if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
|
|
1639
|
+
return function() {
|
|
1640
|
+
var _offConnectRef_current, _offDisconnectRef_current;
|
|
1641
|
+
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1642
|
+
return cleanup();
|
|
1643
|
+
});
|
|
1644
|
+
cleanupSubscriptionsRef.current = [];
|
|
1645
|
+
activeSubscriptionKeyRef.current = null;
|
|
1646
|
+
activeCollectionsRef.current = /* @__PURE__ */ new Set();
|
|
1647
|
+
(_offConnectRef_current = offConnectRef.current) === null || _offConnectRef_current === void 0 ? void 0 : _offConnectRef_current.call(offConnectRef);
|
|
1648
|
+
offConnectRef.current = null;
|
|
1649
|
+
(_offDisconnectRef_current = offDisconnectRef.current) === null || _offDisconnectRef_current === void 0 ? void 0 : _offDisconnectRef_current.call(offDisconnectRef);
|
|
1650
|
+
offDisconnectRef.current = null;
|
|
1651
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
|
|
1652
|
+
console.log("==> ".concat(label || "Cache snapshots", " unsubscribed. "));
|
|
1653
|
+
}
|
|
1654
|
+
};
|
|
1655
|
+
}
|
|
1471
1656
|
}, [
|
|
1472
1657
|
configs,
|
|
1473
1658
|
(_auth_currentUser = auth.currentUser) === null || _auth_currentUser === void 0 ? void 0 : _auth_currentUser.uid
|
|
1474
1659
|
]);
|
|
1475
1660
|
(0, import_react5.useEffect)(function() {
|
|
1476
1661
|
return function() {
|
|
1662
|
+
var _offConnectRef_current, _offDisconnectRef_current;
|
|
1477
1663
|
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1478
1664
|
return cleanup();
|
|
1479
1665
|
});
|
|
1480
1666
|
cleanupSubscriptionsRef.current = [];
|
|
1667
|
+
activeSubscriptionKeyRef.current = null;
|
|
1668
|
+
activeCollectionsRef.current = /* @__PURE__ */ new Set();
|
|
1669
|
+
(_offConnectRef_current = offConnectRef.current) === null || _offConnectRef_current === void 0 ? void 0 : _offConnectRef_current.call(offConnectRef);
|
|
1670
|
+
offConnectRef.current = null;
|
|
1671
|
+
(_offDisconnectRef_current = offDisconnectRef.current) === null || _offDisconnectRef_current === void 0 ? void 0 : _offDisconnectRef_current.call(offDisconnectRef);
|
|
1672
|
+
offDisconnectRef.current = null;
|
|
1481
1673
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
|
|
1482
1674
|
console.log("==> ".concat(label || "Cache snapshots", " unsubscribed. "));
|
|
1483
1675
|
}
|
|
@@ -1496,6 +1688,7 @@ var useSocketSnapshots = function(configs, label, settings) {
|
|
|
1496
1688
|
useSafeEffect: useSafeEffect,
|
|
1497
1689
|
useSetUserCountry: useSetUserCountry,
|
|
1498
1690
|
useSmartSnapshots: useSmartSnapshots,
|
|
1499
|
-
useSocketSnapshots: useSocketSnapshots
|
|
1691
|
+
useSocketSnapshots: useSocketSnapshots,
|
|
1692
|
+
useWebWorker: useWebWorker
|
|
1500
1693
|
});
|
|
1501
1694
|
//# sourceMappingURL=index.js.map
|
package/dist/hooks/index.mjs
CHANGED
|
@@ -942,6 +942,37 @@ var useWebWorker = function(fn, options) {
|
|
|
942
942
|
if (!isSupportedRef.current) {
|
|
943
943
|
return function() {};
|
|
944
944
|
}
|
|
945
|
+
if (options === null || options === void 0 ? void 0 : options.warmStart) {
|
|
946
|
+
try {
|
|
947
|
+
ensureWorker();
|
|
948
|
+
} catch (error) {
|
|
949
|
+
console.error("Error creating worker", error);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
return function() {
|
|
953
|
+
if (workerRef.current) {
|
|
954
|
+
workerRef.current.terminate();
|
|
955
|
+
workerRef.current = null;
|
|
956
|
+
}
|
|
957
|
+
if (workerUrlRef.current) {
|
|
958
|
+
URL.revokeObjectURL(workerUrlRef.current);
|
|
959
|
+
workerUrlRef.current = null;
|
|
960
|
+
}
|
|
961
|
+
pendingJobsRef.current.forEach(function(job) {
|
|
962
|
+
job.reject(new Error("Worker terminated"));
|
|
963
|
+
});
|
|
964
|
+
pendingJobsRef.current.clear();
|
|
965
|
+
isReadyRef.current = false;
|
|
966
|
+
};
|
|
967
|
+
}, [
|
|
968
|
+
fn,
|
|
969
|
+
options === null || options === void 0 ? void 0 : options.debug,
|
|
970
|
+
options === null || options === void 0 ? void 0 : options.name,
|
|
971
|
+
options === null || options === void 0 ? void 0 : options.onError
|
|
972
|
+
]);
|
|
973
|
+
var ensureWorker = useCallback2(function() {
|
|
974
|
+
if (!isSupportedRef.current) return;
|
|
975
|
+
if (workerRef.current) return;
|
|
945
976
|
var functionString = fn.toString();
|
|
946
977
|
var workerSource = [
|
|
947
978
|
"const userFn = (".concat(functionString, ");"),
|
|
@@ -995,30 +1026,23 @@ var useWebWorker = function(fn, options) {
|
|
|
995
1026
|
job.reject(err);
|
|
996
1027
|
});
|
|
997
1028
|
pendingJobsRef.current.clear();
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
URL.revokeObjectURL(workerUrlRef.current);
|
|
1004
|
-
}
|
|
1005
|
-
workerRef.current = worker;
|
|
1006
|
-
workerUrlRef.current = url;
|
|
1007
|
-
return function() {
|
|
1008
|
-
if (workerRef.current) {
|
|
1009
|
-
workerRef.current.terminate();
|
|
1029
|
+
if (options === null || options === void 0 ? void 0 : options.recreateOnError) {
|
|
1030
|
+
try {
|
|
1031
|
+
var _workerRef_current;
|
|
1032
|
+
(_workerRef_current = workerRef.current) === null || _workerRef_current === void 0 ? void 0 : _workerRef_current.terminate();
|
|
1033
|
+
} catch (_) {}
|
|
1010
1034
|
workerRef.current = null;
|
|
1035
|
+
if (workerUrlRef.current) {
|
|
1036
|
+
try {
|
|
1037
|
+
URL.revokeObjectURL(workerUrlRef.current);
|
|
1038
|
+
} catch (_) {}
|
|
1039
|
+
workerUrlRef.current = null;
|
|
1040
|
+
}
|
|
1041
|
+
isReadyRef.current = false;
|
|
1011
1042
|
}
|
|
1012
|
-
if (workerUrlRef.current) {
|
|
1013
|
-
URL.revokeObjectURL(workerUrlRef.current);
|
|
1014
|
-
workerUrlRef.current = null;
|
|
1015
|
-
}
|
|
1016
|
-
pendingJobsRef.current.forEach(function(job) {
|
|
1017
|
-
job.reject(new Error("Worker terminated"));
|
|
1018
|
-
});
|
|
1019
|
-
pendingJobsRef.current.clear();
|
|
1020
|
-
isReadyRef.current = false;
|
|
1021
1043
|
};
|
|
1044
|
+
workerRef.current = worker;
|
|
1045
|
+
workerUrlRef.current = url;
|
|
1022
1046
|
}, [
|
|
1023
1047
|
fn,
|
|
1024
1048
|
options === null || options === void 0 ? void 0 : options.debug,
|
|
@@ -1027,6 +1051,7 @@ var useWebWorker = function(fn, options) {
|
|
|
1027
1051
|
]);
|
|
1028
1052
|
var runWorkerBase = useCallback2(function(data, transfer) {
|
|
1029
1053
|
if (!workerRef.current) {
|
|
1054
|
+
ensureWorker();
|
|
1030
1055
|
if (options === null || options === void 0 ? void 0 : options.debug) {
|
|
1031
1056
|
console.warn("[WebWorker".concat((options === null || options === void 0 ? void 0 : options.name) ? ":" + options.name : "", "] fallback to main thread"));
|
|
1032
1057
|
}
|
|
@@ -1085,11 +1110,24 @@ var useWebWorker = function(fn, options) {
|
|
|
1085
1110
|
reject: reject
|
|
1086
1111
|
});
|
|
1087
1112
|
try {
|
|
1088
|
-
|
|
1113
|
+
var shouldTransfer = Array.isArray(transfer) ? transfer : [];
|
|
1114
|
+
if (!shouldTransfer.length) {
|
|
1115
|
+
var maybeArrayBuffer = _instanceof(data === null || data === void 0 ? void 0 : data.buffer, ArrayBuffer) ? data.buffer : void 0;
|
|
1116
|
+
if (maybeArrayBuffer) {
|
|
1117
|
+
workerRef.current.postMessage({
|
|
1118
|
+
id: id,
|
|
1119
|
+
payload: data
|
|
1120
|
+
}, [
|
|
1121
|
+
maybeArrayBuffer
|
|
1122
|
+
]);
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
if (shouldTransfer.length) {
|
|
1089
1127
|
workerRef.current.postMessage({
|
|
1090
1128
|
id: id,
|
|
1091
1129
|
payload: data
|
|
1092
|
-
},
|
|
1130
|
+
}, shouldTransfer);
|
|
1093
1131
|
} else {
|
|
1094
1132
|
workerRef.current.postMessage({
|
|
1095
1133
|
id: id,
|
|
@@ -1109,7 +1147,8 @@ var useWebWorker = function(fn, options) {
|
|
|
1109
1147
|
fn,
|
|
1110
1148
|
options === null || options === void 0 ? void 0 : options.debug,
|
|
1111
1149
|
options === null || options === void 0 ? void 0 : options.name,
|
|
1112
|
-
options === null || options === void 0 ? void 0 : options.onError
|
|
1150
|
+
options === null || options === void 0 ? void 0 : options.onError,
|
|
1151
|
+
ensureWorker
|
|
1113
1152
|
]);
|
|
1114
1153
|
var runWorker = runWorkerBase;
|
|
1115
1154
|
runWorker.isSupported = function() {
|
|
@@ -1124,18 +1163,89 @@ var useWebWorker = function(fn, options) {
|
|
|
1124
1163
|
runWorker.lastError = function() {
|
|
1125
1164
|
return lastErrorRef.current;
|
|
1126
1165
|
};
|
|
1166
|
+
runWorker.terminate = function() {
|
|
1167
|
+
if (workerRef.current) {
|
|
1168
|
+
try {
|
|
1169
|
+
workerRef.current.terminate();
|
|
1170
|
+
} catch (error) {
|
|
1171
|
+
console.error("Error terminating worker", error);
|
|
1172
|
+
}
|
|
1173
|
+
workerRef.current = null;
|
|
1174
|
+
}
|
|
1175
|
+
if (workerUrlRef.current) {
|
|
1176
|
+
try {
|
|
1177
|
+
URL.revokeObjectURL(workerUrlRef.current);
|
|
1178
|
+
} catch (error) {
|
|
1179
|
+
console.error("Error revoking worker URL", error);
|
|
1180
|
+
}
|
|
1181
|
+
workerUrlRef.current = null;
|
|
1182
|
+
}
|
|
1183
|
+
pendingJobsRef.current.forEach(function(job) {
|
|
1184
|
+
return job.reject(new Error("Worker terminated"));
|
|
1185
|
+
});
|
|
1186
|
+
pendingJobsRef.current.clear();
|
|
1187
|
+
isReadyRef.current = false;
|
|
1188
|
+
};
|
|
1127
1189
|
return runWorker;
|
|
1128
1190
|
};
|
|
1129
|
-
|
|
1130
|
-
// src/hooks/snapshots.ts
|
|
1191
|
+
// src/hooks/snapshots/index.ts
|
|
1131
1192
|
import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo3, useRef as useRef4, useState as useState2 } from "react";
|
|
1132
|
-
// src/hooks/snapshotWorker.ts
|
|
1133
|
-
var wrapConfigsWithWorker = function(cfgs, runProcessor) {
|
|
1193
|
+
// src/hooks/snapshots/snapshotWorker.ts
|
|
1194
|
+
var wrapConfigsWithWorker = function(cfgs, runProcessor, options) {
|
|
1195
|
+
var recentEvents = /* @__PURE__ */ new Map();
|
|
1196
|
+
var _options_eventTtlMs;
|
|
1197
|
+
var EVENT_TTL_MS = (_options_eventTtlMs = options === null || options === void 0 ? void 0 : options.eventTtlMs) !== null && _options_eventTtlMs !== void 0 ? _options_eventTtlMs : 1500;
|
|
1198
|
+
var _options_maxRecentEvents;
|
|
1199
|
+
var MAX_RECENT = (_options_maxRecentEvents = options === null || options === void 0 ? void 0 : options.maxRecentEvents) !== null && _options_maxRecentEvents !== void 0 ? _options_maxRecentEvents : 500;
|
|
1200
|
+
var now = function() {
|
|
1201
|
+
return Date.now();
|
|
1202
|
+
};
|
|
1203
|
+
var makeKey = function(op, docs, collectionName) {
|
|
1204
|
+
try {
|
|
1205
|
+
var ids = (docs || []).map(function(d) {
|
|
1206
|
+
return d === null || d === void 0 ? void 0 : d.id;
|
|
1207
|
+
}).filter(Boolean).sort().join(",");
|
|
1208
|
+
return "".concat(collectionName || "unknown", ":").concat(op, ":").concat(ids);
|
|
1209
|
+
} catch (e) {
|
|
1210
|
+
return "".concat(collectionName || "unknown", ":").concat(op, ":*");
|
|
1211
|
+
}
|
|
1212
|
+
};
|
|
1213
|
+
var shouldProcess = function(key) {
|
|
1214
|
+
var ts = recentEvents.get(key);
|
|
1215
|
+
if (ts && now() - ts < EVENT_TTL_MS) {
|
|
1216
|
+
return false;
|
|
1217
|
+
}
|
|
1218
|
+
if (recentEvents.size > MAX_RECENT) {
|
|
1219
|
+
var cutoff = now() - EVENT_TTL_MS;
|
|
1220
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
1221
|
+
try {
|
|
1222
|
+
for(var _iterator = Array.from(recentEvents.entries())[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
1223
|
+
var _step_value = _sliced_to_array(_step.value, 2), k = _step_value[0], v = _step_value[1];
|
|
1224
|
+
if (v < cutoff) recentEvents.delete(k);
|
|
1225
|
+
}
|
|
1226
|
+
} catch (err) {
|
|
1227
|
+
_didIteratorError = true;
|
|
1228
|
+
_iteratorError = err;
|
|
1229
|
+
} finally{
|
|
1230
|
+
try {
|
|
1231
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1232
|
+
_iterator.return();
|
|
1233
|
+
}
|
|
1234
|
+
} finally{
|
|
1235
|
+
if (_didIteratorError) {
|
|
1236
|
+
throw _iteratorError;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
recentEvents.set(key, now());
|
|
1242
|
+
return true;
|
|
1243
|
+
};
|
|
1134
1244
|
var wrapCb = function(cb, op, run) {
|
|
1135
1245
|
if (!cb) return void 0;
|
|
1136
1246
|
return /*#__PURE__*/ function() {
|
|
1137
1247
|
var _ref = _async_to_generator(function(docs, config) {
|
|
1138
|
-
var safeDocs, _ref, processed, e;
|
|
1248
|
+
var key, safeDocs, _ref, processed, e;
|
|
1139
1249
|
return _ts_generator(this, function(_state) {
|
|
1140
1250
|
switch(_state.label){
|
|
1141
1251
|
case 0:
|
|
@@ -1145,7 +1255,13 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor) {
|
|
|
1145
1255
|
,
|
|
1146
1256
|
3
|
|
1147
1257
|
]);
|
|
1148
|
-
|
|
1258
|
+
key = makeKey(op, docs, config === null || config === void 0 ? void 0 : config.collectionName);
|
|
1259
|
+
if (!shouldProcess(key)) {
|
|
1260
|
+
return [
|
|
1261
|
+
2
|
|
1262
|
+
];
|
|
1263
|
+
}
|
|
1264
|
+
safeDocs = (options === null || options === void 0 ? void 0 : options.jsonClone) === false ? docs : JSON.parse(JSON.stringify(docs));
|
|
1149
1265
|
return [
|
|
1150
1266
|
4,
|
|
1151
1267
|
run({
|
|
@@ -1198,16 +1314,17 @@ var wrapConfigsWithWorker = function(cfgs, runProcessor) {
|
|
|
1198
1314
|
return wrapped;
|
|
1199
1315
|
});
|
|
1200
1316
|
};
|
|
1201
|
-
// src/hooks/snapshots.ts
|
|
1317
|
+
// src/hooks/snapshots/index.ts
|
|
1202
1318
|
var useDbSnapshots = function(configs, label, settings) {
|
|
1203
1319
|
var snapshotsFirstTime = useRef4([]);
|
|
1204
1320
|
var unsubscribeFunctions = useRef4([]);
|
|
1321
|
+
var lastDbCollectionsRef = useRef4([]);
|
|
1205
1322
|
var workerProcessorDb = useCallback3(function(payload) {
|
|
1206
1323
|
return {
|
|
1207
1324
|
docs: payload.docs
|
|
1208
1325
|
};
|
|
1209
1326
|
}, []);
|
|
1210
|
-
var runProcessor =
|
|
1327
|
+
var runProcessor = useWebWorker(workerProcessorDb, settings === null || settings === void 0 ? void 0 : settings.worker);
|
|
1211
1328
|
var wrapConfigsForWorker = function(cfgs) {
|
|
1212
1329
|
return wrapConfigsWithWorker(cfgs, function(payload) {
|
|
1213
1330
|
return runProcessor(payload);
|
|
@@ -1219,6 +1336,9 @@ var useDbSnapshots = function(configs, label, settings) {
|
|
|
1219
1336
|
console.log("==> ".concat(label || "DB snapshots", " started from db... "));
|
|
1220
1337
|
}
|
|
1221
1338
|
var wrappedConfigs = wrapConfigsForWorker(configs);
|
|
1339
|
+
lastDbCollectionsRef.current = configs.map(function(c) {
|
|
1340
|
+
return c.collectionName;
|
|
1341
|
+
});
|
|
1222
1342
|
var snapshotResults = wrappedConfigs.map(function(config) {
|
|
1223
1343
|
return snapshot(config, snapshotsFirstTime.current, settings);
|
|
1224
1344
|
});
|
|
@@ -1234,11 +1354,15 @@ var useDbSnapshots = function(configs, label, settings) {
|
|
|
1234
1354
|
});
|
|
1235
1355
|
if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
|
|
1236
1356
|
return function() {
|
|
1357
|
+
var _settings_worker;
|
|
1237
1358
|
unsubscribeFunctions.current.forEach(function(unsubscribe) {
|
|
1238
1359
|
if (unsubscribe) {
|
|
1239
1360
|
unsubscribe();
|
|
1240
1361
|
}
|
|
1241
1362
|
});
|
|
1363
|
+
if ((settings === null || settings === void 0 ? void 0 : (_settings_worker = settings.worker) === null || _settings_worker === void 0 ? void 0 : _settings_worker.debug) && lastDbCollectionsRef.current.length) {
|
|
1364
|
+
console.log("==> ".concat(label || "DB snapshots", " cleanup: cleaned previous subscriptions for [").concat(lastDbCollectionsRef.current.join(", "), "]"));
|
|
1365
|
+
}
|
|
1242
1366
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
|
|
1243
1367
|
console.log("==> ".concat(label || "DB snapshots", " unsubscribed from db"));
|
|
1244
1368
|
}
|
|
@@ -1251,11 +1375,15 @@ var useDbSnapshots = function(configs, label, settings) {
|
|
|
1251
1375
|
]);
|
|
1252
1376
|
useEffect4(function() {
|
|
1253
1377
|
return function() {
|
|
1378
|
+
var _settings_worker;
|
|
1254
1379
|
unsubscribeFunctions.current.forEach(function(unsubscribe) {
|
|
1255
1380
|
if (unsubscribe) {
|
|
1256
1381
|
unsubscribe();
|
|
1257
1382
|
}
|
|
1258
1383
|
});
|
|
1384
|
+
if ((settings === null || settings === void 0 ? void 0 : (_settings_worker = settings.worker) === null || _settings_worker === void 0 ? void 0 : _settings_worker.debug) && lastDbCollectionsRef.current.length) {
|
|
1385
|
+
console.log("==> ".concat(label || "DB snapshots", " cleanup: cleaned previous subscriptions for [").concat(lastDbCollectionsRef.current.join(", "), "]"));
|
|
1386
|
+
}
|
|
1259
1387
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1260
1388
|
console.log("==> ".concat(label || "DB snapshots", " unsubscribed"));
|
|
1261
1389
|
}
|
|
@@ -1289,6 +1417,10 @@ var useSmartSnapshots = function(configs, label, settings) {
|
|
|
1289
1417
|
configForDb.push(cfg);
|
|
1290
1418
|
}
|
|
1291
1419
|
});
|
|
1420
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1421
|
+
console.log("configForDb", configForDb);
|
|
1422
|
+
console.log("configForCache", configForCache);
|
|
1423
|
+
}
|
|
1292
1424
|
return {
|
|
1293
1425
|
configForDb: configForDb,
|
|
1294
1426
|
configForCache: configForCache
|
|
@@ -1309,40 +1441,68 @@ var useSocketSnapshots = function(configs, label, settings) {
|
|
|
1309
1441
|
var _useState2 = _sliced_to_array(useState2(socketServiceInstance.isConnected()), 2), socketConnected = _useState2[0], setSocketConnected = _useState2[1];
|
|
1310
1442
|
var cleanupSubscriptionsRef = useRef4([]);
|
|
1311
1443
|
var socketStarted = useRef4(false);
|
|
1444
|
+
var activeSubscriptionKeyRef = useRef4(null);
|
|
1445
|
+
var activeCollectionsRef = useRef4(/* @__PURE__ */ new Set());
|
|
1446
|
+
var offConnectRef = useRef4(null);
|
|
1447
|
+
var offDisconnectRef = useRef4(null);
|
|
1312
1448
|
var workerProcessorSocket = useCallback3(function(payload) {
|
|
1313
1449
|
return {
|
|
1314
1450
|
docs: payload.docs
|
|
1315
1451
|
};
|
|
1316
1452
|
}, []);
|
|
1317
|
-
var runProcessor =
|
|
1453
|
+
var runProcessor = useWebWorker(workerProcessorSocket, settings === null || settings === void 0 ? void 0 : settings.worker);
|
|
1318
1454
|
var wrapConfigsForWorker = function(cfgs) {
|
|
1319
1455
|
return wrapConfigsWithWorker(cfgs, function(payload) {
|
|
1320
1456
|
return runProcessor(payload);
|
|
1321
1457
|
});
|
|
1322
1458
|
};
|
|
1323
1459
|
useDeepCompareEffect(function() {
|
|
1460
|
+
var _offConnectRef_current, _offDisconnectRef_current;
|
|
1324
1461
|
if (!auth.currentUser) {
|
|
1325
1462
|
return;
|
|
1326
1463
|
}
|
|
1327
|
-
var clearSubscriptions = function() {
|
|
1328
|
-
if (cleanupSubscriptionsRef.current.length) {
|
|
1329
|
-
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1330
|
-
return cleanup();
|
|
1331
|
-
});
|
|
1332
|
-
cleanupSubscriptionsRef.current = [];
|
|
1333
|
-
}
|
|
1334
|
-
};
|
|
1335
1464
|
var subscribe = function() {
|
|
1336
|
-
|
|
1465
|
+
var desiredNames = new Set(configs.map(function(c) {
|
|
1466
|
+
return c.collectionName;
|
|
1467
|
+
}));
|
|
1468
|
+
var key = JSON.stringify(Array.from(desiredNames).sort());
|
|
1469
|
+
if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
|
|
1470
|
+
if (activeSubscriptionKeyRef.current === key) return;
|
|
1471
|
+
if (cleanupSubscriptionsRef.current.length) {
|
|
1472
|
+
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1473
|
+
return cleanup();
|
|
1474
|
+
});
|
|
1475
|
+
cleanupSubscriptionsRef.current = [];
|
|
1476
|
+
}
|
|
1477
|
+
if (configs.length === 0) return;
|
|
1478
|
+
var disposer2 = socketServiceInstance.subscribeToCollections(wrapConfigsForWorker(configs));
|
|
1479
|
+
cleanupSubscriptionsRef.current.push(disposer2);
|
|
1480
|
+
activeCollectionsRef.current = new Set(desiredNames);
|
|
1481
|
+
activeSubscriptionKeyRef.current = key;
|
|
1482
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1483
|
+
console.log("==> ".concat(label || "Cache snapshots", " subscribed to ").concat(configs.map(function(c) {
|
|
1484
|
+
return c.collectionName;
|
|
1485
|
+
}).join(", ")));
|
|
1486
|
+
}
|
|
1487
|
+
return;
|
|
1488
|
+
}
|
|
1489
|
+
var toAdd = Array.from(desiredNames).filter(function(name) {
|
|
1490
|
+
return !activeCollectionsRef.current.has(name);
|
|
1491
|
+
});
|
|
1492
|
+
if (toAdd.length === 0) {
|
|
1337
1493
|
return;
|
|
1338
1494
|
}
|
|
1339
|
-
|
|
1340
|
-
|
|
1495
|
+
var configsToAdd = configs.filter(function(c) {
|
|
1496
|
+
return toAdd.includes(c.collectionName);
|
|
1497
|
+
});
|
|
1498
|
+
var disposer = socketServiceInstance.subscribeToCollections(wrapConfigsForWorker(configsToAdd));
|
|
1341
1499
|
cleanupSubscriptionsRef.current.push(disposer);
|
|
1500
|
+
toAdd.forEach(function(n) {
|
|
1501
|
+
return activeCollectionsRef.current.add(n);
|
|
1502
|
+
});
|
|
1503
|
+
activeSubscriptionKeyRef.current = JSON.stringify(Array.from(activeCollectionsRef.current).sort());
|
|
1342
1504
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1343
|
-
console.log("==> ".concat(label || "Cache snapshots", "
|
|
1344
|
-
return c.collectionName;
|
|
1345
|
-
}).join(", ")));
|
|
1505
|
+
console.log("==> ".concat(label || "Cache snapshots", " appended subscriptions for ").concat(toAdd.join(", ")));
|
|
1346
1506
|
}
|
|
1347
1507
|
};
|
|
1348
1508
|
if (socketServiceInstance.isConnected()) {
|
|
@@ -1355,35 +1515,64 @@ var useSocketSnapshots = function(configs, label, settings) {
|
|
|
1355
1515
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1356
1516
|
console.log("==> ".concat(label || "Cache snapshots", " started... "));
|
|
1357
1517
|
}
|
|
1518
|
+
}).catch(function(err) {
|
|
1519
|
+
socketStarted.current = false;
|
|
1520
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
|
|
1521
|
+
console.error("==> ".concat(label || "Cache snapshots", " failed to acquire token:"), err);
|
|
1522
|
+
}
|
|
1358
1523
|
});
|
|
1359
1524
|
}
|
|
1360
|
-
|
|
1525
|
+
(_offConnectRef_current = offConnectRef.current) === null || _offConnectRef_current === void 0 ? void 0 : _offConnectRef_current.call(offConnectRef);
|
|
1526
|
+
(_offDisconnectRef_current = offDisconnectRef.current) === null || _offDisconnectRef_current === void 0 ? void 0 : _offDisconnectRef_current.call(offDisconnectRef);
|
|
1527
|
+
offConnectRef.current = socketServiceInstance.onConnect(function() {
|
|
1361
1528
|
setSocketConnected(true);
|
|
1362
1529
|
socketStarted.current = false;
|
|
1363
1530
|
subscribe();
|
|
1364
1531
|
});
|
|
1365
|
-
|
|
1532
|
+
offDisconnectRef.current = socketServiceInstance.onDisconnect(function() {
|
|
1366
1533
|
setSocketConnected(false);
|
|
1367
|
-
|
|
1534
|
+
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1535
|
+
return cleanup();
|
|
1536
|
+
});
|
|
1537
|
+
cleanupSubscriptionsRef.current = [];
|
|
1538
|
+
activeSubscriptionKeyRef.current = null;
|
|
1539
|
+
activeCollectionsRef.current = /* @__PURE__ */ new Set();
|
|
1368
1540
|
});
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1541
|
+
if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
|
|
1542
|
+
return function() {
|
|
1543
|
+
var _offConnectRef_current, _offDisconnectRef_current;
|
|
1544
|
+
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1545
|
+
return cleanup();
|
|
1546
|
+
});
|
|
1547
|
+
cleanupSubscriptionsRef.current = [];
|
|
1548
|
+
activeSubscriptionKeyRef.current = null;
|
|
1549
|
+
activeCollectionsRef.current = /* @__PURE__ */ new Set();
|
|
1550
|
+
(_offConnectRef_current = offConnectRef.current) === null || _offConnectRef_current === void 0 ? void 0 : _offConnectRef_current.call(offConnectRef);
|
|
1551
|
+
offConnectRef.current = null;
|
|
1552
|
+
(_offDisconnectRef_current = offDisconnectRef.current) === null || _offDisconnectRef_current === void 0 ? void 0 : _offDisconnectRef_current.call(offDisconnectRef);
|
|
1553
|
+
offDisconnectRef.current = null;
|
|
1554
|
+
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
|
|
1555
|
+
console.log("==> ".concat(label || "Cache snapshots", " unsubscribed. "));
|
|
1556
|
+
}
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1377
1559
|
}, [
|
|
1378
1560
|
configs,
|
|
1379
1561
|
(_auth_currentUser = auth.currentUser) === null || _auth_currentUser === void 0 ? void 0 : _auth_currentUser.uid
|
|
1380
1562
|
]);
|
|
1381
1563
|
useEffect4(function() {
|
|
1382
1564
|
return function() {
|
|
1565
|
+
var _offConnectRef_current, _offDisconnectRef_current;
|
|
1383
1566
|
cleanupSubscriptionsRef.current.forEach(function(cleanup) {
|
|
1384
1567
|
return cleanup();
|
|
1385
1568
|
});
|
|
1386
1569
|
cleanupSubscriptionsRef.current = [];
|
|
1570
|
+
activeSubscriptionKeyRef.current = null;
|
|
1571
|
+
activeCollectionsRef.current = /* @__PURE__ */ new Set();
|
|
1572
|
+
(_offConnectRef_current = offConnectRef.current) === null || _offConnectRef_current === void 0 ? void 0 : _offConnectRef_current.call(offConnectRef);
|
|
1573
|
+
offConnectRef.current = null;
|
|
1574
|
+
(_offDisconnectRef_current = offDisconnectRef.current) === null || _offDisconnectRef_current === void 0 ? void 0 : _offDisconnectRef_current.call(offDisconnectRef);
|
|
1575
|
+
offDisconnectRef.current = null;
|
|
1387
1576
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
|
|
1388
1577
|
console.log("==> ".concat(label || "Cache snapshots", " unsubscribed. "));
|
|
1389
1578
|
}
|
|
@@ -1393,5 +1582,5 @@ var useSocketSnapshots = function(configs, label, settings) {
|
|
|
1393
1582
|
socketConnected: socketConnected
|
|
1394
1583
|
};
|
|
1395
1584
|
};
|
|
1396
|
-
export { useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots };
|
|
1585
|
+
export { useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots, useWebWorker };
|
|
1397
1586
|
//# sourceMappingURL=index.mjs.map
|