@absolutejs/sync 1.19.0 → 1.20.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/testing.js CHANGED
@@ -625,6 +625,15 @@ class CdcConsumerSlowError extends Error {
625
625
  this.lastDeliveredVersion = lastDeliveredVersion;
626
626
  }
627
627
  }
628
+
629
+ class MutationQueueOverflowError extends Error {
630
+ queueLimit;
631
+ constructor(queueLimit) {
632
+ super(`Mutation queue overflowed (limit ${queueLimit}); the engine is at ` + `its mutationConcurrency cap and the waiting queue is full. ` + `Retry later or shed load at the gateway.`);
633
+ this.name = "MutationQueueOverflowError";
634
+ this.queueLimit = queueLimit;
635
+ }
636
+ }
628
637
  var defaultKey = (row) => row.id;
629
638
  var shallowEqual3 = (a, b) => {
630
639
  if (a === b) {
@@ -741,6 +750,40 @@ var createSyncEngine = (options = {}) => {
741
750
  let mutationsFailed = 0;
742
751
  let mutationsRetried = 0;
743
752
  let mutationsInFlight = 0;
753
+ const mutationWaiters = [];
754
+ let mutationsQueued = 0;
755
+ const acquireMutationSlot = async () => {
756
+ const limit = options.mutationConcurrency;
757
+ if (limit === undefined) {
758
+ mutationsInFlight += 1;
759
+ return;
760
+ }
761
+ if (mutationsInFlight < limit && mutationWaiters.length === 0) {
762
+ mutationsInFlight += 1;
763
+ return;
764
+ }
765
+ const queueLimit = options.mutationQueueLimit;
766
+ if (queueLimit !== undefined && mutationsQueued >= queueLimit) {
767
+ throw new MutationQueueOverflowError(queueLimit);
768
+ }
769
+ mutationsQueued += 1;
770
+ try {
771
+ await new Promise((resolve) => {
772
+ mutationWaiters.push(resolve);
773
+ });
774
+ } finally {
775
+ mutationsQueued -= 1;
776
+ }
777
+ mutationsInFlight += 1;
778
+ };
779
+ const releaseMutationSlot = () => {
780
+ mutationsInFlight -= 1;
781
+ if (options.mutationConcurrency === undefined)
782
+ return;
783
+ const next = mutationWaiters.shift();
784
+ if (next !== undefined)
785
+ next();
786
+ };
744
787
  const reactiveCacheMax = options.reactiveCache?.max ?? 256;
745
788
  const reactiveCacheTtlMs = options.reactiveCache?.ttlMs ?? 60000;
746
789
  const cachedReruns = new Map;
@@ -1768,6 +1811,7 @@ var createSyncEngine = (options = {}) => {
1768
1811
  throw new UnauthorizedError(`run mutation "${name}"`);
1769
1812
  }
1770
1813
  }
1814
+ await acquireMutationSlot();
1771
1815
  const sandboxRunner = sandboxRunners.get(name);
1772
1816
  const invokeHandler = sandboxRunner !== undefined ? sandboxRunner : (a, c, actions) => Promise.resolve(mutation.handler(a, c, actions));
1773
1817
  const runHandler = async (tx) => {
@@ -1783,7 +1827,6 @@ var createSyncEngine = (options = {}) => {
1783
1827
  const startedAt = Date.now();
1784
1828
  let lastError;
1785
1829
  let attemptsMade = 0;
1786
- mutationsInFlight += 1;
1787
1830
  try {
1788
1831
  for (let attempt = 1;attempt <= maxAttempts; attempt++) {
1789
1832
  attemptsMade = attempt;
@@ -1836,7 +1879,7 @@ var createSyncEngine = (options = {}) => {
1836
1879
  }
1837
1880
  throw lastError;
1838
1881
  } finally {
1839
- mutationsInFlight -= 1;
1882
+ releaseMutationSlot();
1840
1883
  }
1841
1884
  },
1842
1885
  runMutations: async (specs, ctx) => {
@@ -1849,6 +1892,7 @@ var createSyncEngine = (options = {}) => {
1849
1892
  }
1850
1893
  return { args: spec.args, mutation, name: spec.name };
1851
1894
  });
1895
+ await acquireMutationSlot();
1852
1896
  const runBatch = async (tx) => {
1853
1897
  const results = [];
1854
1898
  const accumulated = [];
@@ -1886,6 +1930,8 @@ var createSyncEngine = (options = {}) => {
1886
1930
  status: "error"
1887
1931
  });
1888
1932
  throw error;
1933
+ } finally {
1934
+ releaseMutationSlot();
1889
1935
  }
1890
1936
  },
1891
1937
  registerSchedule: (schedule) => {
@@ -2105,6 +2151,7 @@ var createSyncEngine = (options = {}) => {
2105
2151
  completed: mutationsCompleted,
2106
2152
  failed: mutationsFailed,
2107
2153
  inFlight: mutationsInFlight,
2154
+ queued: mutationsQueued,
2108
2155
  retried: mutationsRetried
2109
2156
  },
2110
2157
  reactiveCache: {
@@ -2225,5 +2272,5 @@ export {
2225
2272
  createTestEngine
2226
2273
  };
2227
2274
 
2228
- //# debugId=D332A9D90117FA7264756E2164756E21
2275
+ //# debugId=261DFADB673FBB5664756E2164756E21
2229
2276
  //# sourceMappingURL=testing.js.map