@monorise/react 5.0.0-dev.5 → 5.0.0

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/index.js CHANGED
@@ -1,9 +1,34 @@
1
- import {
2
- WebSocketManager,
3
- __objRest,
4
- __spreadProps,
5
- __spreadValues
6
- } from "./chunk-BIXAYYE4.js";
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
7
32
 
8
33
  // actions/app.action.ts
9
34
  import { produce } from "immer";
@@ -289,13 +314,12 @@ var constructMutual = (byEntityType, byEntityId2, entityType, entityId, mutualDa
289
314
  mutualUpdatedAt: (/* @__PURE__ */ new Date()).toISOString()
290
315
  };
291
316
  };
292
- var flipMutual = (mutual, byEntityData) => {
317
+ var flipMutual = (mutual) => {
293
318
  return __spreadProps(__spreadValues({}, mutual), {
294
319
  entityId: mutual.byEntityId,
295
320
  entityType: mutual.byEntityType,
296
321
  byEntityId: mutual.entityId,
297
- byEntityType: mutual.entityType,
298
- data: byEntityData
322
+ byEntityType: mutual.entityType
299
323
  });
300
324
  };
301
325
  var byEntityId = (a, b) => {
@@ -1038,7 +1062,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1038
1062
  );
1039
1063
  monoriseStore.setState(
1040
1064
  produce4((state) => {
1041
- var _a2, _b, _c, _d, _e;
1065
+ var _a2, _b;
1042
1066
  const bySide = getMutualStateKey(
1043
1067
  byEntityType,
1044
1068
  byEntityId2,
@@ -1058,10 +1082,9 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1058
1082
  dataMap: /* @__PURE__ */ new Map()
1059
1083
  };
1060
1084
  }
1061
- const byEntityData = (_d = (_c = (_b = state.entity[mutual.byEntityType]) == null ? void 0 : _b.dataMap.get(mutual.byEntityId)) == null ? void 0 : _c.data) != null ? _d : {};
1062
- state.mutual[side].dataMap = new Map((_e = state.mutual[side]) == null ? void 0 : _e.dataMap).set(
1085
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1063
1086
  mutual.byEntityId,
1064
- flipMutual(mutual, byEntityData)
1087
+ flipMutual(mutual)
1065
1088
  );
1066
1089
  }),
1067
1090
  void 0,
@@ -1085,7 +1108,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1085
1108
  );
1086
1109
  monoriseStore.setState(
1087
1110
  produce4((state) => {
1088
- var _a, _b, _c, _d, _e;
1111
+ var _a, _b;
1089
1112
  const bySide = getMutualStateKey(byEntityType, byEntityId2, entityType);
1090
1113
  const side = getMutualStateKey(entityType, entityId, byEntityType);
1091
1114
  if (!state.mutual[bySide]) {
@@ -1101,10 +1124,9 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1101
1124
  dataMap: /* @__PURE__ */ new Map()
1102
1125
  };
1103
1126
  }
1104
- const byEntityData = (_d = (_c = (_b = state.entity[mutual.byEntityType]) == null ? void 0 : _b.dataMap.get(mutual.byEntityId)) == null ? void 0 : _c.data) != null ? _d : {};
1105
- state.mutual[side].dataMap = new Map((_e = state.mutual[side]) == null ? void 0 : _e.dataMap).set(
1127
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1106
1128
  mutual.byEntityId,
1107
- flipMutual(mutual, byEntityData)
1129
+ flipMutual(mutual)
1108
1130
  );
1109
1131
  }),
1110
1132
  void 0,
@@ -1132,7 +1154,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1132
1154
  );
1133
1155
  monoriseStore.setState(
1134
1156
  produce4((state) => {
1135
- var _a, _b, _c, _d, _e;
1157
+ var _a, _b;
1136
1158
  const bySide = getMutualStateKey(byEntityType, byEntityId2, entityType);
1137
1159
  const side = getMutualStateKey(entityType, entityId, byEntityType);
1138
1160
  if (!state.mutual[bySide]) {
@@ -1148,10 +1170,9 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1148
1170
  dataMap: /* @__PURE__ */ new Map()
1149
1171
  };
1150
1172
  }
1151
- const byEntityData = (_d = (_c = (_b = state.entity[mutual.byEntityType]) == null ? void 0 : _b.dataMap.get(mutual.byEntityId)) == null ? void 0 : _c.data) != null ? _d : {};
1152
- state.mutual[side].dataMap = new Map((_e = state.mutual[side]) == null ? void 0 : _e.dataMap).set(
1173
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1153
1174
  byEntityId2,
1154
- flipMutual(mutual, byEntityData)
1175
+ flipMutual(mutual)
1155
1176
  );
1156
1177
  }),
1157
1178
  void 0,
@@ -1171,7 +1192,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1171
1192
  );
1172
1193
  monoriseStore.setState(
1173
1194
  produce4((state) => {
1174
- var _a2, _b, _c, _d, _e;
1195
+ var _a2, _b;
1175
1196
  const bySide = getMutualStateKey(
1176
1197
  byEntityType,
1177
1198
  byEntityId2,
@@ -1191,10 +1212,9 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1191
1212
  dataMap: /* @__PURE__ */ new Map()
1192
1213
  };
1193
1214
  }
1194
- const byEntityData = (_d = (_c = (_b = state.entity[mutual.byEntityType]) == null ? void 0 : _b.dataMap.get(mutual.byEntityId)) == null ? void 0 : _c.data) != null ? _d : {};
1195
- state.mutual[side].dataMap = new Map((_e = state.mutual[side]) == null ? void 0 : _e.dataMap).set(
1215
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) == null ? void 0 : _b.dataMap).set(
1196
1216
  mutual.byEntityId,
1197
- flipMutual(mutual, byEntityData)
1217
+ flipMutual(mutual)
1198
1218
  );
1199
1219
  }),
1200
1220
  void 0,
@@ -1435,16 +1455,11 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1435
1455
  return () => queryTimeout && clearTimeout(queryTimeout);
1436
1456
  }, [entityType, query]);
1437
1457
  useEffect2(() => {
1438
- if (!query) {
1439
- const dataMapArray = Array.from(dataMap.values()).sort(
1440
- byEntityId
1458
+ if (!query && dataMap.size !== (entities == null ? void 0 : entities.length)) {
1459
+ setIsSearching(false);
1460
+ setEntities(
1461
+ Array.from(dataMap.values()).sort(byEntityId)
1441
1462
  );
1442
- if (dataMap.size !== (entities == null ? void 0 : entities.length) || dataMapArray.some(
1443
- (item, index) => JSON.stringify(item) !== JSON.stringify(entities == null ? void 0 : entities[index])
1444
- )) {
1445
- setIsSearching(false);
1446
- setEntities(dataMapArray);
1447
- }
1448
1463
  }
1449
1464
  if (query) {
1450
1465
  setEntities(searchResults);
@@ -1452,7 +1467,6 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1452
1467
  }, [
1453
1468
  dataMap,
1454
1469
  dataMap.size,
1455
- entities,
1456
1470
  entities == null ? void 0 : entities.length,
1457
1471
  query,
1458
1472
  searchResults,
@@ -1732,539 +1746,6 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1732
1746
  };
1733
1747
  };
1734
1748
 
1735
- // actions/websocket.action.ts
1736
- import { useCallback, useEffect as useEffect3, useRef, useState as useState3 } from "react";
1737
- import { produce as produce5 } from "immer";
1738
- var globalWsManager = null;
1739
- var wsEndpoint;
1740
- var initializeWebSocketManager = (WebSocketManagerClass, endpoint, token) => {
1741
- if (globalWsManager) return globalWsManager;
1742
- wsEndpoint = endpoint;
1743
- globalWsManager = new WebSocketManagerClass(endpoint, token || "");
1744
- globalWsManager.connect();
1745
- return globalWsManager;
1746
- };
1747
- var getWebSocketManager = () => globalWsManager;
1748
- var initWebSocketActions = (monoriseStore, httpActions) => {
1749
- const useEntitySocket2 = (entityType, opts = {}) => {
1750
- const { limit = 20, skipInitialFetch = false } = opts;
1751
- const [isLoading, setIsLoading] = useState3(!skipInitialFetch);
1752
- const [isFetchingMore, setIsFetchingMore] = useState3(false);
1753
- const [isRefreshing, setIsRefreshing] = useState3(false);
1754
- const [isSubscribed, setIsSubscribed] = useState3(false);
1755
- const [error, setError2] = useState3(null);
1756
- const [hasMore, setHasMore] = useState3(true);
1757
- const lastKeyRef = useRef(void 0);
1758
- const isFirstFetchRef = useRef(true);
1759
- const entities = monoriseStore((state) => {
1760
- var _a;
1761
- const entityState = state.entity[entityType];
1762
- return Array.from(((_a = entityState == null ? void 0 : entityState.dataMap) == null ? void 0 : _a.values()) || []);
1763
- });
1764
- const fetchData = async (type) => {
1765
- if (!httpActions) return;
1766
- if (type === "more") {
1767
- setIsFetchingMore(true);
1768
- } else if (type === "refresh") {
1769
- setIsRefreshing(true);
1770
- } else {
1771
- setIsLoading(true);
1772
- }
1773
- setError2(null);
1774
- try {
1775
- const fetchLastKey = type === "refresh" ? void 0 : lastKeyRef.current;
1776
- const result = await httpActions.listEntities(entityType, {
1777
- limit,
1778
- lastKey: fetchLastKey
1779
- });
1780
- monoriseStore.setState(
1781
- produce5((state) => {
1782
- if (type === "refresh") {
1783
- state.entity[entityType].dataMap.clear();
1784
- }
1785
- for (const entity of result.data) {
1786
- state.entity[entityType].dataMap.set(entity.entityId, entity);
1787
- }
1788
- state.entity[entityType].isFirstFetched = true;
1789
- state.entity[entityType].lastKey = result.lastKey;
1790
- })
1791
- );
1792
- lastKeyRef.current = result.lastKey;
1793
- setHasMore(!!result.lastKey);
1794
- } catch (err) {
1795
- setError2(err instanceof Error ? err : new Error("Failed to fetch"));
1796
- } finally {
1797
- setIsLoading(false);
1798
- setIsFetchingMore(false);
1799
- setIsRefreshing(false);
1800
- }
1801
- };
1802
- useEffect3(() => {
1803
- if (skipInitialFetch) return;
1804
- fetchData("initial");
1805
- }, [entityType, limit, skipInitialFetch]);
1806
- useEffect3(() => {
1807
- if (!globalWsManager) {
1808
- setIsSubscribed(false);
1809
- return;
1810
- }
1811
- const subKey = globalWsManager.subscribeEntityType(entityType);
1812
- setIsSubscribed(true);
1813
- let wasConnected = false;
1814
- const unsubscribeState = globalWsManager.onStateChange((state) => {
1815
- if (state === "connected" && wasConnected === false && !isFirstFetchRef.current) {
1816
- fetchData("refresh");
1817
- }
1818
- wasConnected = state === "connected";
1819
- isFirstFetchRef.current = false;
1820
- });
1821
- const unsubscribeMessage = globalWsManager.onMessage(
1822
- (msg) => {
1823
- if (msg.type === "entity.created" || msg.type === "entity.updated" || msg.type === "entity.deleted") {
1824
- const payload = msg.payload;
1825
- if (payload.entityType !== entityType) return;
1826
- if (msg.type === "entity.created" || msg.type === "entity.updated") {
1827
- if (payload.data) {
1828
- monoriseStore.setState(
1829
- produce5((state) => {
1830
- const key = entityType;
1831
- if (!state.entity[key]) {
1832
- state.entity[key] = { dataMap: /* @__PURE__ */ new Map(), isFirstFetched: false, lastKey: void 0 };
1833
- }
1834
- state.entity[key].dataMap.set(
1835
- payload.entityId,
1836
- payload.data
1837
- );
1838
- })
1839
- );
1840
- }
1841
- } else if (msg.type === "entity.deleted") {
1842
- monoriseStore.setState(
1843
- produce5((state) => {
1844
- var _a;
1845
- const key = entityType;
1846
- if ((_a = state.entity[key]) == null ? void 0 : _a.dataMap) {
1847
- state.entity[key].dataMap.delete(payload.entityId);
1848
- }
1849
- })
1850
- );
1851
- }
1852
- }
1853
- }
1854
- );
1855
- return () => {
1856
- globalWsManager == null ? void 0 : globalWsManager.unsubscribeEntityType(subKey);
1857
- unsubscribeState();
1858
- unsubscribeMessage();
1859
- setIsSubscribed(false);
1860
- };
1861
- }, [entityType]);
1862
- const fetchMore = async () => {
1863
- if (!hasMore || isFetchingMore) return;
1864
- await fetchData("more");
1865
- };
1866
- return {
1867
- entities,
1868
- isLoading,
1869
- isFetchingMore,
1870
- isSubscribed,
1871
- error,
1872
- isRefreshing,
1873
- fetchMore,
1874
- hasMore
1875
- };
1876
- };
1877
- const useMutualSocket2 = (byEntityType, byEntityId2, mutualEntityType, opts = {}) => {
1878
- const { limit = 20, skipInitialFetch = false } = opts;
1879
- const [isLoading, setIsLoading] = useState3(
1880
- !skipInitialFetch && !!byEntityId2
1881
- );
1882
- const [isFetchingMore, setIsFetchingMore] = useState3(false);
1883
- const [isRefreshing, setIsRefreshing] = useState3(false);
1884
- const [isSubscribed, setIsSubscribed] = useState3(false);
1885
- const [error, setError2] = useState3(null);
1886
- const [hasMore, setHasMore] = useState3(true);
1887
- const lastKeyRef = useRef(void 0);
1888
- const isFirstFetchRef = useRef(true);
1889
- const mutualKey = byEntityId2 ? `${byEntityType}/${byEntityId2}/${mutualEntityType}` : "";
1890
- const mutuals = monoriseStore((state) => {
1891
- var _a, _b;
1892
- return Array.from(((_b = (_a = state.mutual[mutualKey]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.values()) || []);
1893
- });
1894
- const fetchData = async (type) => {
1895
- if (!byEntityId2 || !httpActions) return;
1896
- if (type === "more") {
1897
- setIsFetchingMore(true);
1898
- } else if (type === "refresh") {
1899
- setIsRefreshing(true);
1900
- } else {
1901
- setIsLoading(true);
1902
- }
1903
- setError2(null);
1904
- try {
1905
- const fetchLastKey = type === "refresh" ? void 0 : lastKeyRef.current;
1906
- const result = await httpActions.listEntitiesByEntity(
1907
- byEntityType,
1908
- byEntityId2,
1909
- mutualEntityType,
1910
- { limit, lastKey: fetchLastKey }
1911
- );
1912
- monoriseStore.setState(
1913
- produce5((state) => {
1914
- if (!state.mutual[mutualKey]) {
1915
- state.mutual[mutualKey] = {
1916
- dataMap: /* @__PURE__ */ new Map(),
1917
- isFirstFetched: true,
1918
- lastKey: void 0
1919
- };
1920
- }
1921
- if (type === "refresh") {
1922
- state.mutual[mutualKey].dataMap.clear();
1923
- }
1924
- for (const entity of result.entities) {
1925
- const e = entity;
1926
- state.mutual[mutualKey].dataMap.set(e.entityId, entity);
1927
- }
1928
- state.mutual[mutualKey].lastKey = result.lastKey;
1929
- })
1930
- );
1931
- lastKeyRef.current = result.lastKey;
1932
- setHasMore(!!result.lastKey);
1933
- } catch (err) {
1934
- setError2(err instanceof Error ? err : new Error("Failed to fetch"));
1935
- } finally {
1936
- setIsLoading(false);
1937
- setIsFetchingMore(false);
1938
- setIsRefreshing(false);
1939
- }
1940
- };
1941
- useEffect3(() => {
1942
- if (skipInitialFetch || !byEntityId2) {
1943
- setIsLoading(false);
1944
- return;
1945
- }
1946
- lastKeyRef.current = void 0;
1947
- setHasMore(true);
1948
- isFirstFetchRef.current = true;
1949
- fetchData("initial");
1950
- }, [byEntityType, byEntityId2, mutualEntityType, limit, skipInitialFetch]);
1951
- useEffect3(() => {
1952
- if (!globalWsManager || !byEntityId2) {
1953
- setIsSubscribed(false);
1954
- return;
1955
- }
1956
- const subKey = globalWsManager.subscribeMutualType(
1957
- byEntityType,
1958
- byEntityId2,
1959
- mutualEntityType
1960
- );
1961
- setIsSubscribed(true);
1962
- let wasConnected = false;
1963
- const unsubscribeState = globalWsManager.onStateChange((state) => {
1964
- if (state === "connected" && wasConnected === false && !isFirstFetchRef.current) {
1965
- fetchData("refresh");
1966
- }
1967
- wasConnected = state === "connected";
1968
- isFirstFetchRef.current = false;
1969
- });
1970
- const unsubscribeMessage = globalWsManager.onMessage(
1971
- (msg) => {
1972
- if (msg.type === "mutual.created" || msg.type === "mutual.updated" || msg.type === "mutual.deleted") {
1973
- const payload = msg.payload;
1974
- if (payload.byEntityType !== byEntityType || payload.byEntityId !== byEntityId2 || payload.mutualEntityType !== mutualEntityType) {
1975
- return;
1976
- }
1977
- if (msg.type === "mutual.created" || msg.type === "mutual.updated") {
1978
- if (payload.data) {
1979
- monoriseStore.setState(
1980
- produce5((state) => {
1981
- if (!state.mutual[mutualKey]) {
1982
- state.mutual[mutualKey] = {
1983
- dataMap: /* @__PURE__ */ new Map(),
1984
- isFirstFetched: true,
1985
- lastKey: void 0
1986
- };
1987
- }
1988
- state.mutual[mutualKey].dataMap.set(
1989
- payload.entityId,
1990
- payload.data
1991
- );
1992
- })
1993
- );
1994
- }
1995
- } else if (msg.type === "mutual.deleted") {
1996
- monoriseStore.setState(
1997
- produce5((state) => {
1998
- var _a;
1999
- (_a = state.mutual[mutualKey]) == null ? void 0 : _a.dataMap.delete(payload.entityId);
2000
- })
2001
- );
2002
- }
2003
- }
2004
- }
2005
- );
2006
- return () => {
2007
- globalWsManager == null ? void 0 : globalWsManager.unsubscribeMutualType(subKey);
2008
- unsubscribeState();
2009
- unsubscribeMessage();
2010
- setIsSubscribed(false);
2011
- };
2012
- }, [byEntityType, byEntityId2, mutualEntityType, mutualKey]);
2013
- const fetchMore = async () => {
2014
- if (!hasMore || isFetchingMore) return;
2015
- await fetchData("more");
2016
- };
2017
- return {
2018
- mutuals,
2019
- isLoading,
2020
- isFetchingMore,
2021
- isSubscribed,
2022
- error,
2023
- isRefreshing,
2024
- fetchMore,
2025
- hasMore
2026
- };
2027
- };
2028
- const useEphemeralSocket2 = (channel, opts) => {
2029
- const [isSubscribed, setIsSubscribed] = useState3(false);
2030
- useEffect3(() => {
2031
- if (!globalWsManager || !channel) {
2032
- setIsSubscribed(false);
2033
- return;
2034
- }
2035
- const subKey = globalWsManager.subscribeEphemeral(channel);
2036
- setIsSubscribed(true);
2037
- const unsubscribeMessage = globalWsManager.onMessage(
2038
- (msg) => {
2039
- var _a;
2040
- if (msg.type === "ephemeral") {
2041
- const payload = msg.payload;
2042
- if (payload.channel !== channel) return;
2043
- (_a = opts == null ? void 0 : opts.onMessage) == null ? void 0 : _a.call(opts, payload.data, payload.senderId);
2044
- }
2045
- }
2046
- );
2047
- return () => {
2048
- globalWsManager == null ? void 0 : globalWsManager.unsubscribeEphemeral(subKey);
2049
- unsubscribeMessage();
2050
- setIsSubscribed(false);
2051
- };
2052
- }, [channel]);
2053
- const send = useCallback(
2054
- (data) => {
2055
- if (!globalWsManager || !channel) return;
2056
- globalWsManager.sendEphemeral(channel, data);
2057
- },
2058
- [channel]
2059
- );
2060
- return { isSubscribed, send };
2061
- };
2062
- const useEntityFeed2 = (opts) => {
2063
- const { entityType, entityId, ticketEndpoint } = opts;
2064
- const [isConnected, setIsConnected] = useState3(false);
2065
- const [error, setError2] = useState3(null);
2066
- const refreshTimerRef = useRef(null);
2067
- const wsManagerRef = useRef(null);
2068
- const isMountedRef = useRef(true);
2069
- const connectingRef = useRef(false);
2070
- const reconnectCountRef = useRef(0);
2071
- const connectRef = useRef(() => {
2072
- });
2073
- const lastConnectedAtRef = useRef(0);
2074
- const fetchTicket = useCallback(async () => {
2075
- try {
2076
- const url = ticketEndpoint || `/api/core/ws/ticket/${entityType}/${entityId}`;
2077
- const fetchOpts = {
2078
- method: "POST",
2079
- headers: { "Content-Type": "application/json" }
2080
- };
2081
- if (ticketEndpoint) {
2082
- fetchOpts.body = JSON.stringify({ entityType, entityId });
2083
- }
2084
- const response = await fetch(url, fetchOpts);
2085
- if (response.status === 401 || response.status === 403) {
2086
- return { error: { code: "TICKET_UNAUTHORIZED", message: "Unauthorized" } };
2087
- }
2088
- if (!response.ok) {
2089
- return { error: { code: "TICKET_FAILED", message: `HTTP ${response.status}` } };
2090
- }
2091
- const data = await response.json();
2092
- return { data };
2093
- } catch (err) {
2094
- return { error: { code: "TICKET_FAILED", message: err.message } };
2095
- }
2096
- }, [entityType, entityId, ticketEndpoint]);
2097
- const connectWithTicket = useCallback(async () => {
2098
- if (connectingRef.current) return;
2099
- connectingRef.current = true;
2100
- let result = null;
2101
- const maxRetries = 3;
2102
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
2103
- result = await fetchTicket();
2104
- if (!isMountedRef.current) {
2105
- connectingRef.current = false;
2106
- return;
2107
- }
2108
- if (!result.error) break;
2109
- if (result.error.code === "TICKET_UNAUTHORIZED") break;
2110
- if (attempt < maxRetries) {
2111
- await new Promise((r) => setTimeout(r, 1e3 * Math.pow(2, attempt)));
2112
- if (!isMountedRef.current) {
2113
- connectingRef.current = false;
2114
- return;
2115
- }
2116
- }
2117
- }
2118
- if (result.error) {
2119
- setError2(result.error);
2120
- setIsConnected(false);
2121
- connectingRef.current = false;
2122
- return;
2123
- }
2124
- setError2(null);
2125
- const { ticket, wsUrl, expiresIn } = result.data;
2126
- if (wsManagerRef.current) {
2127
- wsManagerRef.current.disconnect();
2128
- }
2129
- const { WebSocketManager: WsManagerClass } = await import("./websocket-7MZ3ENOC.js");
2130
- const wsUrlWithTicket = `${wsUrl}?ticket=${encodeURIComponent(ticket)}`;
2131
- const manager = new WsManagerClass(wsUrlWithTicket, "");
2132
- manager.disableAutoReconnect = true;
2133
- wsManagerRef.current = manager;
2134
- globalWsManager = manager;
2135
- manager.onStateChange((state) => {
2136
- if (!isMountedRef.current) return;
2137
- if (wsManagerRef.current !== manager) return;
2138
- if (state === "connected") {
2139
- connectingRef.current = false;
2140
- reconnectCountRef.current = 0;
2141
- lastConnectedAtRef.current = Date.now();
2142
- setIsConnected(true);
2143
- setError2(null);
2144
- } else if (state === "disconnected") {
2145
- connectingRef.current = false;
2146
- setIsConnected(false);
2147
- const wasStable = Date.now() - lastConnectedAtRef.current > 5e3;
2148
- if (wasStable && reconnectCountRef.current < 5) {
2149
- reconnectCountRef.current++;
2150
- const delay = 2e3 * Math.pow(2, reconnectCountRef.current - 1);
2151
- setTimeout(() => {
2152
- if (isMountedRef.current && !connectingRef.current) {
2153
- connectRef.current();
2154
- }
2155
- }, delay);
2156
- }
2157
- }
2158
- });
2159
- manager.onMessage((msg) => {
2160
- if (!isMountedRef.current) return;
2161
- if (msg.type === "entity.created" || msg.type === "entity.updated") {
2162
- const payload = msg.payload;
2163
- if (payload.data) {
2164
- monoriseStore.setState(
2165
- produce5((state) => {
2166
- const key = payload.entityType;
2167
- if (!state.entity[key]) {
2168
- state.entity[key] = { dataMap: /* @__PURE__ */ new Map(), isFirstFetched: false, lastKey: void 0 };
2169
- }
2170
- state.entity[key].dataMap.set(payload.entityId, payload.data);
2171
- })
2172
- );
2173
- }
2174
- } else if (msg.type === "entity.deleted") {
2175
- const payload = msg.payload;
2176
- monoriseStore.setState(
2177
- produce5((state) => {
2178
- var _a, _b;
2179
- (_b = (_a = state.entity[payload.entityType]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.delete(payload.entityId);
2180
- })
2181
- );
2182
- } else if (msg.type === "mutual.created" || msg.type === "mutual.updated") {
2183
- const payload = msg.payload;
2184
- if (payload.data) {
2185
- const mutualKey = `${payload.byEntityType}/${payload.byEntityId}/${payload.mutualEntityType}`;
2186
- monoriseStore.setState(
2187
- produce5((state) => {
2188
- if (!state.mutual[mutualKey]) {
2189
- state.mutual[mutualKey] = { dataMap: /* @__PURE__ */ new Map(), isFirstFetched: false, lastKey: void 0 };
2190
- }
2191
- state.mutual[mutualKey].dataMap.set(payload.entityId, payload.data);
2192
- })
2193
- );
2194
- }
2195
- } else if (msg.type === "mutual.deleted") {
2196
- const payload = msg.payload;
2197
- const mutualKey = `${payload.byEntityType}/${payload.byEntityId}/${payload.mutualEntityType}`;
2198
- monoriseStore.setState(
2199
- produce5((state) => {
2200
- var _a, _b;
2201
- (_b = (_a = state.mutual[mutualKey]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.delete(payload.entityId);
2202
- })
2203
- );
2204
- }
2205
- });
2206
- manager.connect();
2207
- if (refreshTimerRef.current) {
2208
- clearTimeout(refreshTimerRef.current);
2209
- }
2210
- const refreshMs = Math.max((expiresIn - 120) * 1e3, 6e4);
2211
- refreshTimerRef.current = setTimeout(() => {
2212
- if (isMountedRef.current) {
2213
- connectRef.current();
2214
- }
2215
- }, refreshMs);
2216
- }, [fetchTicket]);
2217
- connectRef.current = connectWithTicket;
2218
- useEffect3(() => {
2219
- isMountedRef.current = true;
2220
- if (entityId) {
2221
- connectRef.current();
2222
- }
2223
- let lastCheckTime = Date.now();
2224
- const sleepCheckInterval = setInterval(() => {
2225
- const now = Date.now();
2226
- const elapsed = now - lastCheckTime;
2227
- lastCheckTime = now;
2228
- if (elapsed > 3e4 && wsManagerRef.current) {
2229
- wsManagerRef.current.disconnect();
2230
- wsManagerRef.current = null;
2231
- connectingRef.current = false;
2232
- reconnectCountRef.current = 0;
2233
- connectRef.current();
2234
- }
2235
- }, 5e3);
2236
- const handleOnline = () => {
2237
- var _a;
2238
- if (!isMountedRef.current) return;
2239
- if (((_a = wsManagerRef.current) == null ? void 0 : _a.getState()) === "connected") return;
2240
- if (connectingRef.current) return;
2241
- reconnectCountRef.current = 0;
2242
- connectRef.current();
2243
- };
2244
- window.addEventListener("online", handleOnline);
2245
- return () => {
2246
- isMountedRef.current = false;
2247
- clearInterval(sleepCheckInterval);
2248
- window.removeEventListener("online", handleOnline);
2249
- if (refreshTimerRef.current) {
2250
- clearTimeout(refreshTimerRef.current);
2251
- }
2252
- if (wsManagerRef.current) {
2253
- wsManagerRef.current.disconnect();
2254
- wsManagerRef.current = null;
2255
- }
2256
- };
2257
- }, [entityType, entityId]);
2258
- return { isConnected, error };
2259
- };
2260
- return {
2261
- useEntitySocket: useEntitySocket2,
2262
- useMutualSocket: useMutualSocket2,
2263
- useEphemeralSocket: useEphemeralSocket2,
2264
- useEntityFeed: useEntityFeed2
2265
- };
2266
- };
2267
-
2268
1749
  // lib/api.ts
2269
1750
  import request from "axios";
2270
1751
  var initAxiosInterceptor = (store2, appActions) => {
@@ -2383,7 +1864,6 @@ var auth_service_default = initAuthService;
2383
1864
  var ENTITY_API_BASE_URL = "/api/core/entity";
2384
1865
  var MUTUAL_API_BASE_URL = "/api/core/mutual";
2385
1866
  var TAG_API_BASE_URL = "/api/core/tag";
2386
- var TRANSACTION_API_BASE_URL = "/api/core/transaction";
2387
1867
  var initCoreService = (monoriseStore, axios2, opts) => {
2388
1868
  let options = opts || {};
2389
1869
  const listEntities = (entityType, payload, opts2 = {}) => {
@@ -2500,10 +1980,9 @@ var initCoreService = (monoriseStore, axios2, opts) => {
2500
1980
  var _a;
2501
1981
  const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
2502
1982
  const entityConfig = monoriseStore.getState().config;
2503
- const body = opts2.condition ? __spreadProps(__spreadValues({}, adjustments), { $condition: opts2.condition }) : adjustments;
2504
1983
  return axios2.post(
2505
1984
  opts2.customUrl || `${entityApiBaseUrl}/${entityType}/${id}/adjust`,
2506
- body,
1985
+ adjustments,
2507
1986
  {
2508
1987
  requestKey: getEntityRequestKey("adjust", entityType, id),
2509
1988
  isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
@@ -2530,22 +2009,6 @@ var initCoreService = (monoriseStore, axios2, opts) => {
2530
2009
  }
2531
2010
  );
2532
2011
  };
2533
- const transaction = (operations, opts2 = {}) => {
2534
- var _a;
2535
- const { transactionApiBaseUrl = TRANSACTION_API_BASE_URL } = options;
2536
- return axios2.post(
2537
- opts2.customUrl || transactionApiBaseUrl,
2538
- { operations },
2539
- {
2540
- requestKey: "transaction",
2541
- isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
2542
- feedback: __spreadValues({
2543
- loading: "Processing transaction",
2544
- success: "Transaction completed"
2545
- }, opts2.feedback || {})
2546
- }
2547
- );
2548
- };
2549
2012
  const listEntitiesByEntity = (byEntityType, entityType, byEntityId2, opts2 = {}, chainEntityQuery) => {
2550
2013
  const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
2551
2014
  return axios2.get(
@@ -2689,7 +2152,6 @@ var initCoreService = (monoriseStore, axios2, opts) => {
2689
2152
  return {
2690
2153
  makeEntityService,
2691
2154
  makeMutualService,
2692
- transaction,
2693
2155
  setOptions
2694
2156
  };
2695
2157
  };
@@ -2920,7 +2382,6 @@ var initMonoriseStore = () => {
2920
2382
  };
2921
2383
 
2922
2384
  // index.ts
2923
- import { transactional } from "@monorise/base";
2924
2385
  var initMonorise = () => {
2925
2386
  const { monoriseStore: store2, setOptions: setMonoriseOptions } = initMonoriseStore();
2926
2387
  const appActions = initAppActions(store2);
@@ -2931,7 +2392,6 @@ var initMonorise = () => {
2931
2392
  const coreService2 = core_service_default(store2, axios2);
2932
2393
  const authActions = initAuthActions(store2, authService2);
2933
2394
  const coreActions = initCoreActions(store2, appActions, coreService2);
2934
- const websocketActions = initWebSocketActions(store2);
2935
2395
  const axiosInterceptor = injectAxiosInterceptor(
2936
2396
  appActions,
2937
2397
  authActions,
@@ -2957,14 +2417,14 @@ var initMonorise = () => {
2957
2417
  onApiSuccess: opts.onApiSuccess
2958
2418
  });
2959
2419
  };
2960
- return __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
2420
+ return __spreadValues(__spreadValues(__spreadValues(__spreadValues({
2961
2421
  config,
2962
2422
  store: store2,
2963
2423
  axios: axios2,
2964
2424
  authService: authService2,
2965
2425
  filestoreService: filestoreService2,
2966
2426
  coreService: coreService2
2967
- }, configActions), appActions), authActions), coreActions), websocketActions);
2427
+ }, configActions), appActions), authActions), coreActions);
2968
2428
  };
2969
2429
  var Monorise = initMonorise();
2970
2430
  var {
@@ -3016,15 +2476,10 @@ var {
3016
2476
  useEntityState,
3017
2477
  getEntity,
3018
2478
  updateLocalTaggedEntity,
3019
- deleteLocalTaggedEntity,
3020
- useEntitySocket,
3021
- useMutualSocket,
3022
- useEphemeralSocket,
3023
- useEntityFeed
2479
+ deleteLocalTaggedEntity
3024
2480
  } = Monorise;
3025
2481
  var index_default = Monorise;
3026
2482
  export {
3027
- WebSocketManager,
3028
2483
  adjustEntity,
3029
2484
  authService,
3030
2485
  axios,
@@ -3052,8 +2507,6 @@ export {
3052
2507
  getProfile,
3053
2508
  getTagRequestKey,
3054
2509
  getUniqueFieldRequestKey,
3055
- getWebSocketManager,
3056
- initializeWebSocketManager,
3057
2510
  listMoreEntities,
3058
2511
  logout,
3059
2512
  openModal,
@@ -3063,7 +2516,6 @@ export {
3063
2516
  setIsUnauthorized,
3064
2517
  startLoading,
3065
2518
  store,
3066
- transactional,
3067
2519
  updateLocalEntity,
3068
2520
  updateLocalTaggedEntity,
3069
2521
  upsertEntity,
@@ -3072,17 +2524,13 @@ export {
3072
2524
  useEntities,
3073
2525
  useEntity,
3074
2526
  useEntityByUniqueField,
3075
- useEntityFeed,
3076
- useEntitySocket,
3077
2527
  useEntityState,
3078
- useEphemeralSocket,
3079
2528
  useErrorStore,
3080
2529
  useInterruptiveLoadStore,
3081
2530
  useIsUnauthorized,
3082
2531
  useLoadStore,
3083
2532
  useModalStore,
3084
2533
  useMutual,
3085
- useMutualSocket,
3086
2534
  useMutuals,
3087
2535
  useProfile,
3088
2536
  useTaggedEntities