@monorise/react 8.0.0 → 8.1.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
@@ -47,7 +47,9 @@ var initAppActions = (store2) => {
47
47
  const stackType = isInterruptive ? "intLoadStack" : "loadStack";
48
48
  const requestKeyCount = state.app[stackType].get(requestKey);
49
49
  state.app.loadingMessage = typeof message === "string" ? message : "Loading";
50
- state.app.ongoingRequests.set(requestKey, request2);
50
+ if (request2) {
51
+ state.app.ongoingRequests.set(requestKey, request2);
52
+ }
51
53
  state.app[stackType].set(
52
54
  requestKey,
53
55
  requestKeyCount ? requestKeyCount + 1 : 1
@@ -57,13 +59,17 @@ var initAppActions = (store2) => {
57
59
  `mr/start-loading/${requestKey}`
58
60
  );
59
61
  };
60
- const endLoading2 = ({ requestKey, isInterruptive }) => {
62
+ const endLoading2 = ({
63
+ requestKey,
64
+ isInterruptive,
65
+ skipDedupe
66
+ }) => {
61
67
  setTimeout(() => {
62
68
  store2.setState(
63
69
  produce((state) => {
64
70
  const stackType = isInterruptive ? "intLoadStack" : "loadStack";
65
71
  const requestKeyCount = state.app[stackType].get(requestKey);
66
- if (state.app.ongoingRequests.has(requestKey)) {
72
+ if (!skipDedupe && state.app.ongoingRequests.has(requestKey)) {
67
73
  state.app.ongoingRequests.delete(requestKey);
68
74
  }
69
75
  if (typeof requestKeyCount === "number" && requestKeyCount > 1) {
@@ -373,6 +379,54 @@ var getUniqueFieldRequestKey = (entityType, fieldName, value) => {
373
379
  return `entity/${entityType}/unique/${getUniqueFieldStateKey(fieldName, value)}`;
374
380
  };
375
381
 
382
+ // helpers/transactional.ts
383
+ var getTransactionOperationRequestKey = (op) => {
384
+ switch (op.operation) {
385
+ case "createEntity":
386
+ return getEntityRequestKey("create", op.entityType);
387
+ case "updateEntity":
388
+ return getEntityRequestKey("edit", op.entityType, op.entityId);
389
+ case "adjustEntity":
390
+ return getEntityRequestKey("adjust", op.entityType, op.entityId);
391
+ case "deleteEntity":
392
+ return getEntityRequestKey("delete", op.entityType, op.entityId);
393
+ }
394
+ };
395
+ var getTransactionCallRequestKey = (operations) => `transaction/${operations.map(getTransactionOperationRequestKey).join("|")}`;
396
+ var transactional = {
397
+ createEntity: (entityType, payload) => {
398
+ const _a = payload, { entityId } = _a, rest = __objRest(_a, ["entityId"]);
399
+ return __spreadValues({
400
+ operation: "createEntity",
401
+ entityType,
402
+ payload: rest
403
+ }, entityId !== void 0 && { entityId });
404
+ },
405
+ updateEntity: (entityType, entityId, payload) => {
406
+ const _a = payload, { $condition } = _a, rest = __objRest(_a, ["$condition"]);
407
+ return __spreadValues({
408
+ operation: "updateEntity",
409
+ entityType,
410
+ entityId,
411
+ payload: rest
412
+ }, $condition !== void 0 && { condition: $condition });
413
+ },
414
+ adjustEntity: (entityType, entityId, adjustments) => {
415
+ const _a = adjustments, { $condition } = _a, rest = __objRest(_a, ["$condition"]);
416
+ return __spreadValues({
417
+ operation: "adjustEntity",
418
+ entityType,
419
+ entityId,
420
+ adjustments: rest
421
+ }, $condition !== void 0 && { condition: $condition });
422
+ },
423
+ deleteEntity: (entityType, entityId) => ({
424
+ operation: "deleteEntity",
425
+ entityType,
426
+ entityId
427
+ })
428
+ };
429
+
376
430
  // actions/core.action.ts
377
431
  var defaultOnError = (error) => {
378
432
  if (!(error instanceof Error) && error.status >= 500) {
@@ -381,7 +435,16 @@ var defaultOnError = (error) => {
381
435
  };
382
436
  var initCoreActions = (monoriseStore, appActions, coreService2) => {
383
437
  const { makeEntityService, makeMutualService } = coreService2;
384
- const { checkIsLoading, getError: getError2, useLoadStore: useLoadStore2, useErrorStore: useErrorStore2 } = appActions;
438
+ const {
439
+ checkIsLoading,
440
+ getError: getError2,
441
+ useLoadStore: useLoadStore2,
442
+ useErrorStore: useErrorStore2,
443
+ startLoading: startLoading2,
444
+ endLoading: endLoading2,
445
+ setError: setError2,
446
+ clearError: clearError2
447
+ } = appActions;
385
448
  const listEntities = async (entityType, params = { limit: 20 }, opts = {}) => {
386
449
  var _a, _b;
387
450
  const store2 = monoriseStore.getState();
@@ -651,6 +714,33 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
651
714
  return { error: error2 };
652
715
  }
653
716
  };
717
+ const populateMutualsForCreatedEntity = (state, entityType, data, payload) => {
718
+ var _a, _b;
719
+ const mutualFields = (_b = (_a = state.config[entityType]) == null ? void 0 : _a.mutual) == null ? void 0 : _b.mutualFields;
720
+ if (!mutualFields) return;
721
+ for (const [field, fieldConfig] of Object.entries(mutualFields)) {
722
+ const byEntityType = fieldConfig.entityType;
723
+ const ids = payload[field];
724
+ if (!Array.isArray(ids)) continue;
725
+ for (const byEntityId2 of ids) {
726
+ const mutualKey = getMutualStateKey(byEntityType, byEntityId2, entityType);
727
+ if (state.mutual[mutualKey] && state.mutual[mutualKey].isFirstFetched) {
728
+ state.mutual[mutualKey].dataMap.set(data.entityId, {
729
+ entityId: data.entityId,
730
+ entityType,
731
+ byEntityId: byEntityId2,
732
+ byEntityType,
733
+ mutualId: "",
734
+ data: data.data,
735
+ mutualData: {},
736
+ createdAt: data.createdAt,
737
+ updatedAt: data.updatedAt,
738
+ mutualUpdatedAt: data.updatedAt
739
+ });
740
+ }
741
+ }
742
+ }
743
+ };
654
744
  const createEntity2 = async (entityType, entity, opts = {}) => {
655
745
  var _a;
656
746
  const entityService = makeEntityService(entityType);
@@ -659,7 +749,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
659
749
  const { data } = await entityService.createEntity(entity, opts);
660
750
  monoriseStore.setState(
661
751
  produce4((state) => {
662
- var _a2, _b, _c, _d;
752
+ var _a2, _b;
663
753
  state.entity[entityType].dataMap.set(data.entityId, data);
664
754
  const tagConfigs = (_a2 = state.config[entityType]) == null ? void 0 : _a2.tags;
665
755
  if (tagConfigs) {
@@ -704,35 +794,12 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
704
794
  }
705
795
  }
706
796
  }
707
- const mutualFields = (_d = (_c = state.config[entityType]) == null ? void 0 : _c.mutual) == null ? void 0 : _d.mutualFields;
708
- if (mutualFields) {
709
- for (const [field, fieldConfig] of Object.entries(mutualFields)) {
710
- const byEntityType = fieldConfig.entityType;
711
- const ids = entity[field];
712
- if (!Array.isArray(ids)) continue;
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
- }
797
+ populateMutualsForCreatedEntity(
798
+ state,
799
+ entityType,
800
+ data,
801
+ entity
802
+ );
736
803
  }),
737
804
  void 0,
738
805
  `mr/entity/create/${entityType}`
@@ -819,6 +886,36 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
819
886
  }
820
887
  }
821
888
  };
889
+ const propagateEntityDataToMutuals = (state, entityType, entityId, data) => {
890
+ for (const key of Object.keys(state.mutual)) {
891
+ const { entity: mutualEntityType } = parseMutualStateKey(key);
892
+ if (mutualEntityType === entityType) {
893
+ const mutual = state.mutual[key].dataMap.get(entityId);
894
+ if (mutual) {
895
+ state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(
896
+ entityId,
897
+ __spreadProps(__spreadValues({}, mutual), { data })
898
+ );
899
+ }
900
+ }
901
+ }
902
+ };
903
+ const deleteEntityFromStores = (state, entityType, entityId) => {
904
+ var _a, _b, _c;
905
+ (_a = state.entity[entityType]) == null ? void 0 : _a.dataMap.delete(entityId);
906
+ for (const key of Object.keys(state.mutual)) {
907
+ const { entity: mutualEntityType } = parseMutualStateKey(key);
908
+ if (mutualEntityType === entityType) {
909
+ state.mutual[key].dataMap.delete(entityId);
910
+ }
911
+ }
912
+ for (const tagKey of Object.keys(state.tag)) {
913
+ const [tagEntityType] = tagKey.split("/");
914
+ if (tagEntityType === entityType) {
915
+ (_c = (_b = state.tag[tagKey]) == null ? void 0 : _b.dataMap) == null ? void 0 : _c.delete(entityId);
916
+ }
917
+ }
918
+ };
822
919
  const editEntity2 = async (entityType, id, entity, opts = {}) => {
823
920
  var _a;
824
921
  const entityService = makeEntityService(entityType);
@@ -828,17 +925,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
828
925
  monoriseStore.setState(
829
926
  produce4((state) => {
830
927
  state.entity[entityType].dataMap.set(data.entityId, data);
831
- for (const key of Object.keys(state.mutual)) {
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
- }
928
+ propagateEntityDataToMutuals(state, entityType, id, data.data);
842
929
  reconcileTaggedEntityStore(
843
930
  state,
844
931
  entityType,
@@ -865,17 +952,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
865
952
  monoriseStore.setState(
866
953
  produce4((state) => {
867
954
  state.entity[entityType].dataMap.set(data.entityId, data);
868
- for (const key of Object.keys(state.mutual)) {
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
- }
955
+ propagateEntityDataToMutuals(state, entityType, id, data.data);
879
956
  reconcileTaggedEntityStore(
880
957
  state,
881
958
  entityType,
@@ -905,20 +982,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
905
982
  deleteLocalMutualsByEntity(entityType, id);
906
983
  monoriseStore.setState(
907
984
  produce4((state) => {
908
- var _a2, _b;
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
- }
985
+ deleteEntityFromStores(state, entityType, id);
922
986
  }),
923
987
  void 0,
924
988
  `mr/entity/delete/${entityType}/${id}`
@@ -930,6 +994,103 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
930
994
  return { error };
931
995
  }
932
996
  };
997
+ const executeTransaction2 = async (operations, opts = {}) => {
998
+ var _a, _b, _c, _d;
999
+ const onError = (_a = opts.onError) != null ? _a : defaultOnError;
1000
+ const isInterruptive = (_b = opts.isInterruptive) != null ? _b : true;
1001
+ const opRequestKeys = operations.map(getTransactionOperationRequestKey);
1002
+ const callRequestKey = opts.requestKey || getTransactionCallRequestKey(operations);
1003
+ const callPromise = coreService2.executeTransaction(operations, __spreadProps(__spreadValues({}, opts), {
1004
+ requestKey: callRequestKey
1005
+ }));
1006
+ const loadingMessage = (_d = (_c = opts.feedback) == null ? void 0 : _c.loading) != null ? _d : "Processing transaction";
1007
+ for (const requestKey of opRequestKeys) {
1008
+ startLoading2({ requestKey, isInterruptive, message: loadingMessage });
1009
+ }
1010
+ let settled = false;
1011
+ try {
1012
+ const { data } = await callPromise;
1013
+ for (const requestKey of opRequestKeys) {
1014
+ endLoading2({ requestKey, isInterruptive, skipDedupe: true });
1015
+ clearError2(requestKey);
1016
+ }
1017
+ settled = true;
1018
+ const deletedEntities = [];
1019
+ monoriseStore.setState(
1020
+ produce4((state) => {
1021
+ data.results.forEach((entry, index) => {
1022
+ var _a2, _b2, _c2;
1023
+ const { entityType, entityId } = entry;
1024
+ if (entry.operation === "deleteEntity") {
1025
+ deleteEntityFromStores(state, entityType, entityId);
1026
+ deletedEntities.push({ entityType, entityId });
1027
+ return;
1028
+ }
1029
+ if (entry.operation === "createEntity") {
1030
+ const createdEntity = {
1031
+ entityId,
1032
+ entityType,
1033
+ data: (_a2 = entry.data) != null ? _a2 : {},
1034
+ createdAt: entry.createdAt,
1035
+ updatedAt: entry.updatedAt
1036
+ };
1037
+ state.entity[entityType].dataMap.set(entityId, createdEntity);
1038
+ const op = operations[index];
1039
+ populateMutualsForCreatedEntity(
1040
+ state,
1041
+ entityType,
1042
+ createdEntity,
1043
+ op == null ? void 0 : op.payload
1044
+ );
1045
+ reconcileTaggedEntityStore(
1046
+ state,
1047
+ entityType,
1048
+ entityId,
1049
+ createdEntity
1050
+ );
1051
+ return;
1052
+ }
1053
+ const existing = (_b2 = state.entity[entityType]) == null ? void 0 : _b2.dataMap.get(entityId);
1054
+ if (!existing && !entry.data) return;
1055
+ if (!entry.data) {
1056
+ if (entry.updatedAt) {
1057
+ state.entity[entityType].dataMap.set(entityId, __spreadProps(__spreadValues({}, existing), {
1058
+ updatedAt: entry.updatedAt
1059
+ }));
1060
+ }
1061
+ return;
1062
+ }
1063
+ const updated = __spreadProps(__spreadValues({}, existing != null ? existing : { entityId, entityType }), {
1064
+ data: entry.data,
1065
+ updatedAt: (_c2 = entry.updatedAt) != null ? _c2 : existing == null ? void 0 : existing.updatedAt
1066
+ });
1067
+ state.entity[entityType].dataMap.set(entityId, updated);
1068
+ propagateEntityDataToMutuals(state, entityType, entityId, updated.data);
1069
+ reconcileTaggedEntityStore(state, entityType, entityId, updated);
1070
+ });
1071
+ }),
1072
+ void 0,
1073
+ "mr/entity/execute-transaction"
1074
+ );
1075
+ for (const { entityType, entityId } of deletedEntities) {
1076
+ deleteLocalMutualsByEntity(entityType, entityId);
1077
+ }
1078
+ return { data };
1079
+ } catch (err) {
1080
+ if (!settled) {
1081
+ const callAppError = getError2(callRequestKey);
1082
+ for (const requestKey of opRequestKeys) {
1083
+ endLoading2({ requestKey, isInterruptive, skipDedupe: true });
1084
+ if (callAppError) {
1085
+ setError2({ requestKey, error: callAppError });
1086
+ }
1087
+ }
1088
+ }
1089
+ const error = err instanceof Error ? err : new Error("Unknown error occurred");
1090
+ onError(error);
1091
+ return { error };
1092
+ }
1093
+ };
933
1094
  const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
934
1095
  var _a, _b, _c;
935
1096
  const onError = (_a = opts.onError) != null ? _a : defaultOnError;
@@ -1798,6 +1959,7 @@ var initCoreActions = (monoriseStore, appActions, coreService2) => {
1798
1959
  editEntity: editEntity2,
1799
1960
  adjustEntity: adjustEntity2,
1800
1961
  deleteEntity: deleteEntity2,
1962
+ executeTransaction: executeTransaction2,
1801
1963
  getMutual: getMutual2,
1802
1964
  updateLocalEntity: updateLocalEntity2,
1803
1965
  createMutual: createMutual2,
@@ -2083,14 +2245,22 @@ var initCoreService = (monoriseStore, axios2, opts) => {
2083
2245
  }
2084
2246
  );
2085
2247
  };
2086
- const transaction = (operations, opts2 = {}) => {
2248
+ const executeTransaction2 = (operations, opts2 = {}) => {
2087
2249
  var _a;
2088
2250
  const { transactionApiBaseUrl = TRANSACTION_API_BASE_URL } = options;
2089
2251
  return axios2.post(
2090
2252
  opts2.customUrl || transactionApiBaseUrl,
2091
2253
  { operations },
2092
2254
  {
2093
- requestKey: "transaction",
2255
+ // A hardcoded requestKey would collide across concurrent transaction
2256
+ // calls, sharing one loading/error slot. Using a single operation's
2257
+ // own key (e.g. operations[0]'s) would collide the OTHER way — with
2258
+ // a standalone editEntity/createEntity call on that same target,
2259
+ // via lib/api.ts's `ongoingRequests` dedupe, which would hand either
2260
+ // caller the other's differently-shaped response. The joined key is
2261
+ // still deterministic (identical operation sets dedupe onto one
2262
+ // request) but namespaced so it can never match a single-entity key.
2263
+ requestKey: opts2.requestKey || getTransactionCallRequestKey(operations),
2094
2264
  isInterruptive: (_a = opts2.isInterruptive) != null ? _a : true,
2095
2265
  feedback: __spreadValues({
2096
2266
  loading: "Processing transaction",
@@ -2242,7 +2412,9 @@ var initCoreService = (monoriseStore, axios2, opts) => {
2242
2412
  return {
2243
2413
  makeEntityService,
2244
2414
  makeMutualService,
2245
- transaction,
2415
+ executeTransaction: executeTransaction2,
2416
+ /** @deprecated use `executeTransaction` — kept as an alias since `coreService` is a public export and this method already existed under this name. */
2417
+ transaction: executeTransaction2,
2246
2418
  setOptions
2247
2419
  };
2248
2420
  };
@@ -2472,41 +2644,6 @@ var initMonoriseStore = () => {
2472
2644
  return { setOptions, monoriseStore };
2473
2645
  };
2474
2646
 
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
2647
  // index.ts
2511
2648
  var initMonorise = () => {
2512
2649
  const { monoriseStore: store2, setOptions: setMonoriseOptions } = initMonoriseStore();
@@ -2586,6 +2723,7 @@ var {
2586
2723
  adjustEntity,
2587
2724
  updateLocalEntity,
2588
2725
  deleteEntity,
2726
+ executeTransaction,
2589
2727
  getMutual,
2590
2728
  createMutual,
2591
2729
  createLocalMutual,
@@ -2623,6 +2761,7 @@ export {
2623
2761
  editEntity,
2624
2762
  editMutual,
2625
2763
  endLoading,
2764
+ executeTransaction,
2626
2765
  filestoreService,
2627
2766
  getConfig,
2628
2767
  getEntity,