@monorise/react 8.0.0 → 8.2.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/actions/app.action.d.ts +3 -2
- package/dist/actions/app.action.d.ts.map +1 -1
- package/dist/actions/config.action.d.ts +2 -2
- package/dist/actions/core.action.d.ts +10 -0
- package/dist/actions/core.action.d.ts.map +1 -1
- package/dist/actions/websocket.action.d.ts +71 -0
- package/dist/actions/websocket.action.d.ts.map +1 -0
- package/dist/chunk-CQQYJW4K.js +444 -0
- package/dist/chunk-CQQYJW4K.js.map +1 -0
- package/dist/helpers/transactional.d.ts +13 -0
- package/dist/helpers/transactional.d.ts.map +1 -1
- package/dist/index.d.ts +61 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +849 -142
- package/dist/index.js.map +1 -1
- package/dist/services/core.service.d.ts +4 -2
- package/dist/services/core.service.d.ts.map +1 -1
- package/dist/websocket/WebSocketManager.d.ts +72 -0
- package/dist/websocket/WebSocketManager.d.ts.map +1 -0
- package/dist/websocket/index.d.ts +3 -0
- package/dist/websocket/index.d.ts.map +1 -0
- package/dist/websocket/optimistic.d.ts +51 -0
- package/dist/websocket/optimistic.d.ts.map +1 -0
- package/dist/websocket-34W2HQLH.js +9 -0
- package/dist/websocket-34W2HQLH.js.map +1 -0
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -1,34 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
WebSocketManager,
|
|
3
|
+
__objRest,
|
|
4
|
+
__spreadProps,
|
|
5
|
+
__spreadValues
|
|
6
|
+
} from "./chunk-CQQYJW4K.js";
|
|
32
7
|
|
|
33
8
|
// actions/app.action.ts
|
|
34
9
|
import { produce } from "immer";
|
|
@@ -47,7 +22,9 @@ var initAppActions = (store2) => {
|
|
|
47
22
|
const stackType = isInterruptive ? "intLoadStack" : "loadStack";
|
|
48
23
|
const requestKeyCount = state.app[stackType].get(requestKey);
|
|
49
24
|
state.app.loadingMessage = typeof message === "string" ? message : "Loading";
|
|
50
|
-
|
|
25
|
+
if (request2) {
|
|
26
|
+
state.app.ongoingRequests.set(requestKey, request2);
|
|
27
|
+
}
|
|
51
28
|
state.app[stackType].set(
|
|
52
29
|
requestKey,
|
|
53
30
|
requestKeyCount ? requestKeyCount + 1 : 1
|
|
@@ -57,13 +34,17 @@ var initAppActions = (store2) => {
|
|
|
57
34
|
`mr/start-loading/${requestKey}`
|
|
58
35
|
);
|
|
59
36
|
};
|
|
60
|
-
const endLoading2 = ({
|
|
37
|
+
const endLoading2 = ({
|
|
38
|
+
requestKey,
|
|
39
|
+
isInterruptive,
|
|
40
|
+
skipDedupe
|
|
41
|
+
}) => {
|
|
61
42
|
setTimeout(() => {
|
|
62
43
|
store2.setState(
|
|
63
44
|
produce((state) => {
|
|
64
45
|
const stackType = isInterruptive ? "intLoadStack" : "loadStack";
|
|
65
46
|
const requestKeyCount = state.app[stackType].get(requestKey);
|
|
66
|
-
if (state.app.ongoingRequests.has(requestKey)) {
|
|
47
|
+
if (!skipDedupe && state.app.ongoingRequests.has(requestKey)) {
|
|
67
48
|
state.app.ongoingRequests.delete(requestKey);
|
|
68
49
|
}
|
|
69
50
|
if (typeof requestKeyCount === "number" && requestKeyCount > 1) {
|
|
@@ -373,6 +354,54 @@ var getUniqueFieldRequestKey = (entityType, fieldName, value) => {
|
|
|
373
354
|
return `entity/${entityType}/unique/${getUniqueFieldStateKey(fieldName, value)}`;
|
|
374
355
|
};
|
|
375
356
|
|
|
357
|
+
// helpers/transactional.ts
|
|
358
|
+
var getTransactionOperationRequestKey = (op) => {
|
|
359
|
+
switch (op.operation) {
|
|
360
|
+
case "createEntity":
|
|
361
|
+
return getEntityRequestKey("create", op.entityType);
|
|
362
|
+
case "updateEntity":
|
|
363
|
+
return getEntityRequestKey("edit", op.entityType, op.entityId);
|
|
364
|
+
case "adjustEntity":
|
|
365
|
+
return getEntityRequestKey("adjust", op.entityType, op.entityId);
|
|
366
|
+
case "deleteEntity":
|
|
367
|
+
return getEntityRequestKey("delete", op.entityType, op.entityId);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
var getTransactionCallRequestKey = (operations) => `transaction/${operations.map(getTransactionOperationRequestKey).join("|")}`;
|
|
371
|
+
var transactional = {
|
|
372
|
+
createEntity: (entityType, payload) => {
|
|
373
|
+
const _a = payload, { entityId } = _a, rest = __objRest(_a, ["entityId"]);
|
|
374
|
+
return __spreadValues({
|
|
375
|
+
operation: "createEntity",
|
|
376
|
+
entityType,
|
|
377
|
+
payload: rest
|
|
378
|
+
}, entityId !== void 0 && { entityId });
|
|
379
|
+
},
|
|
380
|
+
updateEntity: (entityType, entityId, payload) => {
|
|
381
|
+
const _a = payload, { $condition } = _a, rest = __objRest(_a, ["$condition"]);
|
|
382
|
+
return __spreadValues({
|
|
383
|
+
operation: "updateEntity",
|
|
384
|
+
entityType,
|
|
385
|
+
entityId,
|
|
386
|
+
payload: rest
|
|
387
|
+
}, $condition !== void 0 && { condition: $condition });
|
|
388
|
+
},
|
|
389
|
+
adjustEntity: (entityType, entityId, adjustments) => {
|
|
390
|
+
const _a = adjustments, { $condition } = _a, rest = __objRest(_a, ["$condition"]);
|
|
391
|
+
return __spreadValues({
|
|
392
|
+
operation: "adjustEntity",
|
|
393
|
+
entityType,
|
|
394
|
+
entityId,
|
|
395
|
+
adjustments: rest
|
|
396
|
+
}, $condition !== void 0 && { condition: $condition });
|
|
397
|
+
},
|
|
398
|
+
deleteEntity: (entityType, entityId) => ({
|
|
399
|
+
operation: "deleteEntity",
|
|
400
|
+
entityType,
|
|
401
|
+
entityId
|
|
402
|
+
})
|
|
403
|
+
};
|
|
404
|
+
|
|
376
405
|
// actions/core.action.ts
|
|
377
406
|
var defaultOnError = (error) => {
|
|
378
407
|
if (!(error instanceof Error) && error.status >= 500) {
|
|
@@ -381,7 +410,16 @@ var defaultOnError = (error) => {
|
|
|
381
410
|
};
|
|
382
411
|
var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
383
412
|
const { makeEntityService, makeMutualService } = coreService2;
|
|
384
|
-
const {
|
|
413
|
+
const {
|
|
414
|
+
checkIsLoading,
|
|
415
|
+
getError: getError2,
|
|
416
|
+
useLoadStore: useLoadStore2,
|
|
417
|
+
useErrorStore: useErrorStore2,
|
|
418
|
+
startLoading: startLoading2,
|
|
419
|
+
endLoading: endLoading2,
|
|
420
|
+
setError: setError2,
|
|
421
|
+
clearError: clearError2
|
|
422
|
+
} = appActions;
|
|
385
423
|
const listEntities = async (entityType, params = { limit: 20 }, opts = {}) => {
|
|
386
424
|
var _a, _b;
|
|
387
425
|
const store2 = monoriseStore.getState();
|
|
@@ -651,6 +689,33 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
651
689
|
return { error: error2 };
|
|
652
690
|
}
|
|
653
691
|
};
|
|
692
|
+
const populateMutualsForCreatedEntity = (state, entityType, data, payload) => {
|
|
693
|
+
var _a, _b;
|
|
694
|
+
const mutualFields = (_b = (_a = state.config[entityType]) == null ? void 0 : _a.mutual) == null ? void 0 : _b.mutualFields;
|
|
695
|
+
if (!mutualFields) return;
|
|
696
|
+
for (const [field, fieldConfig] of Object.entries(mutualFields)) {
|
|
697
|
+
const byEntityType = fieldConfig.entityType;
|
|
698
|
+
const ids = payload[field];
|
|
699
|
+
if (!Array.isArray(ids)) continue;
|
|
700
|
+
for (const byEntityId2 of ids) {
|
|
701
|
+
const mutualKey = getMutualStateKey(byEntityType, byEntityId2, entityType);
|
|
702
|
+
if (state.mutual[mutualKey] && state.mutual[mutualKey].isFirstFetched) {
|
|
703
|
+
state.mutual[mutualKey].dataMap.set(data.entityId, {
|
|
704
|
+
entityId: data.entityId,
|
|
705
|
+
entityType,
|
|
706
|
+
byEntityId: byEntityId2,
|
|
707
|
+
byEntityType,
|
|
708
|
+
mutualId: "",
|
|
709
|
+
data: data.data,
|
|
710
|
+
mutualData: {},
|
|
711
|
+
createdAt: data.createdAt,
|
|
712
|
+
updatedAt: data.updatedAt,
|
|
713
|
+
mutualUpdatedAt: data.updatedAt
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
};
|
|
654
719
|
const createEntity2 = async (entityType, entity, opts = {}) => {
|
|
655
720
|
var _a;
|
|
656
721
|
const entityService = makeEntityService(entityType);
|
|
@@ -659,7 +724,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
659
724
|
const { data } = await entityService.createEntity(entity, opts);
|
|
660
725
|
monoriseStore.setState(
|
|
661
726
|
produce4((state) => {
|
|
662
|
-
var _a2, _b
|
|
727
|
+
var _a2, _b;
|
|
663
728
|
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
664
729
|
const tagConfigs = (_a2 = state.config[entityType]) == null ? void 0 : _a2.tags;
|
|
665
730
|
if (tagConfigs) {
|
|
@@ -704,35 +769,12 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
704
769
|
}
|
|
705
770
|
}
|
|
706
771
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
for (const byEntityId2 of ids) {
|
|
714
|
-
const mutualKey = getMutualStateKey(
|
|
715
|
-
byEntityType,
|
|
716
|
-
byEntityId2,
|
|
717
|
-
entityType
|
|
718
|
-
);
|
|
719
|
-
if (state.mutual[mutualKey] && state.mutual[mutualKey].isFirstFetched) {
|
|
720
|
-
state.mutual[mutualKey].dataMap.set(data.entityId, {
|
|
721
|
-
entityId: data.entityId,
|
|
722
|
-
entityType,
|
|
723
|
-
byEntityId: byEntityId2,
|
|
724
|
-
byEntityType,
|
|
725
|
-
mutualId: "",
|
|
726
|
-
data: data.data,
|
|
727
|
-
mutualData: {},
|
|
728
|
-
createdAt: data.createdAt,
|
|
729
|
-
updatedAt: data.updatedAt,
|
|
730
|
-
mutualUpdatedAt: data.updatedAt
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
}
|
|
772
|
+
populateMutualsForCreatedEntity(
|
|
773
|
+
state,
|
|
774
|
+
entityType,
|
|
775
|
+
data,
|
|
776
|
+
entity
|
|
777
|
+
);
|
|
736
778
|
}),
|
|
737
779
|
void 0,
|
|
738
780
|
`mr/entity/create/${entityType}`
|
|
@@ -819,6 +861,36 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
819
861
|
}
|
|
820
862
|
}
|
|
821
863
|
};
|
|
864
|
+
const propagateEntityDataToMutuals = (state, entityType, entityId, data) => {
|
|
865
|
+
for (const key of Object.keys(state.mutual)) {
|
|
866
|
+
const { entity: mutualEntityType } = parseMutualStateKey(key);
|
|
867
|
+
if (mutualEntityType === entityType) {
|
|
868
|
+
const mutual = state.mutual[key].dataMap.get(entityId);
|
|
869
|
+
if (mutual) {
|
|
870
|
+
state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(
|
|
871
|
+
entityId,
|
|
872
|
+
__spreadProps(__spreadValues({}, mutual), { data })
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
const deleteEntityFromStores = (state, entityType, entityId) => {
|
|
879
|
+
var _a, _b, _c;
|
|
880
|
+
(_a = state.entity[entityType]) == null ? void 0 : _a.dataMap.delete(entityId);
|
|
881
|
+
for (const key of Object.keys(state.mutual)) {
|
|
882
|
+
const { entity: mutualEntityType } = parseMutualStateKey(key);
|
|
883
|
+
if (mutualEntityType === entityType) {
|
|
884
|
+
state.mutual[key].dataMap.delete(entityId);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
for (const tagKey of Object.keys(state.tag)) {
|
|
888
|
+
const [tagEntityType] = tagKey.split("/");
|
|
889
|
+
if (tagEntityType === entityType) {
|
|
890
|
+
(_c = (_b = state.tag[tagKey]) == null ? void 0 : _b.dataMap) == null ? void 0 : _c.delete(entityId);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
};
|
|
822
894
|
const editEntity2 = async (entityType, id, entity, opts = {}) => {
|
|
823
895
|
var _a;
|
|
824
896
|
const entityService = makeEntityService(entityType);
|
|
@@ -828,17 +900,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
828
900
|
monoriseStore.setState(
|
|
829
901
|
produce4((state) => {
|
|
830
902
|
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
831
|
-
|
|
832
|
-
const { entity: mutualEntityType } = parseMutualStateKey(key);
|
|
833
|
-
if (mutualEntityType === entityType) {
|
|
834
|
-
const mutual = state.mutual[key].dataMap.get(id);
|
|
835
|
-
if (mutual) {
|
|
836
|
-
state.mutual[key].dataMap = new Map(
|
|
837
|
-
state.mutual[key].dataMap
|
|
838
|
-
).set(id, __spreadProps(__spreadValues({}, mutual), { data: data.data }));
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
}
|
|
903
|
+
propagateEntityDataToMutuals(state, entityType, id, data.data);
|
|
842
904
|
reconcileTaggedEntityStore(
|
|
843
905
|
state,
|
|
844
906
|
entityType,
|
|
@@ -865,17 +927,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
865
927
|
monoriseStore.setState(
|
|
866
928
|
produce4((state) => {
|
|
867
929
|
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
868
|
-
|
|
869
|
-
const { entity: mutualEntityType } = parseMutualStateKey(key);
|
|
870
|
-
if (mutualEntityType === entityType) {
|
|
871
|
-
const mutual = state.mutual[key].dataMap.get(id);
|
|
872
|
-
if (mutual) {
|
|
873
|
-
state.mutual[key].dataMap = new Map(
|
|
874
|
-
state.mutual[key].dataMap
|
|
875
|
-
).set(id, __spreadProps(__spreadValues({}, mutual), { data: data.data }));
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|
|
930
|
+
propagateEntityDataToMutuals(state, entityType, id, data.data);
|
|
879
931
|
reconcileTaggedEntityStore(
|
|
880
932
|
state,
|
|
881
933
|
entityType,
|
|
@@ -905,20 +957,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
905
957
|
deleteLocalMutualsByEntity(entityType, id);
|
|
906
958
|
monoriseStore.setState(
|
|
907
959
|
produce4((state) => {
|
|
908
|
-
|
|
909
|
-
state.entity[entityType].dataMap.delete(id);
|
|
910
|
-
for (const key of Object.keys(state.mutual)) {
|
|
911
|
-
const { entity: mutualEntityType } = parseMutualStateKey(key);
|
|
912
|
-
if (mutualEntityType === entityType) {
|
|
913
|
-
state.mutual[key].dataMap.delete(id);
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
for (const tagKey of Object.keys(state.tag)) {
|
|
917
|
-
const [tagEntityType] = tagKey.split("/");
|
|
918
|
-
if (tagEntityType === entityType) {
|
|
919
|
-
(_b = (_a2 = state.tag[tagKey]) == null ? void 0 : _a2.dataMap) == null ? void 0 : _b.delete(id);
|
|
920
|
-
}
|
|
921
|
-
}
|
|
960
|
+
deleteEntityFromStores(state, entityType, id);
|
|
922
961
|
}),
|
|
923
962
|
void 0,
|
|
924
963
|
`mr/entity/delete/${entityType}/${id}`
|
|
@@ -930,6 +969,103 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
930
969
|
return { error };
|
|
931
970
|
}
|
|
932
971
|
};
|
|
972
|
+
const executeTransaction2 = async (operations, opts = {}) => {
|
|
973
|
+
var _a, _b, _c, _d;
|
|
974
|
+
const onError = (_a = opts.onError) != null ? _a : defaultOnError;
|
|
975
|
+
const isInterruptive = (_b = opts.isInterruptive) != null ? _b : true;
|
|
976
|
+
const opRequestKeys = operations.map(getTransactionOperationRequestKey);
|
|
977
|
+
const callRequestKey = opts.requestKey || getTransactionCallRequestKey(operations);
|
|
978
|
+
const callPromise = coreService2.executeTransaction(operations, __spreadProps(__spreadValues({}, opts), {
|
|
979
|
+
requestKey: callRequestKey
|
|
980
|
+
}));
|
|
981
|
+
const loadingMessage = (_d = (_c = opts.feedback) == null ? void 0 : _c.loading) != null ? _d : "Processing transaction";
|
|
982
|
+
for (const requestKey of opRequestKeys) {
|
|
983
|
+
startLoading2({ requestKey, isInterruptive, message: loadingMessage });
|
|
984
|
+
}
|
|
985
|
+
let settled = false;
|
|
986
|
+
try {
|
|
987
|
+
const { data } = await callPromise;
|
|
988
|
+
for (const requestKey of opRequestKeys) {
|
|
989
|
+
endLoading2({ requestKey, isInterruptive, skipDedupe: true });
|
|
990
|
+
clearError2(requestKey);
|
|
991
|
+
}
|
|
992
|
+
settled = true;
|
|
993
|
+
const deletedEntities = [];
|
|
994
|
+
monoriseStore.setState(
|
|
995
|
+
produce4((state) => {
|
|
996
|
+
data.results.forEach((entry, index) => {
|
|
997
|
+
var _a2, _b2, _c2;
|
|
998
|
+
const { entityType, entityId } = entry;
|
|
999
|
+
if (entry.operation === "deleteEntity") {
|
|
1000
|
+
deleteEntityFromStores(state, entityType, entityId);
|
|
1001
|
+
deletedEntities.push({ entityType, entityId });
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
if (entry.operation === "createEntity") {
|
|
1005
|
+
const createdEntity = {
|
|
1006
|
+
entityId,
|
|
1007
|
+
entityType,
|
|
1008
|
+
data: (_a2 = entry.data) != null ? _a2 : {},
|
|
1009
|
+
createdAt: entry.createdAt,
|
|
1010
|
+
updatedAt: entry.updatedAt
|
|
1011
|
+
};
|
|
1012
|
+
state.entity[entityType].dataMap.set(entityId, createdEntity);
|
|
1013
|
+
const op = operations[index];
|
|
1014
|
+
populateMutualsForCreatedEntity(
|
|
1015
|
+
state,
|
|
1016
|
+
entityType,
|
|
1017
|
+
createdEntity,
|
|
1018
|
+
op == null ? void 0 : op.payload
|
|
1019
|
+
);
|
|
1020
|
+
reconcileTaggedEntityStore(
|
|
1021
|
+
state,
|
|
1022
|
+
entityType,
|
|
1023
|
+
entityId,
|
|
1024
|
+
createdEntity
|
|
1025
|
+
);
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
const existing = (_b2 = state.entity[entityType]) == null ? void 0 : _b2.dataMap.get(entityId);
|
|
1029
|
+
if (!existing && !entry.data) return;
|
|
1030
|
+
if (!entry.data) {
|
|
1031
|
+
if (entry.updatedAt) {
|
|
1032
|
+
state.entity[entityType].dataMap.set(entityId, __spreadProps(__spreadValues({}, existing), {
|
|
1033
|
+
updatedAt: entry.updatedAt
|
|
1034
|
+
}));
|
|
1035
|
+
}
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
const updated = __spreadProps(__spreadValues({}, existing != null ? existing : { entityId, entityType }), {
|
|
1039
|
+
data: entry.data,
|
|
1040
|
+
updatedAt: (_c2 = entry.updatedAt) != null ? _c2 : existing == null ? void 0 : existing.updatedAt
|
|
1041
|
+
});
|
|
1042
|
+
state.entity[entityType].dataMap.set(entityId, updated);
|
|
1043
|
+
propagateEntityDataToMutuals(state, entityType, entityId, updated.data);
|
|
1044
|
+
reconcileTaggedEntityStore(state, entityType, entityId, updated);
|
|
1045
|
+
});
|
|
1046
|
+
}),
|
|
1047
|
+
void 0,
|
|
1048
|
+
"mr/entity/execute-transaction"
|
|
1049
|
+
);
|
|
1050
|
+
for (const { entityType, entityId } of deletedEntities) {
|
|
1051
|
+
deleteLocalMutualsByEntity(entityType, entityId);
|
|
1052
|
+
}
|
|
1053
|
+
return { data };
|
|
1054
|
+
} catch (err) {
|
|
1055
|
+
if (!settled) {
|
|
1056
|
+
const callAppError = getError2(callRequestKey);
|
|
1057
|
+
for (const requestKey of opRequestKeys) {
|
|
1058
|
+
endLoading2({ requestKey, isInterruptive, skipDedupe: true });
|
|
1059
|
+
if (callAppError) {
|
|
1060
|
+
setError2({ requestKey, error: callAppError });
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
const error = err instanceof Error ? err : new Error("Unknown error occurred");
|
|
1065
|
+
onError(error);
|
|
1066
|
+
return { error };
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
933
1069
|
const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
|
|
934
1070
|
var _a, _b, _c;
|
|
935
1071
|
const onError = (_a = opts.onError) != null ? _a : defaultOnError;
|
|
@@ -1798,6 +1934,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
1798
1934
|
editEntity: editEntity2,
|
|
1799
1935
|
adjustEntity: adjustEntity2,
|
|
1800
1936
|
deleteEntity: deleteEntity2,
|
|
1937
|
+
executeTransaction: executeTransaction2,
|
|
1801
1938
|
getMutual: getMutual2,
|
|
1802
1939
|
updateLocalEntity: updateLocalEntity2,
|
|
1803
1940
|
createMutual: createMutual2,
|
|
@@ -1818,6 +1955,575 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
|
|
|
1818
1955
|
};
|
|
1819
1956
|
};
|
|
1820
1957
|
|
|
1958
|
+
// actions/websocket.action.ts
|
|
1959
|
+
import { useCallback, useEffect as useEffect3, useRef, useState as useState3 } from "react";
|
|
1960
|
+
import { produce as produce5 } from "immer";
|
|
1961
|
+
var globalWsManager = null;
|
|
1962
|
+
var wsEndpoint;
|
|
1963
|
+
var initializeWebSocketManager = (WebSocketManagerClass, endpoint, token) => {
|
|
1964
|
+
if (globalWsManager) return globalWsManager;
|
|
1965
|
+
wsEndpoint = endpoint;
|
|
1966
|
+
globalWsManager = new WebSocketManagerClass(endpoint, token || "");
|
|
1967
|
+
globalWsManager.connect();
|
|
1968
|
+
return globalWsManager;
|
|
1969
|
+
};
|
|
1970
|
+
var getWebSocketManager = () => globalWsManager;
|
|
1971
|
+
var initWebSocketActions = (monoriseStore, httpActions) => {
|
|
1972
|
+
const useEntitySocket2 = (entityType, opts = {}) => {
|
|
1973
|
+
const { limit = 20, skipInitialFetch = false } = opts;
|
|
1974
|
+
const [isLoading, setIsLoading] = useState3(!skipInitialFetch);
|
|
1975
|
+
const [isFetchingMore, setIsFetchingMore] = useState3(false);
|
|
1976
|
+
const [isRefreshing, setIsRefreshing] = useState3(false);
|
|
1977
|
+
const [isSubscribed, setIsSubscribed] = useState3(false);
|
|
1978
|
+
const [error, setError2] = useState3(null);
|
|
1979
|
+
const [hasMore, setHasMore] = useState3(true);
|
|
1980
|
+
const lastKeyRef = useRef(void 0);
|
|
1981
|
+
const isFirstFetchRef = useRef(true);
|
|
1982
|
+
const entities = monoriseStore((state) => {
|
|
1983
|
+
var _a;
|
|
1984
|
+
const entityState = state.entity[entityType];
|
|
1985
|
+
return Array.from(((_a = entityState == null ? void 0 : entityState.dataMap) == null ? void 0 : _a.values()) || []);
|
|
1986
|
+
});
|
|
1987
|
+
const fetchData = async (type) => {
|
|
1988
|
+
if (!httpActions) return;
|
|
1989
|
+
if (type === "more") {
|
|
1990
|
+
setIsFetchingMore(true);
|
|
1991
|
+
} else if (type === "refresh") {
|
|
1992
|
+
setIsRefreshing(true);
|
|
1993
|
+
} else {
|
|
1994
|
+
setIsLoading(true);
|
|
1995
|
+
}
|
|
1996
|
+
setError2(null);
|
|
1997
|
+
try {
|
|
1998
|
+
const fetchLastKey = type === "refresh" ? void 0 : lastKeyRef.current;
|
|
1999
|
+
const result = await httpActions.listEntities(entityType, {
|
|
2000
|
+
limit,
|
|
2001
|
+
lastKey: fetchLastKey
|
|
2002
|
+
});
|
|
2003
|
+
monoriseStore.setState(
|
|
2004
|
+
produce5((state) => {
|
|
2005
|
+
if (type === "refresh") {
|
|
2006
|
+
state.entity[entityType].dataMap.clear();
|
|
2007
|
+
}
|
|
2008
|
+
for (const entity of result.data) {
|
|
2009
|
+
state.entity[entityType].dataMap.set(entity.entityId, entity);
|
|
2010
|
+
}
|
|
2011
|
+
state.entity[entityType].isFirstFetched = true;
|
|
2012
|
+
state.entity[entityType].lastKey = result.lastKey;
|
|
2013
|
+
})
|
|
2014
|
+
);
|
|
2015
|
+
lastKeyRef.current = result.lastKey;
|
|
2016
|
+
setHasMore(!!result.lastKey);
|
|
2017
|
+
} catch (err) {
|
|
2018
|
+
setError2(err instanceof Error ? err : new Error("Failed to fetch"));
|
|
2019
|
+
} finally {
|
|
2020
|
+
setIsLoading(false);
|
|
2021
|
+
setIsFetchingMore(false);
|
|
2022
|
+
setIsRefreshing(false);
|
|
2023
|
+
}
|
|
2024
|
+
};
|
|
2025
|
+
useEffect3(() => {
|
|
2026
|
+
if (skipInitialFetch) return;
|
|
2027
|
+
fetchData("initial");
|
|
2028
|
+
}, [entityType, limit, skipInitialFetch]);
|
|
2029
|
+
useEffect3(() => {
|
|
2030
|
+
if (!globalWsManager) {
|
|
2031
|
+
setIsSubscribed(false);
|
|
2032
|
+
return;
|
|
2033
|
+
}
|
|
2034
|
+
const subKey = globalWsManager.subscribeEntityType(entityType);
|
|
2035
|
+
setIsSubscribed(true);
|
|
2036
|
+
let wasConnected = false;
|
|
2037
|
+
const unsubscribeState = globalWsManager.onStateChange((state) => {
|
|
2038
|
+
if (state === "connected" && wasConnected === false && !isFirstFetchRef.current) {
|
|
2039
|
+
fetchData("refresh");
|
|
2040
|
+
}
|
|
2041
|
+
wasConnected = state === "connected";
|
|
2042
|
+
isFirstFetchRef.current = false;
|
|
2043
|
+
});
|
|
2044
|
+
const unsubscribeMessage = globalWsManager.onMessage(
|
|
2045
|
+
(msg) => {
|
|
2046
|
+
if (msg.type === "entity.created" || msg.type === "entity.updated" || msg.type === "entity.deleted") {
|
|
2047
|
+
const payload = msg.payload;
|
|
2048
|
+
if (payload.entityType !== entityType) return;
|
|
2049
|
+
if (msg.type === "entity.created" || msg.type === "entity.updated") {
|
|
2050
|
+
if (payload.data) {
|
|
2051
|
+
monoriseStore.setState(
|
|
2052
|
+
produce5((state) => {
|
|
2053
|
+
const key = entityType;
|
|
2054
|
+
if (!state.entity[key]) {
|
|
2055
|
+
state.entity[key] = { dataMap: /* @__PURE__ */ new Map(), isFirstFetched: false, lastKey: void 0 };
|
|
2056
|
+
}
|
|
2057
|
+
state.entity[key].dataMap.set(
|
|
2058
|
+
payload.entityId,
|
|
2059
|
+
payload.data
|
|
2060
|
+
);
|
|
2061
|
+
})
|
|
2062
|
+
);
|
|
2063
|
+
}
|
|
2064
|
+
} else if (msg.type === "entity.deleted") {
|
|
2065
|
+
monoriseStore.setState(
|
|
2066
|
+
produce5((state) => {
|
|
2067
|
+
var _a;
|
|
2068
|
+
const key = entityType;
|
|
2069
|
+
if ((_a = state.entity[key]) == null ? void 0 : _a.dataMap) {
|
|
2070
|
+
state.entity[key].dataMap.delete(payload.entityId);
|
|
2071
|
+
}
|
|
2072
|
+
})
|
|
2073
|
+
);
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
);
|
|
2078
|
+
return () => {
|
|
2079
|
+
globalWsManager == null ? void 0 : globalWsManager.unsubscribeEntityType(subKey);
|
|
2080
|
+
unsubscribeState();
|
|
2081
|
+
unsubscribeMessage();
|
|
2082
|
+
setIsSubscribed(false);
|
|
2083
|
+
};
|
|
2084
|
+
}, [entityType]);
|
|
2085
|
+
const fetchMore = async () => {
|
|
2086
|
+
if (!hasMore || isFetchingMore) return;
|
|
2087
|
+
await fetchData("more");
|
|
2088
|
+
};
|
|
2089
|
+
return {
|
|
2090
|
+
entities,
|
|
2091
|
+
isLoading,
|
|
2092
|
+
isFetchingMore,
|
|
2093
|
+
isSubscribed,
|
|
2094
|
+
error,
|
|
2095
|
+
isRefreshing,
|
|
2096
|
+
fetchMore,
|
|
2097
|
+
hasMore
|
|
2098
|
+
};
|
|
2099
|
+
};
|
|
2100
|
+
const useMutualSocket2 = (byEntityType, byEntityId2, mutualEntityType, opts = {}) => {
|
|
2101
|
+
const { limit = 20, skipInitialFetch = false } = opts;
|
|
2102
|
+
const [isLoading, setIsLoading] = useState3(
|
|
2103
|
+
!skipInitialFetch && !!byEntityId2
|
|
2104
|
+
);
|
|
2105
|
+
const [isFetchingMore, setIsFetchingMore] = useState3(false);
|
|
2106
|
+
const [isRefreshing, setIsRefreshing] = useState3(false);
|
|
2107
|
+
const [isSubscribed, setIsSubscribed] = useState3(false);
|
|
2108
|
+
const [error, setError2] = useState3(null);
|
|
2109
|
+
const [hasMore, setHasMore] = useState3(true);
|
|
2110
|
+
const lastKeyRef = useRef(void 0);
|
|
2111
|
+
const isFirstFetchRef = useRef(true);
|
|
2112
|
+
const mutualKey = byEntityId2 ? `${byEntityType}/${byEntityId2}/${mutualEntityType}` : "";
|
|
2113
|
+
const mutuals = monoriseStore((state) => {
|
|
2114
|
+
var _a, _b;
|
|
2115
|
+
return Array.from(((_b = (_a = state.mutual[mutualKey]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.values()) || []);
|
|
2116
|
+
});
|
|
2117
|
+
const fetchData = async (type) => {
|
|
2118
|
+
if (!byEntityId2 || !httpActions) return;
|
|
2119
|
+
if (type === "more") {
|
|
2120
|
+
setIsFetchingMore(true);
|
|
2121
|
+
} else if (type === "refresh") {
|
|
2122
|
+
setIsRefreshing(true);
|
|
2123
|
+
} else {
|
|
2124
|
+
setIsLoading(true);
|
|
2125
|
+
}
|
|
2126
|
+
setError2(null);
|
|
2127
|
+
try {
|
|
2128
|
+
const fetchLastKey = type === "refresh" ? void 0 : lastKeyRef.current;
|
|
2129
|
+
const result = await httpActions.listEntitiesByEntity(
|
|
2130
|
+
byEntityType,
|
|
2131
|
+
byEntityId2,
|
|
2132
|
+
mutualEntityType,
|
|
2133
|
+
{ limit, lastKey: fetchLastKey }
|
|
2134
|
+
);
|
|
2135
|
+
monoriseStore.setState(
|
|
2136
|
+
produce5((state) => {
|
|
2137
|
+
if (!state.mutual[mutualKey]) {
|
|
2138
|
+
state.mutual[mutualKey] = {
|
|
2139
|
+
dataMap: /* @__PURE__ */ new Map(),
|
|
2140
|
+
isFirstFetched: true,
|
|
2141
|
+
lastKey: void 0
|
|
2142
|
+
};
|
|
2143
|
+
}
|
|
2144
|
+
if (type === "refresh") {
|
|
2145
|
+
state.mutual[mutualKey].dataMap.clear();
|
|
2146
|
+
}
|
|
2147
|
+
for (const entity of result.entities) {
|
|
2148
|
+
const e = entity;
|
|
2149
|
+
state.mutual[mutualKey].dataMap.set(e.entityId, entity);
|
|
2150
|
+
}
|
|
2151
|
+
state.mutual[mutualKey].lastKey = result.lastKey;
|
|
2152
|
+
})
|
|
2153
|
+
);
|
|
2154
|
+
lastKeyRef.current = result.lastKey;
|
|
2155
|
+
setHasMore(!!result.lastKey);
|
|
2156
|
+
} catch (err) {
|
|
2157
|
+
setError2(err instanceof Error ? err : new Error("Failed to fetch"));
|
|
2158
|
+
} finally {
|
|
2159
|
+
setIsLoading(false);
|
|
2160
|
+
setIsFetchingMore(false);
|
|
2161
|
+
setIsRefreshing(false);
|
|
2162
|
+
}
|
|
2163
|
+
};
|
|
2164
|
+
useEffect3(() => {
|
|
2165
|
+
if (skipInitialFetch || !byEntityId2) {
|
|
2166
|
+
setIsLoading(false);
|
|
2167
|
+
return;
|
|
2168
|
+
}
|
|
2169
|
+
lastKeyRef.current = void 0;
|
|
2170
|
+
setHasMore(true);
|
|
2171
|
+
isFirstFetchRef.current = true;
|
|
2172
|
+
fetchData("initial");
|
|
2173
|
+
}, [byEntityType, byEntityId2, mutualEntityType, limit, skipInitialFetch]);
|
|
2174
|
+
useEffect3(() => {
|
|
2175
|
+
if (!globalWsManager || !byEntityId2) {
|
|
2176
|
+
setIsSubscribed(false);
|
|
2177
|
+
return;
|
|
2178
|
+
}
|
|
2179
|
+
const subKey = globalWsManager.subscribeMutualType(
|
|
2180
|
+
byEntityType,
|
|
2181
|
+
byEntityId2,
|
|
2182
|
+
mutualEntityType
|
|
2183
|
+
);
|
|
2184
|
+
setIsSubscribed(true);
|
|
2185
|
+
let wasConnected = false;
|
|
2186
|
+
const unsubscribeState = globalWsManager.onStateChange((state) => {
|
|
2187
|
+
if (state === "connected" && wasConnected === false && !isFirstFetchRef.current) {
|
|
2188
|
+
fetchData("refresh");
|
|
2189
|
+
}
|
|
2190
|
+
wasConnected = state === "connected";
|
|
2191
|
+
isFirstFetchRef.current = false;
|
|
2192
|
+
});
|
|
2193
|
+
const unsubscribeMessage = globalWsManager.onMessage(
|
|
2194
|
+
(msg) => {
|
|
2195
|
+
if (msg.type === "mutual.created" || msg.type === "mutual.updated" || msg.type === "mutual.deleted") {
|
|
2196
|
+
const payload = msg.payload;
|
|
2197
|
+
if (payload.byEntityType !== byEntityType || payload.byEntityId !== byEntityId2 || payload.mutualEntityType !== mutualEntityType) {
|
|
2198
|
+
return;
|
|
2199
|
+
}
|
|
2200
|
+
if (msg.type === "mutual.created" || msg.type === "mutual.updated") {
|
|
2201
|
+
if (payload.data) {
|
|
2202
|
+
monoriseStore.setState(
|
|
2203
|
+
produce5((state) => {
|
|
2204
|
+
if (!state.mutual[mutualKey]) {
|
|
2205
|
+
state.mutual[mutualKey] = {
|
|
2206
|
+
dataMap: /* @__PURE__ */ new Map(),
|
|
2207
|
+
isFirstFetched: true,
|
|
2208
|
+
lastKey: void 0
|
|
2209
|
+
};
|
|
2210
|
+
}
|
|
2211
|
+
state.mutual[mutualKey].dataMap.set(
|
|
2212
|
+
payload.entityId,
|
|
2213
|
+
payload.data
|
|
2214
|
+
);
|
|
2215
|
+
})
|
|
2216
|
+
);
|
|
2217
|
+
}
|
|
2218
|
+
} else if (msg.type === "mutual.deleted") {
|
|
2219
|
+
monoriseStore.setState(
|
|
2220
|
+
produce5((state) => {
|
|
2221
|
+
var _a;
|
|
2222
|
+
(_a = state.mutual[mutualKey]) == null ? void 0 : _a.dataMap.delete(payload.entityId);
|
|
2223
|
+
})
|
|
2224
|
+
);
|
|
2225
|
+
}
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
);
|
|
2229
|
+
return () => {
|
|
2230
|
+
globalWsManager == null ? void 0 : globalWsManager.unsubscribeMutualType(subKey);
|
|
2231
|
+
unsubscribeState();
|
|
2232
|
+
unsubscribeMessage();
|
|
2233
|
+
setIsSubscribed(false);
|
|
2234
|
+
};
|
|
2235
|
+
}, [byEntityType, byEntityId2, mutualEntityType, mutualKey]);
|
|
2236
|
+
const fetchMore = async () => {
|
|
2237
|
+
if (!hasMore || isFetchingMore) return;
|
|
2238
|
+
await fetchData("more");
|
|
2239
|
+
};
|
|
2240
|
+
return {
|
|
2241
|
+
mutuals,
|
|
2242
|
+
isLoading,
|
|
2243
|
+
isFetchingMore,
|
|
2244
|
+
isSubscribed,
|
|
2245
|
+
error,
|
|
2246
|
+
isRefreshing,
|
|
2247
|
+
fetchMore,
|
|
2248
|
+
hasMore
|
|
2249
|
+
};
|
|
2250
|
+
};
|
|
2251
|
+
const useEphemeralSocket2 = (channel, opts) => {
|
|
2252
|
+
const [isSubscribed, setIsSubscribed] = useState3(false);
|
|
2253
|
+
useEffect3(() => {
|
|
2254
|
+
if (!globalWsManager || !channel) {
|
|
2255
|
+
setIsSubscribed(false);
|
|
2256
|
+
return;
|
|
2257
|
+
}
|
|
2258
|
+
const subKey = globalWsManager.subscribeEphemeral(channel);
|
|
2259
|
+
setIsSubscribed(true);
|
|
2260
|
+
const unsubscribeMessage = globalWsManager.onMessage(
|
|
2261
|
+
(msg) => {
|
|
2262
|
+
var _a;
|
|
2263
|
+
if (msg.type === "ephemeral") {
|
|
2264
|
+
const payload = msg.payload;
|
|
2265
|
+
if (payload.channel !== channel) return;
|
|
2266
|
+
(_a = opts == null ? void 0 : opts.onMessage) == null ? void 0 : _a.call(opts, payload.data, payload.senderId);
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
2269
|
+
);
|
|
2270
|
+
return () => {
|
|
2271
|
+
globalWsManager == null ? void 0 : globalWsManager.unsubscribeEphemeral(subKey);
|
|
2272
|
+
unsubscribeMessage();
|
|
2273
|
+
setIsSubscribed(false);
|
|
2274
|
+
};
|
|
2275
|
+
}, [channel]);
|
|
2276
|
+
const send = useCallback(
|
|
2277
|
+
(data) => {
|
|
2278
|
+
if (!globalWsManager || !channel) return;
|
|
2279
|
+
globalWsManager.sendEphemeral(channel, data);
|
|
2280
|
+
},
|
|
2281
|
+
[channel]
|
|
2282
|
+
);
|
|
2283
|
+
return { isSubscribed, send };
|
|
2284
|
+
};
|
|
2285
|
+
const useEntityFeed2 = (opts) => {
|
|
2286
|
+
const { entityType, entityId, ticketEndpoint } = opts;
|
|
2287
|
+
const [isConnected, setIsConnected] = useState3(false);
|
|
2288
|
+
const [error, setError2] = useState3(null);
|
|
2289
|
+
const refreshTimerRef = useRef(null);
|
|
2290
|
+
const hasConnectedOnceRef = useRef(false);
|
|
2291
|
+
const resyncFromHttp = async () => {
|
|
2292
|
+
var _a, _b;
|
|
2293
|
+
if (!httpActions) return;
|
|
2294
|
+
const snapshot = monoriseStore.getState();
|
|
2295
|
+
const keys = Object.keys((_a = snapshot.mutual) != null ? _a : {});
|
|
2296
|
+
for (const key of keys) {
|
|
2297
|
+
const parsed = parseMutualStateKey(key);
|
|
2298
|
+
if (parsed.entityId) continue;
|
|
2299
|
+
if (parsed.byEntityId !== entityId) continue;
|
|
2300
|
+
if (!((_b = snapshot.mutual[key]) == null ? void 0 : _b.isFirstFetched)) continue;
|
|
2301
|
+
try {
|
|
2302
|
+
const result = await httpActions.listEntitiesByEntity(
|
|
2303
|
+
parsed.byEntity,
|
|
2304
|
+
parsed.byEntityId,
|
|
2305
|
+
parsed.entity
|
|
2306
|
+
);
|
|
2307
|
+
if (!isMountedRef.current) return;
|
|
2308
|
+
monoriseStore.setState(
|
|
2309
|
+
produce5((draft) => {
|
|
2310
|
+
const slice = draft.mutual[key];
|
|
2311
|
+
if (!slice) return;
|
|
2312
|
+
slice.dataMap.clear();
|
|
2313
|
+
for (const mutual of result.entities) {
|
|
2314
|
+
slice.dataMap.set(mutual.entityId, mutual);
|
|
2315
|
+
}
|
|
2316
|
+
slice.lastKey = result.lastKey;
|
|
2317
|
+
})
|
|
2318
|
+
);
|
|
2319
|
+
} catch (e) {
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
};
|
|
2323
|
+
const wsManagerRef = useRef(null);
|
|
2324
|
+
const isMountedRef = useRef(true);
|
|
2325
|
+
const connectingRef = useRef(false);
|
|
2326
|
+
const reconnectCountRef = useRef(0);
|
|
2327
|
+
const connectRef = useRef(() => {
|
|
2328
|
+
});
|
|
2329
|
+
const lastConnectedAtRef = useRef(0);
|
|
2330
|
+
const fetchTicket = useCallback(async () => {
|
|
2331
|
+
try {
|
|
2332
|
+
const url = ticketEndpoint || `/api/core/ws/ticket/${entityType}/${entityId}`;
|
|
2333
|
+
const fetchOpts = {
|
|
2334
|
+
method: "POST",
|
|
2335
|
+
headers: { "Content-Type": "application/json" }
|
|
2336
|
+
};
|
|
2337
|
+
if (ticketEndpoint) {
|
|
2338
|
+
fetchOpts.body = JSON.stringify({ entityType, entityId });
|
|
2339
|
+
}
|
|
2340
|
+
const response = await fetch(url, fetchOpts);
|
|
2341
|
+
if (response.status === 401 || response.status === 403) {
|
|
2342
|
+
return { error: { code: "TICKET_UNAUTHORIZED", message: "Unauthorized" } };
|
|
2343
|
+
}
|
|
2344
|
+
if (!response.ok) {
|
|
2345
|
+
return { error: { code: "TICKET_FAILED", message: `HTTP ${response.status}` } };
|
|
2346
|
+
}
|
|
2347
|
+
const data = await response.json();
|
|
2348
|
+
return { data };
|
|
2349
|
+
} catch (err) {
|
|
2350
|
+
return { error: { code: "TICKET_FAILED", message: err.message } };
|
|
2351
|
+
}
|
|
2352
|
+
}, [entityType, entityId, ticketEndpoint]);
|
|
2353
|
+
const connectWithTicket = useCallback(async () => {
|
|
2354
|
+
if (connectingRef.current) return;
|
|
2355
|
+
connectingRef.current = true;
|
|
2356
|
+
let result = null;
|
|
2357
|
+
const maxRetries = 3;
|
|
2358
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
2359
|
+
result = await fetchTicket();
|
|
2360
|
+
if (!isMountedRef.current) {
|
|
2361
|
+
connectingRef.current = false;
|
|
2362
|
+
return;
|
|
2363
|
+
}
|
|
2364
|
+
if (!result.error) break;
|
|
2365
|
+
if (result.error.code === "TICKET_UNAUTHORIZED") break;
|
|
2366
|
+
if (attempt < maxRetries) {
|
|
2367
|
+
await new Promise((r) => setTimeout(r, 1e3 * Math.pow(2, attempt)));
|
|
2368
|
+
if (!isMountedRef.current) {
|
|
2369
|
+
connectingRef.current = false;
|
|
2370
|
+
return;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
if (result.error) {
|
|
2375
|
+
setError2(result.error);
|
|
2376
|
+
setIsConnected(false);
|
|
2377
|
+
connectingRef.current = false;
|
|
2378
|
+
return;
|
|
2379
|
+
}
|
|
2380
|
+
setError2(null);
|
|
2381
|
+
const { ticket, wsUrl, expiresIn } = result.data;
|
|
2382
|
+
if (wsManagerRef.current) {
|
|
2383
|
+
wsManagerRef.current.disconnect();
|
|
2384
|
+
}
|
|
2385
|
+
const { WebSocketManager: WsManagerClass } = await import("./websocket-34W2HQLH.js");
|
|
2386
|
+
const wsUrlWithTicket = `${wsUrl}?ticket=${encodeURIComponent(ticket)}`;
|
|
2387
|
+
const manager = new WsManagerClass(wsUrlWithTicket, "");
|
|
2388
|
+
manager.disableAutoReconnect = true;
|
|
2389
|
+
wsManagerRef.current = manager;
|
|
2390
|
+
manager.onStateChange((state) => {
|
|
2391
|
+
if (!isMountedRef.current) return;
|
|
2392
|
+
if (wsManagerRef.current !== manager) return;
|
|
2393
|
+
if (state === "connected") {
|
|
2394
|
+
connectingRef.current = false;
|
|
2395
|
+
reconnectCountRef.current = 0;
|
|
2396
|
+
lastConnectedAtRef.current = Date.now();
|
|
2397
|
+
setIsConnected(true);
|
|
2398
|
+
setError2(null);
|
|
2399
|
+
if (hasConnectedOnceRef.current) {
|
|
2400
|
+
void resyncFromHttp();
|
|
2401
|
+
}
|
|
2402
|
+
hasConnectedOnceRef.current = true;
|
|
2403
|
+
} else if (state === "disconnected") {
|
|
2404
|
+
connectingRef.current = false;
|
|
2405
|
+
setIsConnected(false);
|
|
2406
|
+
const wasStable = hasConnectedOnceRef.current && Date.now() - lastConnectedAtRef.current > 5e3;
|
|
2407
|
+
if (wasStable && reconnectCountRef.current < 5) {
|
|
2408
|
+
reconnectCountRef.current++;
|
|
2409
|
+
const delay = 2e3 * Math.pow(2, reconnectCountRef.current - 1);
|
|
2410
|
+
setTimeout(() => {
|
|
2411
|
+
if (isMountedRef.current && !connectingRef.current) {
|
|
2412
|
+
connectRef.current();
|
|
2413
|
+
}
|
|
2414
|
+
}, delay);
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
});
|
|
2418
|
+
manager.onMessage((msg) => {
|
|
2419
|
+
if (!isMountedRef.current) return;
|
|
2420
|
+
if (msg.type === "entity.created" || msg.type === "entity.updated") {
|
|
2421
|
+
const payload = msg.payload;
|
|
2422
|
+
if (payload.data) {
|
|
2423
|
+
monoriseStore.setState(
|
|
2424
|
+
produce5((state) => {
|
|
2425
|
+
const key = payload.entityType;
|
|
2426
|
+
if (!state.entity[key]) {
|
|
2427
|
+
state.entity[key] = { dataMap: /* @__PURE__ */ new Map(), isFirstFetched: false, lastKey: void 0 };
|
|
2428
|
+
}
|
|
2429
|
+
state.entity[key].dataMap.set(payload.entityId, payload.data);
|
|
2430
|
+
})
|
|
2431
|
+
);
|
|
2432
|
+
}
|
|
2433
|
+
} else if (msg.type === "entity.deleted") {
|
|
2434
|
+
const payload = msg.payload;
|
|
2435
|
+
monoriseStore.setState(
|
|
2436
|
+
produce5((state) => {
|
|
2437
|
+
var _a, _b;
|
|
2438
|
+
(_b = (_a = state.entity[payload.entityType]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.delete(payload.entityId);
|
|
2439
|
+
})
|
|
2440
|
+
);
|
|
2441
|
+
} else if (msg.type === "mutual.created" || msg.type === "mutual.updated") {
|
|
2442
|
+
const payload = msg.payload;
|
|
2443
|
+
if (payload.data) {
|
|
2444
|
+
const mutualKey = `${payload.byEntityType}/${payload.byEntityId}/${payload.mutualEntityType}`;
|
|
2445
|
+
monoriseStore.setState(
|
|
2446
|
+
produce5((state) => {
|
|
2447
|
+
if (!state.mutual[mutualKey]) {
|
|
2448
|
+
state.mutual[mutualKey] = { dataMap: /* @__PURE__ */ new Map(), isFirstFetched: false, lastKey: void 0 };
|
|
2449
|
+
}
|
|
2450
|
+
state.mutual[mutualKey].dataMap.set(payload.entityId, payload.data);
|
|
2451
|
+
})
|
|
2452
|
+
);
|
|
2453
|
+
}
|
|
2454
|
+
} else if (msg.type === "mutual.deleted") {
|
|
2455
|
+
const payload = msg.payload;
|
|
2456
|
+
const mutualKey = `${payload.byEntityType}/${payload.byEntityId}/${payload.mutualEntityType}`;
|
|
2457
|
+
monoriseStore.setState(
|
|
2458
|
+
produce5((state) => {
|
|
2459
|
+
var _a, _b;
|
|
2460
|
+
(_b = (_a = state.mutual[mutualKey]) == null ? void 0 : _a.dataMap) == null ? void 0 : _b.delete(payload.entityId);
|
|
2461
|
+
})
|
|
2462
|
+
);
|
|
2463
|
+
}
|
|
2464
|
+
});
|
|
2465
|
+
manager.connect();
|
|
2466
|
+
if (refreshTimerRef.current) {
|
|
2467
|
+
clearTimeout(refreshTimerRef.current);
|
|
2468
|
+
}
|
|
2469
|
+
const refreshMs = Math.max((expiresIn - 120) * 1e3, 6e4);
|
|
2470
|
+
refreshTimerRef.current = setTimeout(() => {
|
|
2471
|
+
if (isMountedRef.current) {
|
|
2472
|
+
connectRef.current();
|
|
2473
|
+
}
|
|
2474
|
+
}, refreshMs);
|
|
2475
|
+
}, [fetchTicket]);
|
|
2476
|
+
connectRef.current = connectWithTicket;
|
|
2477
|
+
useEffect3(() => {
|
|
2478
|
+
isMountedRef.current = true;
|
|
2479
|
+
if (entityId) {
|
|
2480
|
+
connectRef.current();
|
|
2481
|
+
}
|
|
2482
|
+
let lastCheckTime = Date.now();
|
|
2483
|
+
const sleepCheckInterval = setInterval(() => {
|
|
2484
|
+
const now = Date.now();
|
|
2485
|
+
const elapsed = now - lastCheckTime;
|
|
2486
|
+
lastCheckTime = now;
|
|
2487
|
+
if (elapsed > 3e4 && wsManagerRef.current) {
|
|
2488
|
+
wsManagerRef.current.disconnect();
|
|
2489
|
+
wsManagerRef.current = null;
|
|
2490
|
+
connectingRef.current = false;
|
|
2491
|
+
reconnectCountRef.current = 0;
|
|
2492
|
+
connectRef.current();
|
|
2493
|
+
}
|
|
2494
|
+
}, 5e3);
|
|
2495
|
+
const handleOnline = () => {
|
|
2496
|
+
var _a;
|
|
2497
|
+
if (!isMountedRef.current) return;
|
|
2498
|
+
if (((_a = wsManagerRef.current) == null ? void 0 : _a.getState()) === "connected") return;
|
|
2499
|
+
if (connectingRef.current) return;
|
|
2500
|
+
reconnectCountRef.current = 0;
|
|
2501
|
+
connectRef.current();
|
|
2502
|
+
};
|
|
2503
|
+
window.addEventListener("online", handleOnline);
|
|
2504
|
+
return () => {
|
|
2505
|
+
isMountedRef.current = false;
|
|
2506
|
+
clearInterval(sleepCheckInterval);
|
|
2507
|
+
window.removeEventListener("online", handleOnline);
|
|
2508
|
+
if (refreshTimerRef.current) {
|
|
2509
|
+
clearTimeout(refreshTimerRef.current);
|
|
2510
|
+
}
|
|
2511
|
+
if (wsManagerRef.current) {
|
|
2512
|
+
wsManagerRef.current.disconnect();
|
|
2513
|
+
wsManagerRef.current = null;
|
|
2514
|
+
}
|
|
2515
|
+
};
|
|
2516
|
+
}, [entityType, entityId]);
|
|
2517
|
+
return { isConnected, error };
|
|
2518
|
+
};
|
|
2519
|
+
return {
|
|
2520
|
+
useEntitySocket: useEntitySocket2,
|
|
2521
|
+
useMutualSocket: useMutualSocket2,
|
|
2522
|
+
useEphemeralSocket: useEphemeralSocket2,
|
|
2523
|
+
useEntityFeed: useEntityFeed2
|
|
2524
|
+
};
|
|
2525
|
+
};
|
|
2526
|
+
|
|
1821
2527
|
// lib/api.ts
|
|
1822
2528
|
import request from "axios";
|
|
1823
2529
|
var initAxiosInterceptor = (store2, appActions) => {
|
|
@@ -2083,14 +2789,22 @@ var initCoreService = (monoriseStore, axios2, opts) => {
|
|
|
2083
2789
|
}
|
|
2084
2790
|
);
|
|
2085
2791
|
};
|
|
2086
|
-
const
|
|
2792
|
+
const executeTransaction2 = (operations, opts2 = {}) => {
|
|
2087
2793
|
var _a;
|
|
2088
2794
|
const { transactionApiBaseUrl = TRANSACTION_API_BASE_URL } = options;
|
|
2089
2795
|
return axios2.post(
|
|
2090
2796
|
opts2.customUrl || transactionApiBaseUrl,
|
|
2091
2797
|
{ operations },
|
|
2092
2798
|
{
|
|
2093
|
-
requestKey
|
|
2799
|
+
// A hardcoded requestKey would collide across concurrent transaction
|
|
2800
|
+
// calls, sharing one loading/error slot. Using a single operation's
|
|
2801
|
+
// own key (e.g. operations[0]'s) would collide the OTHER way — with
|
|
2802
|
+
// a standalone editEntity/createEntity call on that same target,
|
|
2803
|
+
// via lib/api.ts's `ongoingRequests` dedupe, which would hand either
|
|
2804
|
+
// caller the other's differently-shaped response. The joined key is
|
|
2805
|
+
// still deterministic (identical operation sets dedupe onto one
|
|
2806
|
+
// request) but namespaced so it can never match a single-entity key.
|
|
2807
|
+
requestKey: opts2.requestKey || getTransactionCallRequestKey(operations),
|
|
2094
2808
|
isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
|
|
2095
2809
|
feedback: __spreadValues({
|
|
2096
2810
|
loading: "Processing transaction",
|
|
@@ -2242,7 +2956,9 @@ var initCoreService = (monoriseStore, axios2, opts) => {
|
|
|
2242
2956
|
return {
|
|
2243
2957
|
makeEntityService,
|
|
2244
2958
|
makeMutualService,
|
|
2245
|
-
|
|
2959
|
+
executeTransaction: executeTransaction2,
|
|
2960
|
+
/** @deprecated use `executeTransaction` — kept as an alias since `coreService` is a public export and this method already existed under this name. */
|
|
2961
|
+
transaction: executeTransaction2,
|
|
2246
2962
|
setOptions
|
|
2247
2963
|
};
|
|
2248
2964
|
};
|
|
@@ -2472,41 +3188,6 @@ var initMonoriseStore = () => {
|
|
|
2472
3188
|
return { setOptions, monoriseStore };
|
|
2473
3189
|
};
|
|
2474
3190
|
|
|
2475
|
-
// helpers/transactional.ts
|
|
2476
|
-
var transactional = {
|
|
2477
|
-
createEntity: (entityType, payload) => {
|
|
2478
|
-
const _a = payload, { entityId } = _a, rest = __objRest(_a, ["entityId"]);
|
|
2479
|
-
return __spreadValues({
|
|
2480
|
-
operation: "createEntity",
|
|
2481
|
-
entityType,
|
|
2482
|
-
payload: rest
|
|
2483
|
-
}, entityId !== void 0 && { entityId });
|
|
2484
|
-
},
|
|
2485
|
-
updateEntity: (entityType, entityId, payload) => {
|
|
2486
|
-
const _a = payload, { $condition } = _a, rest = __objRest(_a, ["$condition"]);
|
|
2487
|
-
return __spreadValues({
|
|
2488
|
-
operation: "updateEntity",
|
|
2489
|
-
entityType,
|
|
2490
|
-
entityId,
|
|
2491
|
-
payload: rest
|
|
2492
|
-
}, $condition !== void 0 && { condition: $condition });
|
|
2493
|
-
},
|
|
2494
|
-
adjustEntity: (entityType, entityId, adjustments) => {
|
|
2495
|
-
const _a = adjustments, { $condition } = _a, rest = __objRest(_a, ["$condition"]);
|
|
2496
|
-
return __spreadValues({
|
|
2497
|
-
operation: "adjustEntity",
|
|
2498
|
-
entityType,
|
|
2499
|
-
entityId,
|
|
2500
|
-
adjustments: rest
|
|
2501
|
-
}, $condition !== void 0 && { condition: $condition });
|
|
2502
|
-
},
|
|
2503
|
-
deleteEntity: (entityType, entityId) => ({
|
|
2504
|
-
operation: "deleteEntity",
|
|
2505
|
-
entityType,
|
|
2506
|
-
entityId
|
|
2507
|
-
})
|
|
2508
|
-
};
|
|
2509
|
-
|
|
2510
3191
|
// index.ts
|
|
2511
3192
|
var initMonorise = () => {
|
|
2512
3193
|
const { monoriseStore: store2, setOptions: setMonoriseOptions } = initMonoriseStore();
|
|
@@ -2518,6 +3199,19 @@ var initMonorise = () => {
|
|
|
2518
3199
|
const coreService2 = core_service_default(store2, axios2);
|
|
2519
3200
|
const authActions = initAuthActions(store2, authService2);
|
|
2520
3201
|
const coreActions = initCoreActions(store2, appActions, coreService2);
|
|
3202
|
+
const websocketActions = initWebSocketActions(store2, {
|
|
3203
|
+
listEntities: async (entityType, params) => {
|
|
3204
|
+
const { data } = await coreService2.makeEntityService(entityType).listEntities({ limit: params == null ? void 0 : params.limit, lastKey: params == null ? void 0 : params.lastKey });
|
|
3205
|
+
return { data: data.data, lastKey: data.lastKey };
|
|
3206
|
+
},
|
|
3207
|
+
listEntitiesByEntity: async (byEntityType, byEntityId2, entityType, params) => {
|
|
3208
|
+
const { data } = await coreService2.makeMutualService(byEntityType, entityType).listEntitiesByEntity(byEntityId2, {
|
|
3209
|
+
limit: params == null ? void 0 : params.limit,
|
|
3210
|
+
params: { lastKey: params == null ? void 0 : params.lastKey }
|
|
3211
|
+
});
|
|
3212
|
+
return { entities: data.entities, lastKey: data.lastKey };
|
|
3213
|
+
}
|
|
3214
|
+
});
|
|
2521
3215
|
const axiosInterceptor = injectAxiosInterceptor(
|
|
2522
3216
|
appActions,
|
|
2523
3217
|
authActions,
|
|
@@ -2543,14 +3237,14 @@ var initMonorise = () => {
|
|
|
2543
3237
|
onApiSuccess: opts.onApiSuccess
|
|
2544
3238
|
});
|
|
2545
3239
|
};
|
|
2546
|
-
return __spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
3240
|
+
return __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
2547
3241
|
config,
|
|
2548
3242
|
store: store2,
|
|
2549
3243
|
axios: axios2,
|
|
2550
3244
|
authService: authService2,
|
|
2551
3245
|
filestoreService: filestoreService2,
|
|
2552
3246
|
coreService: coreService2
|
|
2553
|
-
}, configActions), appActions), authActions), coreActions);
|
|
3247
|
+
}, configActions), appActions), authActions), coreActions), websocketActions);
|
|
2554
3248
|
};
|
|
2555
3249
|
var Monorise = initMonorise();
|
|
2556
3250
|
var {
|
|
@@ -2586,6 +3280,7 @@ var {
|
|
|
2586
3280
|
adjustEntity,
|
|
2587
3281
|
updateLocalEntity,
|
|
2588
3282
|
deleteEntity,
|
|
3283
|
+
executeTransaction,
|
|
2589
3284
|
getMutual,
|
|
2590
3285
|
createMutual,
|
|
2591
3286
|
createLocalMutual,
|
|
@@ -2602,10 +3297,15 @@ var {
|
|
|
2602
3297
|
useEntityState,
|
|
2603
3298
|
getEntity,
|
|
2604
3299
|
updateLocalTaggedEntity,
|
|
2605
|
-
deleteLocalTaggedEntity
|
|
3300
|
+
deleteLocalTaggedEntity,
|
|
3301
|
+
useEntitySocket,
|
|
3302
|
+
useMutualSocket,
|
|
3303
|
+
useEphemeralSocket,
|
|
3304
|
+
useEntityFeed
|
|
2606
3305
|
} = Monorise;
|
|
2607
3306
|
var index_default = Monorise;
|
|
2608
3307
|
export {
|
|
3308
|
+
WebSocketManager,
|
|
2609
3309
|
adjustEntity,
|
|
2610
3310
|
authService,
|
|
2611
3311
|
axios,
|
|
@@ -2623,6 +3323,7 @@ export {
|
|
|
2623
3323
|
editEntity,
|
|
2624
3324
|
editMutual,
|
|
2625
3325
|
endLoading,
|
|
3326
|
+
executeTransaction,
|
|
2626
3327
|
filestoreService,
|
|
2627
3328
|
getConfig,
|
|
2628
3329
|
getEntity,
|
|
@@ -2633,6 +3334,8 @@ export {
|
|
|
2633
3334
|
getProfile,
|
|
2634
3335
|
getTagRequestKey,
|
|
2635
3336
|
getUniqueFieldRequestKey,
|
|
3337
|
+
getWebSocketManager,
|
|
3338
|
+
initializeWebSocketManager,
|
|
2636
3339
|
listMoreEntities,
|
|
2637
3340
|
logout,
|
|
2638
3341
|
openModal,
|
|
@@ -2651,13 +3354,17 @@ export {
|
|
|
2651
3354
|
useEntities,
|
|
2652
3355
|
useEntity,
|
|
2653
3356
|
useEntityByUniqueField,
|
|
3357
|
+
useEntityFeed,
|
|
3358
|
+
useEntitySocket,
|
|
2654
3359
|
useEntityState,
|
|
3360
|
+
useEphemeralSocket,
|
|
2655
3361
|
useErrorStore,
|
|
2656
3362
|
useInterruptiveLoadStore,
|
|
2657
3363
|
useIsUnauthorized,
|
|
2658
3364
|
useLoadStore,
|
|
2659
3365
|
useModalStore,
|
|
2660
3366
|
useMutual,
|
|
3367
|
+
useMutualSocket,
|
|
2661
3368
|
useMutuals,
|
|
2662
3369
|
useProfile,
|
|
2663
3370
|
useTaggedEntities
|