akeyless-client-commons 1.1.21 → 1.1.23

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.
@@ -1,4 +1,15 @@
1
1
  // src/hooks/global.ts
2
+ function _array_like_to_array(arr, len) {
3
+ if (len == null || len > arr.length) len = arr.length;
4
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
+ return arr2;
6
+ }
7
+ function _array_with_holes(arr) {
8
+ if (Array.isArray(arr)) return arr;
9
+ }
10
+ function _array_without_holes(arr) {
11
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
12
+ }
2
13
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
3
14
  try {
4
15
  var info = gen[key](arg);
@@ -28,6 +39,25 @@ function _async_to_generator(fn) {
28
39
  });
29
40
  };
30
41
  }
42
+ function _class_call_check(instance, Constructor) {
43
+ if (!(instance instanceof Constructor)) {
44
+ throw new TypeError("Cannot call a class as a function");
45
+ }
46
+ }
47
+ function _defineProperties(target, props) {
48
+ for(var i = 0; i < props.length; i++){
49
+ var descriptor = props[i];
50
+ descriptor.enumerable = descriptor.enumerable || false;
51
+ descriptor.configurable = true;
52
+ if ("value" in descriptor) descriptor.writable = true;
53
+ Object.defineProperty(target, descriptor.key, descriptor);
54
+ }
55
+ }
56
+ function _create_class(Constructor, protoProps, staticProps) {
57
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
58
+ if (staticProps) _defineProperties(Constructor, staticProps);
59
+ return Constructor;
60
+ }
31
61
  function _define_property(obj, key, value) {
32
62
  if (key in obj) {
33
63
  Object.defineProperty(obj, key, {
@@ -41,6 +71,39 @@ function _define_property(obj, key, value) {
41
71
  }
42
72
  return obj;
43
73
  }
74
+ function _iterable_to_array(iter) {
75
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
76
+ }
77
+ function _iterable_to_array_limit(arr, i) {
78
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
79
+ if (_i == null) return;
80
+ var _arr = [];
81
+ var _n = true;
82
+ var _d = false;
83
+ var _s, _e;
84
+ try {
85
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
86
+ _arr.push(_s.value);
87
+ if (i && _arr.length === i) break;
88
+ }
89
+ } catch (err) {
90
+ _d = true;
91
+ _e = err;
92
+ } finally{
93
+ try {
94
+ if (!_n && _i["return"] != null) _i["return"]();
95
+ } finally{
96
+ if (_d) throw _e;
97
+ }
98
+ }
99
+ return _arr;
100
+ }
101
+ function _non_iterable_rest() {
102
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
103
+ }
104
+ function _non_iterable_spread() {
105
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
106
+ }
44
107
  function _object_spread(target) {
45
108
  for(var i = 1; i < arguments.length; i++){
46
109
  var source = arguments[i] != null ? arguments[i] : {};
@@ -80,6 +143,20 @@ function _object_spread_props(target, source) {
80
143
  }
81
144
  return target;
82
145
  }
146
+ function _sliced_to_array(arr, i) {
147
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
148
+ }
149
+ function _to_consumable_array(arr) {
150
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
151
+ }
152
+ function _unsupported_iterable_to_array(o, minLen) {
153
+ if (!o) return;
154
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
155
+ var n = Object.prototype.toString.call(o).slice(8, -1);
156
+ if (n === "Object" && o.constructor) n = o.constructor.name;
157
+ if (n === "Map" || n === "Set") return Array.from(n);
158
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
159
+ }
83
160
  function _ts_generator(thisArg, body) {
84
161
  var f, y, t, g, _ = {
85
162
  label: 0,
@@ -176,7 +253,7 @@ function _ts_generator(thisArg, body) {
176
253
  }
177
254
  }
178
255
  import { CountryOptions as CountryOptions2 } from "akeyless-types-commons";
179
- import { useEffect as useEffect2, useLayoutEffect, useRef as useRef2 } from "react";
256
+ import { useEffect, useLayoutEffect } from "react";
180
257
  // src/helpers/firebase.ts
181
258
  import moment from "moment";
182
259
  import { initializeApp } from "firebase/app";
@@ -280,6 +357,50 @@ var simpleExtractData = function(doc2) {
280
357
  id: doc2.id
281
358
  });
282
359
  };
360
+ var get_document_by_id = /*#__PURE__*/ function() {
361
+ var _ref = _async_to_generator(function(collection_path, doc_id) {
362
+ var doc_ref, doc_snap, error;
363
+ return _ts_generator(this, function(_state) {
364
+ switch(_state.label){
365
+ case 0:
366
+ _state.trys.push([
367
+ 0,
368
+ 2,
369
+ ,
370
+ 3
371
+ ]);
372
+ doc_ref = doc(db, collection_path, doc_id);
373
+ return [
374
+ 4,
375
+ getDoc(doc_ref)
376
+ ];
377
+ case 1:
378
+ doc_snap = _state.sent();
379
+ if (!doc_snap.exists()) {
380
+ throw new Error("Document not found, document id: " + doc_id);
381
+ }
382
+ return [
383
+ 2,
384
+ simpleExtractData(doc_snap)
385
+ ];
386
+ case 2:
387
+ error = _state.sent();
388
+ console.error("Error from get_document_by_id", error);
389
+ return [
390
+ 2,
391
+ null
392
+ ];
393
+ case 3:
394
+ return [
395
+ 2
396
+ ];
397
+ }
398
+ });
399
+ });
400
+ return function get_document_by_id(collection_path, doc_id) {
401
+ return _ref.apply(this, arguments);
402
+ };
403
+ }();
283
404
  var snapshot = function(config, snapshotsFirstTime, settings) {
284
405
  var resolvePromise;
285
406
  var isResolved = false;
@@ -305,11 +426,11 @@ var snapshot = function(config, snapshotsFirstTime, settings) {
305
426
  collectionRef = query(collectionRef, orderBy(order.fieldName, order.direction));
306
427
  });
307
428
  }
308
- var unsubscribe = onSnapshot(collectionRef, function(snapshot2) {
429
+ var unsubscribe = onSnapshot(collectionRef, function(snapshot3) {
309
430
  if (!snapshotsFirstTime.includes(config.collectionName)) {
310
431
  var _config_onFirstTime, _config_extraParsers;
311
432
  snapshotsFirstTime.push(config.collectionName);
312
- var documents = snapshot2.docs.map(function(doc2) {
433
+ var documents = snapshot3.docs.map(function(doc2) {
313
434
  return simpleExtractData(doc2);
314
435
  });
315
436
  (_config_onFirstTime = config.onFirstTime) === null || _config_onFirstTime === void 0 ? void 0 : _config_onFirstTime.call(config, documents, config);
@@ -323,7 +444,7 @@ var snapshot = function(config, snapshotsFirstTime, settings) {
323
444
  var addedDocs = [];
324
445
  var modifiedDocs = [];
325
446
  var removedDocs = [];
326
- snapshot2.docChanges().forEach(function(change) {
447
+ snapshot3.docChanges().forEach(function(change) {
327
448
  if (change.type === "added") {
328
449
  addedDocs.push(simpleExtractData(change.doc));
329
450
  } else if (change.type === "modified") {
@@ -426,11 +547,363 @@ var biDomain = isLocal ? "http://localhost:9002/api/bi" : baseDomain + "/bi";
426
547
  var notificationsDomain = isLocal ? "http://localhost:9006/api/notifications" : baseDomain + "/notifications";
427
548
  var callCenterGeoDomain = isLocal ? "http://localhost:9007/api/call-center/geo" : baseDomain + "/call-center/geo";
428
549
  var callCenterEventsDomain = isLocal ? "http://localhost:9008/api/call-center/events" : baseDomain + "/call-center/events";
550
+ var dataSocketDomain = isLocal ? "http://localhost:9009/api/data-socket" : baseDomain + "/data-socket";
551
+ var dataSyncDomain = isLocal ? "http://localhost:9010/api/data-sync" : baseDomain + "/data-sync";
552
+ // src/helpers/emails.ts
553
+ import axios3 from "axios";
554
+ // src/helpers/socket.ts
555
+ import { io } from "socket.io-client";
556
+ var SESSION_STORAGE_KEY = "sessionId";
557
+ var SocketService = /*#__PURE__*/ function() {
558
+ "use strict";
559
+ function _SocketService() {
560
+ _class_call_check(this, _SocketService);
561
+ this.socket = null;
562
+ this.connectCallbacks = [];
563
+ this.disconnectCallbacks = [];
564
+ this.authToken = null;
565
+ }
566
+ _create_class(_SocketService, [
567
+ {
568
+ /// Initialize the socket connection
569
+ key: "initSocket",
570
+ value: function initSocket() {
571
+ var _this = this;
572
+ if (!this.socket) {
573
+ var socketUrl = isLocal ? "http://localhost:9009" : mode === "qa" ? "https://nx-api.xyz" : "https://nx-api.info";
574
+ this.socket = io(socketUrl, {
575
+ path: "/api/data-socket/connect",
576
+ auth: function(cb) {
577
+ var sessionId = localStorage.getItem(SESSION_STORAGE_KEY) || void 0;
578
+ var token = _this.authToken;
579
+ var authPayload = {};
580
+ if (token) authPayload.token = token;
581
+ if (sessionId) authPayload.sessionId = sessionId;
582
+ cb(authPayload);
583
+ },
584
+ transports: [
585
+ "websocket"
586
+ ],
587
+ reconnection: true,
588
+ reconnectionAttempts: 30,
589
+ reconnectionDelay: 2 * 1e3
590
+ });
591
+ this.socket.on("connect", function() {
592
+ var _this_socket, _this_socket1;
593
+ console.log("\uD83D\uDFE2 Socket connected: ".concat((_this_socket = _this.socket) === null || _this_socket === void 0 ? void 0 : _this_socket.id, " (recovered - ").concat((_this_socket1 = _this.socket) === null || _this_socket1 === void 0 ? void 0 : _this_socket1.recovered, ")"));
594
+ _this.connectCallbacks.forEach(function(cb) {
595
+ return cb();
596
+ });
597
+ });
598
+ this.socket.on("disconnect", function(reason) {
599
+ console.log("Socket disconnected:", reason);
600
+ _this.disconnectCallbacks.forEach(function(cb) {
601
+ return cb();
602
+ });
603
+ });
604
+ this.socket.on("session", function(param) {
605
+ var session_id = param.session_id;
606
+ if (session_id) {
607
+ localStorage.setItem(SESSION_STORAGE_KEY, session_id);
608
+ }
609
+ });
610
+ this.socket.on("connect_error", function(error) {
611
+ console.error("Socket connection error:", error);
612
+ });
613
+ }
614
+ }
615
+ },
616
+ {
617
+ /// get socket instance
618
+ key: "getSocketInstance",
619
+ value: function getSocketInstance() {
620
+ if (!this.socket) {
621
+ this.initSocket();
622
+ }
623
+ if (!this.socket) {
624
+ throw new Error("Socket not initialized");
625
+ }
626
+ if (!this.socket.connected) {
627
+ this.socket.connect();
628
+ }
629
+ return this.socket;
630
+ }
631
+ },
632
+ {
633
+ /// connection management methods
634
+ key: "startSession",
635
+ value: function startSession(token) {
636
+ this.setAuthToken(token);
637
+ this.initSocket();
638
+ }
639
+ },
640
+ {
641
+ key: "onConnect",
642
+ value: function onConnect(callback) {
643
+ var _this = this;
644
+ var _this_socket;
645
+ if (!this.connectCallbacks.includes(callback)) {
646
+ this.connectCallbacks.push(callback);
647
+ }
648
+ if ((_this_socket = this.socket) === null || _this_socket === void 0 ? void 0 : _this_socket.connected) {
649
+ callback();
650
+ }
651
+ return function() {
652
+ return _this.offConnect(callback);
653
+ };
654
+ }
655
+ },
656
+ {
657
+ key: "offConnect",
658
+ value: function offConnect(callback) {
659
+ this.connectCallbacks = this.connectCallbacks.filter(function(cb) {
660
+ return cb !== callback;
661
+ });
662
+ }
663
+ },
664
+ {
665
+ key: "onDisconnect",
666
+ value: function onDisconnect(callback) {
667
+ var _this = this;
668
+ if (!this.disconnectCallbacks.includes(callback)) {
669
+ this.disconnectCallbacks.push(callback);
670
+ }
671
+ if (this.socket && !this.socket.connected) {
672
+ callback();
673
+ }
674
+ return function() {
675
+ return _this.offDisconnect(callback);
676
+ };
677
+ }
678
+ },
679
+ {
680
+ key: "offDisconnect",
681
+ value: function offDisconnect(callback) {
682
+ this.disconnectCallbacks = this.disconnectCallbacks.filter(function(cb) {
683
+ return cb !== callback;
684
+ });
685
+ }
686
+ },
687
+ {
688
+ key: "isConnected",
689
+ value: function isConnected() {
690
+ var _this_socket;
691
+ return ((_this_socket = this.socket) === null || _this_socket === void 0 ? void 0 : _this_socket.connected) || false;
692
+ }
693
+ },
694
+ {
695
+ key: "setAuthToken",
696
+ value: function setAuthToken(token) {
697
+ this.authToken = token;
698
+ if (this.socket) {
699
+ this.socket.connect();
700
+ }
701
+ }
702
+ },
703
+ {
704
+ key: "disconnectSocket",
705
+ value: function disconnectSocket() {
706
+ if (this.socket) {
707
+ this.socket.io.engine.close();
708
+ }
709
+ }
710
+ },
711
+ {
712
+ /// subscribe to collections
713
+ key: "subscribeToCollections",
714
+ value: function subscribeToCollections(config) {
715
+ var _this = this;
716
+ if (config.length === 0) {
717
+ return function() {};
718
+ }
719
+ var s = this.getSocketInstance();
720
+ var collectionsNames = config.map(function(c) {
721
+ return c.collectionName;
722
+ });
723
+ var eventHandlers = [];
724
+ config.forEach(function(configuration) {
725
+ var collectionName = configuration.collectionName, onAdd = configuration.onAdd, onFirstTime = configuration.onFirstTime, onModify = configuration.onModify, onRemove = configuration.onRemove, extraParsers = configuration.extraParsers, conditions = configuration.conditions, orderBy2 = configuration.orderBy;
726
+ var attach = function(eventName, handler) {
727
+ _this.socket.off(eventName, handler);
728
+ _this.socket.on(eventName, handler);
729
+ eventHandlers.push({
730
+ eventName: eventName,
731
+ handler: handler
732
+ });
733
+ };
734
+ attach("initial:".concat(collectionName), onFirstTime);
735
+ attach("add:".concat(collectionName), onAdd);
736
+ attach("update:".concat(collectionName), onModify);
737
+ attach("delete:".concat(collectionName), onRemove);
738
+ extraParsers === null || extraParsers === void 0 ? void 0 : extraParsers.forEach(function(parsers) {
739
+ var extraOnAdd = parsers.onAdd, extraOnFirstTime = parsers.onFirstTime, extraOnModify = parsers.onModify, extraOnRemove = parsers.onRemove;
740
+ attach("initial:".concat(collectionName), extraOnFirstTime);
741
+ attach("add:".concat(collectionName), extraOnAdd);
742
+ attach("update:".concat(collectionName), extraOnModify);
743
+ attach("delete:".concat(collectionName), extraOnRemove);
744
+ });
745
+ });
746
+ s.emit("subscribe_collections", collectionsNames, function(callback) {
747
+ if (callback.success) {
748
+ console.log("Successfully subscribed to: ".concat(collectionsNames.join(", ")));
749
+ } else {
750
+ console.error("Failed to subscribe to ".concat(config.join(", "), ": ").concat(callback.message));
751
+ }
752
+ });
753
+ return function() {
754
+ console.log("Cleaning up subscriptions for: ".concat(collectionsNames.join(", ")));
755
+ s.emit("unsubscribe_collections", collectionsNames);
756
+ eventHandlers.forEach(function(eh) {
757
+ s.off(eh.eventName, eh.handler);
758
+ });
759
+ };
760
+ }
761
+ },
762
+ {
763
+ /// set data
764
+ key: "setData",
765
+ value: function setData(payload) {
766
+ var s = this.getSocketInstance();
767
+ return new Promise(function(resolve, reject) {
768
+ s.emit("set_data", payload, function(callback) {
769
+ if (callback.success) {
770
+ console.log("Data saved successfully:", payload);
771
+ console.log("ack", callback);
772
+ resolve(callback);
773
+ } else {
774
+ reject(new Error(callback.message || "Save operation failed"));
775
+ }
776
+ });
777
+ });
778
+ }
779
+ },
780
+ {
781
+ /// get data
782
+ key: "getCollectionData",
783
+ value: function getCollectionData(payload) {
784
+ var s = this.getSocketInstance();
785
+ s.emit("get_data", {
786
+ collection_name: payload.collection_name
787
+ }, function(socketCallback) {
788
+ if (socketCallback.success && socketCallback.data) {
789
+ payload.callback(socketCallback.data);
790
+ } else {
791
+ payload.callback(payload.defaultValue);
792
+ }
793
+ });
794
+ }
795
+ },
796
+ {
797
+ key: "getDocumentData",
798
+ value: function getDocumentData(payload) {
799
+ var s = this.getSocketInstance();
800
+ s.emit("get_data", {
801
+ collection_name: payload.collection_name,
802
+ key: payload.key
803
+ }, function(socketCallback) {
804
+ if (socketCallback.success && socketCallback.data) {
805
+ payload.callback(socketCallback.data);
806
+ } else {
807
+ payload.callback(payload.defaultValue);
808
+ }
809
+ });
810
+ }
811
+ },
812
+ {
813
+ /// delete data
814
+ key: "deleteData",
815
+ value: function deleteData(payload) {
816
+ var s = this.getSocketInstance();
817
+ return new Promise(function(resolve, reject) {
818
+ s.emit("delete_data", payload, function(callback) {
819
+ if (callback.success) {
820
+ console.log("Data deleted successfully:", payload);
821
+ console.log("delete ack", callback);
822
+ resolve(callback);
823
+ } else {
824
+ reject(new Error(callback.message || "Delete operation failed"));
825
+ }
826
+ });
827
+ });
828
+ }
829
+ },
830
+ {
831
+ key: "clearAllRedisData",
832
+ value: function clearAllRedisData() {
833
+ var s = this.getSocketInstance();
834
+ return new Promise(function(resolve, reject) {
835
+ s.emit("clear_all_redis_data", function(ack) {
836
+ if (ack.success) {
837
+ resolve(ack);
838
+ } else {
839
+ reject(new Error(ack.message || "Clear all Redis data operation failed"));
840
+ }
841
+ });
842
+ });
843
+ }
844
+ }
845
+ ], [
846
+ {
847
+ key: "getInstance",
848
+ value: function getInstance() {
849
+ if (!_SocketService.instance) {
850
+ _SocketService.instance = new _SocketService();
851
+ }
852
+ return _SocketService.instance;
853
+ }
854
+ }
855
+ ]);
856
+ return _SocketService;
857
+ }();
858
+ var socketServiceInstance = SocketService.getInstance();
859
+ // src/hooks/global.ts
860
+ var useDocumentTitle = function(title) {
861
+ useEffect(function() {
862
+ document.title = title;
863
+ }, [
864
+ title
865
+ ]);
866
+ return null;
867
+ };
868
+ var useSetUserCountry = function(setUserCountry, changLang) {
869
+ useLayoutEffect(function() {
870
+ var currentCountry = localStorage.getItem("userCountry");
871
+ if (!currentCountry) {
872
+ var updateCountry = /*#__PURE__*/ function() {
873
+ var _ref = _async_to_generator(function() {
874
+ var country;
875
+ return _ts_generator(this, function(_state) {
876
+ switch(_state.label){
877
+ case 0:
878
+ return [
879
+ 4,
880
+ getUserCountryByIp()
881
+ ];
882
+ case 1:
883
+ country = _state.sent();
884
+ changLang(country === CountryOptions2.IL ? "he" : "en");
885
+ setUserCountry(country);
886
+ localStorage.setItem("userCountry", country);
887
+ return [
888
+ 2
889
+ ];
890
+ }
891
+ });
892
+ });
893
+ return function updateCountry() {
894
+ return _ref.apply(this, arguments);
895
+ };
896
+ }();
897
+ updateCountry();
898
+ }
899
+ }, []);
900
+ return null;
901
+ };
429
902
  // src/hooks/react.ts
430
903
  import { isEqual as isEqual2 } from "lodash";
431
- import { useEffect, useMemo, useRef } from "react";
904
+ import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2 } from "react";
432
905
  function useSafeEffect(callback, dependencies, error_message) {
433
- useEffect(function() {
906
+ useEffect2(function() {
434
907
  try {
435
908
  callback();
436
909
  } catch (error) {
@@ -439,37 +912,32 @@ function useSafeEffect(callback, dependencies, error_message) {
439
912
  }, dependencies);
440
913
  }
441
914
  var useDeepCompareMemo = function(factory, dependencies) {
442
- var previousDepsRef = useRef([]);
915
+ var previousDepsRef = useRef2([]);
443
916
  if (!isEqual2(dependencies, previousDepsRef.current)) {
444
917
  previousDepsRef.current = dependencies;
445
918
  }
446
- return useMemo(factory, previousDepsRef.current);
919
+ return useMemo2(factory, previousDepsRef.current);
447
920
  };
448
921
  function useDeepCompareEffect(effect, dependencies) {
449
- var previousDepsRef = useRef();
922
+ var previousDepsRef = useRef2();
450
923
  if (!isEqual2(previousDepsRef.current, dependencies)) {
451
924
  previousDepsRef.current = dependencies;
452
925
  }
453
- useEffect(effect, [
926
+ useEffect2(effect, [
454
927
  previousDepsRef.current
455
928
  ]);
456
929
  }
457
- // src/hooks/global.ts
458
- var useDocumentTitle = function(title) {
459
- useEffect2(function() {
460
- document.title = title;
461
- }, [
462
- title
463
- ]);
464
- return null;
465
- };
466
- var useSnapshotBulk = function(configs, label, settings) {
467
- var snapshotsFirstTime = useRef2([]);
468
- var unsubscribeFunctions = useRef2([]);
930
+ // src/hooks/WebWorker.ts
931
+ import { useCallback as useCallback2, useEffect as useEffect3, useRef as useRef3 } from "react";
932
+ // src/hooks/snapshots.ts
933
+ import { useEffect as useEffect4, useMemo as useMemo3, useRef as useRef4, useState as useState2 } from "react";
934
+ var useDbSnapshots = function(configs, label, settings) {
935
+ var snapshotsFirstTime = useRef4([]);
936
+ var unsubscribeFunctions = useRef4([]);
469
937
  useDeepCompareEffect(function() {
470
938
  var start = performance.now();
471
- if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
472
- console.log("==> ".concat(label || "Custom snapshots", " started... "));
939
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
940
+ console.log("==> ".concat(label || "DB snapshots", " started from db... "));
473
941
  }
474
942
  var snapshotResults = configs.map(function(config) {
475
943
  return snapshot(config, snapshotsFirstTime.current, settings);
@@ -480,8 +948,8 @@ var useSnapshotBulk = function(configs, label, settings) {
480
948
  Promise.all(snapshotResults.map(function(result) {
481
949
  return result.promise;
482
950
  })).then(function() {
483
- if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
484
- console.log("==> ".concat(label || "Custom snapshots", " ended. It took ").concat((performance.now() - start).toFixed(2), " ms"));
951
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
952
+ console.log("==> ".concat(label || "DB snapshots", " ended from db. It took ").concat((performance.now() - start).toFixed(2), " ms"));
485
953
  }
486
954
  });
487
955
  if (settings === null || settings === void 0 ? void 0 : settings.cleanupForConfigChange) {
@@ -491,8 +959,8 @@ var useSnapshotBulk = function(configs, label, settings) {
491
959
  unsubscribe();
492
960
  }
493
961
  });
494
- if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
495
- console.log("==> ".concat(label || "Custom snapshots", " unsubscribed"));
962
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
963
+ console.log("==> ".concat(label || "DB snapshots", " unsubscribed from db"));
496
964
  }
497
965
  };
498
966
  }
@@ -501,7 +969,7 @@ var useSnapshotBulk = function(configs, label, settings) {
501
969
  label,
502
970
  settings
503
971
  ]);
504
- useEffect2(function() {
972
+ useEffect4(function() {
505
973
  return function() {
506
974
  unsubscribeFunctions.current.forEach(function(unsubscribe) {
507
975
  if (unsubscribe) {
@@ -509,46 +977,125 @@ var useSnapshotBulk = function(configs, label, settings) {
509
977
  }
510
978
  });
511
979
  if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
512
- console.log("==> ".concat(label || "Custom snapshots", " unsubscribed"));
980
+ console.log("==> ".concat(label || "DB snapshots", " unsubscribed"));
513
981
  }
514
982
  };
515
983
  }, []);
516
984
  };
517
- var useSetUserCountry = function(setUserCountry, changLang) {
518
- useLayoutEffect(function() {
519
- var currentCountry = localStorage.getItem("userCountry");
520
- if (!currentCountry) {
521
- var updateCountry = /*#__PURE__*/ function() {
522
- var _ref = _async_to_generator(function() {
523
- var country;
524
- return _ts_generator(this, function(_state) {
525
- switch(_state.label){
526
- case 0:
527
- return [
528
- 4,
529
- getUserCountryByIp()
530
- ];
531
- case 1:
532
- country = _state.sent();
533
- changLang(country === CountryOptions2.IL ? "he" : "en");
534
- setUserCountry(country);
535
- localStorage.setItem("userCountry", country);
536
- return [
537
- 2
538
- ];
539
- }
540
- });
541
- });
542
- return function updateCountry() {
543
- return _ref.apply(this, arguments);
544
- };
545
- }();
546
- updateCountry();
985
+ var useSmartSnapshots = function(configs, label, settings) {
986
+ var _useState2 = _sliced_to_array(useState2(null), 2), cacheCollectionsConfig = _useState2[0], setCacheCollectionsConfig = _useState2[1];
987
+ useEffect4(function() {
988
+ get_document_by_id("nx-settings", "cache_collections_config").then(function(res) {
989
+ return setCacheCollectionsConfig(res);
990
+ });
991
+ return function() {
992
+ return setCacheCollectionsConfig(null);
993
+ };
994
+ }, []);
995
+ var groupedConfig = useMemo3(function() {
996
+ if (!cacheCollectionsConfig) {
997
+ return {
998
+ configForDb: [],
999
+ configForCache: []
1000
+ };
1001
+ }
1002
+ var configForDb = [];
1003
+ var configForCache = [];
1004
+ configs.forEach(function(cfg) {
1005
+ var collectionName = cfg.collectionName, _cfg_subscribeTo = cfg.subscribeTo, subscribeTo = _cfg_subscribeTo === void 0 ? "cache" : _cfg_subscribeTo;
1006
+ if (subscribeTo === "cache" && cacheCollectionsConfig[collectionName]) {
1007
+ configForCache.push(cfg);
1008
+ } else {
1009
+ configForDb.push(cfg);
1010
+ }
1011
+ });
1012
+ return {
1013
+ configForDb: configForDb,
1014
+ configForCache: configForCache
1015
+ };
1016
+ }, [
1017
+ configs,
1018
+ cacheCollectionsConfig
1019
+ ]);
1020
+ useDbSnapshots(groupedConfig.configForDb, label, settings);
1021
+ var socketConnected = useSocketSnapshots(groupedConfig.configForCache, label, settings).socketConnected;
1022
+ return {
1023
+ groupedConfig: groupedConfig,
1024
+ socketConnected: socketConnected
1025
+ };
1026
+ };
1027
+ var useSocketSnapshots = function(configs, label, settings) {
1028
+ var _auth_currentUser;
1029
+ var _useState2 = _sliced_to_array(useState2(socketServiceInstance.isConnected()), 2), socketConnected = _useState2[0], setSocketConnected = _useState2[1];
1030
+ var _useState21 = _sliced_to_array(useState2([]), 2), cleanupSubscriptions = _useState21[0], setCleanupSubscriptions = _useState21[1];
1031
+ var socketStarted = useRef4(false);
1032
+ useDeepCompareEffect(function() {
1033
+ if (!auth.currentUser) {
1034
+ return;
547
1035
  }
1036
+ var subscribe = function() {
1037
+ if (configs.length === 0) {
1038
+ return;
1039
+ }
1040
+ var disposer = socketServiceInstance.subscribeToCollections(configs);
1041
+ setCleanupSubscriptions(function(prev) {
1042
+ return _to_consumable_array(prev).concat([
1043
+ disposer
1044
+ ]);
1045
+ });
1046
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
1047
+ console.log("==> ".concat(label || "Cache snapshots", " subscribed to ").concat(configs.map(function(c) {
1048
+ return c.collectionName;
1049
+ }).join(", ")));
1050
+ }
1051
+ };
1052
+ if (socketServiceInstance.isConnected()) {
1053
+ setSocketConnected(true);
1054
+ subscribe();
1055
+ } else if (!socketStarted.current) {
1056
+ socketStarted.current = true;
1057
+ auth.currentUser.getIdToken().then(function(token) {
1058
+ socketServiceInstance.startSession(token);
1059
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs)) {
1060
+ console.log("==> ".concat(label || "Cache snapshots", " started... "));
1061
+ }
1062
+ });
1063
+ }
1064
+ var offConnect = socketServiceInstance.onConnect(function() {
1065
+ setSocketConnected(true);
1066
+ socketStarted.current = false;
1067
+ subscribe();
1068
+ });
1069
+ var offDisconnect = socketServiceInstance.onDisconnect(function() {
1070
+ setSocketConnected(false);
1071
+ cleanupSubscriptions.forEach(function(cleanup) {
1072
+ return cleanup();
1073
+ });
1074
+ });
1075
+ return function() {
1076
+ cleanupSubscriptions.forEach(function(cleanup) {
1077
+ return cleanup();
1078
+ });
1079
+ offConnect === null || offConnect === void 0 ? void 0 : offConnect();
1080
+ offDisconnect === null || offDisconnect === void 0 ? void 0 : offDisconnect();
1081
+ if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogs) && configs.length > 0) {
1082
+ console.log("==> ".concat(label || "Cache snapshots", " unsubscribed. "));
1083
+ }
1084
+ };
1085
+ }, [
1086
+ configs,
1087
+ (_auth_currentUser = auth.currentUser) === null || _auth_currentUser === void 0 ? void 0 : _auth_currentUser.uid
1088
+ ]);
1089
+ useEffect4(function() {
1090
+ return function() {
1091
+ cleanupSubscriptions.forEach(function(cleanup) {
1092
+ return cleanup();
1093
+ });
1094
+ };
548
1095
  }, []);
549
- return null;
1096
+ return {
1097
+ socketConnected: socketConnected
1098
+ };
550
1099
  };
551
- // src/hooks/WebWorker.ts
552
- import { useCallback as useCallback2, useEffect as useEffect3, useRef as useRef3 } from "react";
553
- export { useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSnapshotBulk };
1100
+ export { useDbSnapshots, useDeepCompareEffect, useDeepCompareMemo, useDocumentTitle, useSafeEffect, useSetUserCountry, useSmartSnapshots, useSocketSnapshots };
554
1101
  //# sourceMappingURL=index.mjs.map