@web-ts-toolkit/access-router-react 0.42.2 → 0.44.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/index.js CHANGED
@@ -81,8 +81,41 @@ function requestConfigKeyInput(requestConfig) {
81
81
  if (!requestConfig) {
82
82
  return void 0;
83
83
  }
84
- const { signal: _signal, ...rest } = requestConfig;
85
- return Object.keys(rest).length === 0 ? void 0 : rest;
84
+ const rest = {};
85
+ let hasContent = false;
86
+ for (const k of Object.keys(requestConfig)) {
87
+ if (k === "signal") {
88
+ continue;
89
+ }
90
+ const desc = Object.getOwnPropertyDescriptor(requestConfig, k);
91
+ if (desc === void 0) {
92
+ throw new RequestKeyError(`requestKeyFor: property ${truncatedKeyLabel(k)} has no descriptor (request key).`);
93
+ }
94
+ if ("get" in desc || "set" in desc) {
95
+ throw new RequestKeyError(
96
+ `requestKeyFor: accessor property ${truncatedKeyLabel(k)} is not supported in request keys; getters/setters must not run during dep-key construction.`
97
+ );
98
+ }
99
+ rest[k] = desc.value;
100
+ hasContent = true;
101
+ }
102
+ for (const s of Object.getOwnPropertySymbols(requestConfig)) {
103
+ if (!Object.prototype.propertyIsEnumerable.call(requestConfig, s)) {
104
+ continue;
105
+ }
106
+ const desc = Object.getOwnPropertyDescriptor(requestConfig, s);
107
+ if (desc === void 0) {
108
+ throw new RequestKeyError("requestKeyFor: symbol-keyed property has no descriptor (request key).");
109
+ }
110
+ if ("get" in desc || "set" in desc) {
111
+ throw new RequestKeyError(
112
+ "requestKeyFor: accessor property (symbol key) is not supported in request keys; getters/setters must not run during dep-key construction."
113
+ );
114
+ }
115
+ rest[s] = desc.value;
116
+ hasContent = true;
117
+ }
118
+ return hasContent ? rest : void 0;
86
119
  }
87
120
  function mergeRequestConfig(requestConfig, signal) {
88
121
  if (signal === void 0) {
@@ -96,6 +129,9 @@ function useAbortManager() {
96
129
  ref.current?.abort();
97
130
  ref.current = next;
98
131
  }, []);
132
+ const abort = (0, import_react.useCallback)(() => {
133
+ ref.current?.abort();
134
+ }, []);
99
135
  (0, import_react.useEffect)(() => {
100
136
  return () => {
101
137
  ref.current?.abort();
@@ -104,7 +140,7 @@ function useAbortManager() {
104
140
  }, []);
105
141
  const managerRef = (0, import_react.useRef)(null);
106
142
  if (managerRef.current === null) {
107
- managerRef.current = { replace };
143
+ managerRef.current = { replace, abort };
108
144
  }
109
145
  return managerRef.current;
110
146
  }
@@ -117,42 +153,106 @@ var RequestKeyError = class extends Error {
117
153
  var REQUEST_KEY_MAX_DEPTH = 64;
118
154
  var REQUEST_KEY_MAX_NODES = 2e4;
119
155
  var REQUEST_KEY_MAX_OUTPUT_LENGTH = 2e5;
156
+ var intrinsicDateGetTime = Date.prototype.getTime;
157
+ function prototypeChainIncludes(value, target) {
158
+ let current = Object.getPrototypeOf(value);
159
+ while (current !== null) {
160
+ if (current === target) {
161
+ return true;
162
+ }
163
+ current = Object.getPrototypeOf(current);
164
+ }
165
+ return false;
166
+ }
167
+ function safePrototypeDisplayName(proto) {
168
+ if (proto === null) {
169
+ return "null";
170
+ }
171
+ const desc = Object.getOwnPropertyDescriptor(proto, "constructor");
172
+ if (desc !== void 0 && "value" in desc && typeof desc.value === "function" && typeof desc.value.name === "string" && desc.value.name.length > 0) {
173
+ return desc.value.name;
174
+ }
175
+ return "unknown";
176
+ }
177
+ function truncatedKeyLabel(k) {
178
+ if (k.length <= 100) {
179
+ return JSON.stringify(k);
180
+ }
181
+ return `${JSON.stringify(k.slice(0, 50))}... (key length ${k.length})`;
182
+ }
120
183
  function requestKeyFor(value) {
121
- return requestKeyForImpl(value, { stack: /* @__PURE__ */ new WeakSet(), cache: /* @__PURE__ */ new WeakMap(), nodesVisited: 0, outputLength: 0 }, 0);
184
+ return requestKeyForImpl(
185
+ value,
186
+ { stack: /* @__PURE__ */ new WeakSet(), cache: /* @__PURE__ */ new WeakMap(), nodesVisited: 0, outputLength: 0 },
187
+ 0,
188
+ false
189
+ );
190
+ }
191
+ function sortKeyFor(value) {
192
+ return requestKeyForImpl(
193
+ value,
194
+ { stack: /* @__PURE__ */ new WeakSet(), cache: /* @__PURE__ */ new WeakMap(), nodesVisited: 0, outputLength: 0 },
195
+ 0,
196
+ true
197
+ );
198
+ }
199
+ function throwRequestKeyDepthError() {
200
+ throw new RequestKeyError(
201
+ `requestKeyFor: request input exceeds the maximum depth of ${REQUEST_KEY_MAX_DEPTH} nested arrays/objects; flatten or normalize the structure before passing it to a query hook.`
202
+ );
122
203
  }
123
- function requestKeyForImpl(value, ctx, depth) {
204
+ function throwRequestKeyNodesError() {
205
+ throw new RequestKeyError(
206
+ `requestKeyFor: request input exceeds the maximum traversal budget of ${REQUEST_KEY_MAX_NODES} nodes; reduce the filter, params, or requestConfig shape before passing it to a query hook.`
207
+ );
208
+ }
209
+ function throwRequestKeyOutputError() {
210
+ throw new RequestKeyError(
211
+ `requestKeyFor: request input exceeds the maximum serialized key length of ${REQUEST_KEY_MAX_OUTPUT_LENGTH} characters; reduce repeated or oversized request data before passing it to a query hook.`
212
+ );
213
+ }
214
+ function requestKeyForImpl(value, ctx, depth, preserveObjectKeyOrder) {
215
+ return serializeRequestKeyNode(value, ctx, depth, preserveObjectKeyOrder).text;
216
+ }
217
+ function serializeRequestKeyNode(value, ctx, depth, preserveObjectKeyOrder) {
124
218
  if (depth > REQUEST_KEY_MAX_DEPTH) {
125
- throw new RequestKeyError(
126
- `requestKeyFor: request input exceeds the maximum depth of ${REQUEST_KEY_MAX_DEPTH} nested arrays/objects; flatten or normalize the structure before passing it to a query hook.`
127
- );
219
+ throwRequestKeyDepthError();
128
220
  }
129
221
  if (value === null) {
130
222
  noteRequestKeyNode(ctx);
131
- return finalizePrimitiveRequestKey(ctx, "n:");
223
+ return { text: finalizePrimitiveRequestKey(ctx, "n:"), height: 0 };
132
224
  }
133
225
  if (value === void 0) {
134
226
  noteRequestKeyNode(ctx);
135
- return finalizePrimitiveRequestKey(ctx, "u:");
227
+ return { text: finalizePrimitiveRequestKey(ctx, "u:"), height: 0 };
136
228
  }
137
229
  if (typeof value === "object") {
138
230
  const cached = ctx.cache.get(value);
139
231
  if (cached !== void 0) {
140
- reserveRequestKeyOutput(ctx, cached.length);
141
- return cached;
232
+ if (depth + cached.height > REQUEST_KEY_MAX_DEPTH) {
233
+ throwRequestKeyDepthError();
234
+ }
235
+ reserveRequestKeyOutput(ctx, cached.key.length);
236
+ return { text: cached.key, height: cached.height };
142
237
  }
143
238
  }
144
239
  noteRequestKeyNode(ctx);
145
240
  const t = typeof value;
146
241
  switch (t) {
147
242
  case "boolean":
148
- return finalizePrimitiveRequestKey(ctx, `b:${value}`);
243
+ return { text: finalizePrimitiveRequestKey(ctx, `b:${value}`), height: 0 };
149
244
  case "number": {
150
- if (Number.isNaN(value)) return finalizePrimitiveRequestKey(ctx, "n:NaN");
151
- if (Object.is(value, -0)) return finalizePrimitiveRequestKey(ctx, "n:-0");
152
- return finalizePrimitiveRequestKey(ctx, `n:${String(value)}`);
245
+ if (Number.isNaN(value)) return { text: finalizePrimitiveRequestKey(ctx, "n:NaN"), height: 0 };
246
+ if (Object.is(value, -0)) return { text: finalizePrimitiveRequestKey(ctx, "n:-0"), height: 0 };
247
+ return { text: finalizePrimitiveRequestKey(ctx, `n:${String(value)}`), height: 0 };
248
+ }
249
+ case "string": {
250
+ const rawLength = value.length;
251
+ if (ctx.outputLength + rawLength + 4 > REQUEST_KEY_MAX_OUTPUT_LENGTH) {
252
+ throwRequestKeyOutputError();
253
+ }
254
+ return { text: finalizePrimitiveRequestKey(ctx, `s:${JSON.stringify(value)}`), height: 0 };
153
255
  }
154
- case "string":
155
- return finalizePrimitiveRequestKey(ctx, `s:${JSON.stringify(value)}`);
156
256
  case "bigint":
157
257
  throw new RequestKeyError(
158
258
  "requestKeyFor: bigint is not supported in request keys; convert to a number or string before passing to a query hook."
@@ -166,12 +266,45 @@ function requestKeyForImpl(value, ctx, depth) {
166
266
  "requestKeyFor: symbol values are not supported in request keys; symbol-keyed properties cannot participate in structural comparison."
167
267
  );
168
268
  case "object": {
169
- if (value instanceof Date) {
170
- return finalizePrimitiveRequestKey(ctx, `d:${value.getTime()}`);
269
+ if (prototypeChainIncludes(value, Date.prototype)) {
270
+ if (typeof intrinsicDateGetTime !== "function") {
271
+ throw new RequestKeyError(
272
+ "requestKeyFor: Date values are not supported in this environment (Date.prototype.getTime is unavailable); pass an epoch-millis number instead."
273
+ );
274
+ }
275
+ let instant;
276
+ try {
277
+ instant = intrinsicDateGetTime.call(value);
278
+ } catch {
279
+ throw new RequestKeyError(
280
+ "requestKeyFor: invalid Date receiver in request keys; pass an epoch-millis number instead."
281
+ );
282
+ }
283
+ return { text: finalizePrimitiveRequestKey(ctx, `d:${instant}`), height: 0 };
171
284
  }
172
- if (value instanceof RegExp || value instanceof Map || value instanceof Set || typeof URL !== "undefined" && value instanceof URL || value instanceof Error) {
285
+ if (prototypeChainIncludes(value, RegExp.prototype)) {
173
286
  throw new RequestKeyError(
174
- `requestKeyFor: instance of ${value.constructor?.name ?? "unknown"} is not supported in request keys; pass its plain-data representation (e.g. a URL string, a sorted array of entries) to the query hook instead.`
287
+ "requestKeyFor: instance of RegExp is not supported in request keys; pass its plain-data representation (e.g. a source/flags string) to the query hook instead."
288
+ );
289
+ }
290
+ if (prototypeChainIncludes(value, Map.prototype)) {
291
+ throw new RequestKeyError(
292
+ "requestKeyFor: instance of Map is not supported in request keys; pass its plain-data representation (e.g. a sorted array of entries) to the query hook instead."
293
+ );
294
+ }
295
+ if (prototypeChainIncludes(value, Set.prototype)) {
296
+ throw new RequestKeyError(
297
+ "requestKeyFor: instance of Set is not supported in request keys; pass its plain-data representation (e.g. a sorted array of entries) to the query hook instead."
298
+ );
299
+ }
300
+ if (typeof URL !== "undefined" && prototypeChainIncludes(value, URL.prototype)) {
301
+ throw new RequestKeyError(
302
+ "requestKeyFor: instance of URL is not supported in request keys; pass its plain-data representation (e.g. a URL string) to the query hook instead."
303
+ );
304
+ }
305
+ if (prototypeChainIncludes(value, Error.prototype)) {
306
+ throw new RequestKeyError(
307
+ "requestKeyFor: instance of Error is not supported in request keys; pass its plain-data representation (e.g. a message string) to the query hook instead."
175
308
  );
176
309
  }
177
310
  if (Array.isArray(value)) {
@@ -181,23 +314,67 @@ function requestKeyForImpl(value, ctx, depth) {
181
314
  ctx.stack.add(value);
182
315
  reserveRequestKeyOutput(ctx, 1);
183
316
  let out2 = "[";
184
- for (let i = 0; i < value.length; i++) {
317
+ const length = value.length;
318
+ if (ctx.nodesVisited + length > REQUEST_KEY_MAX_NODES) {
319
+ throwRequestKeyNodesError();
320
+ }
321
+ let maxChildHeight2 = 0;
322
+ let hasChild2 = false;
323
+ for (let i = 0; i < length; i++) {
185
324
  if (i > 0) {
186
325
  reserveRequestKeyOutput(ctx, 1);
187
326
  out2 += ",";
188
327
  }
189
- out2 += requestKeyForImpl(value[i], ctx, depth + 1);
328
+ const indexKey = String(i);
329
+ const ownDesc = Object.getOwnPropertyDescriptor(value, indexKey);
330
+ let element;
331
+ if (ownDesc !== void 0) {
332
+ if ("get" in ownDesc || "set" in ownDesc) {
333
+ throw new RequestKeyError(
334
+ `requestKeyFor: accessor array element at index ${i} is not supported in request keys; getters/setters must not run during dep-key construction.`
335
+ );
336
+ }
337
+ element = ownDesc.value;
338
+ } else {
339
+ let inherited;
340
+ let proto2 = Object.getPrototypeOf(value);
341
+ while (proto2 !== null) {
342
+ const candidate = Object.getOwnPropertyDescriptor(proto2, indexKey);
343
+ if (candidate !== void 0) {
344
+ inherited = candidate;
345
+ break;
346
+ }
347
+ proto2 = Object.getPrototypeOf(proto2);
348
+ }
349
+ if (inherited !== void 0) {
350
+ if ("get" in inherited || "set" in inherited) {
351
+ throw new RequestKeyError(
352
+ `requestKeyFor: inherited accessor array element at index ${i} is not supported in request keys; getters/setters must not run during dep-key construction.`
353
+ );
354
+ }
355
+ element = inherited.value;
356
+ } else {
357
+ element = void 0;
358
+ }
359
+ }
360
+ const child = serializeRequestKeyNode(element, ctx, depth + 1, preserveObjectKeyOrder);
361
+ hasChild2 = true;
362
+ if (child.height + 1 > maxChildHeight2) {
363
+ maxChildHeight2 = child.height + 1;
364
+ }
365
+ out2 += child.text;
190
366
  }
191
367
  reserveRequestKeyOutput(ctx, 1);
192
368
  out2 += "]";
193
369
  ctx.stack.delete(value);
194
- ctx.cache.set(value, out2);
195
- return out2;
370
+ const height2 = hasChild2 ? maxChildHeight2 : 0;
371
+ ctx.cache.set(value, { key: out2, height: height2 });
372
+ return { text: out2, height: height2 };
196
373
  }
197
374
  const proto = Object.getPrototypeOf(value);
198
375
  if (proto !== null && proto !== Object.prototype) {
199
376
  throw new RequestKeyError(
200
- `requestKeyFor: object with prototype ${proto.constructor?.name ?? "unknown"} is not a plain object; only object literals and Object.create(null) shapes are supported in request keys.`
377
+ `requestKeyFor: object with prototype ${safePrototypeDisplayName(proto)} is not a plain object; only object literals and Object.create(null) shapes are supported in request keys.`
201
378
  );
202
379
  }
203
380
  if (ctx.stack.has(value)) {
@@ -210,9 +387,22 @@ function requestKeyForImpl(value, ctx, depth) {
210
387
  `requestKeyFor: symbol-keyed properties are not supported in request keys; remove symbol keys from the request input before passing it to a query hook.`
211
388
  );
212
389
  }
213
- const keys = Object.keys(value).sort();
390
+ const unsortedKeys = Object.keys(value);
391
+ if (ctx.nodesVisited + unsortedKeys.length > REQUEST_KEY_MAX_NODES) {
392
+ throwRequestKeyNodesError();
393
+ }
394
+ let totalKeyChars = 0;
395
+ for (let i = 0; i < unsortedKeys.length; i++) {
396
+ totalKeyChars += unsortedKeys[i].length;
397
+ }
398
+ if (ctx.outputLength + totalKeyChars + 6 * unsortedKeys.length + 2 > REQUEST_KEY_MAX_OUTPUT_LENGTH) {
399
+ throwRequestKeyOutputError();
400
+ }
401
+ const keys = preserveObjectKeyOrder ? unsortedKeys : unsortedKeys.sort();
214
402
  reserveRequestKeyOutput(ctx, 1);
215
403
  let out = "{";
404
+ let maxChildHeight = 0;
405
+ let hasChild = false;
216
406
  for (let i = 0; i < keys.length; i++) {
217
407
  if (i > 0) {
218
408
  reserveRequestKeyOutput(ctx, 1);
@@ -221,22 +411,36 @@ function requestKeyForImpl(value, ctx, depth) {
221
411
  const k = keys[i];
222
412
  const desc = Object.getOwnPropertyDescriptor(value, k);
223
413
  if (desc === void 0) {
224
- throw new RequestKeyError(`requestKeyFor: property ${JSON.stringify(k)} has no descriptor (request key).`);
414
+ throw new RequestKeyError(`requestKeyFor: property ${truncatedKeyLabel(k)} has no descriptor (request key).`);
225
415
  }
226
416
  if ("get" in desc || "set" in desc) {
227
417
  throw new RequestKeyError(
228
- `requestKeyFor: accessor property ${JSON.stringify(k)} is not supported in request keys; getters/setters must not run during dep-key construction.`
418
+ `requestKeyFor: accessor property ${truncatedKeyLabel(k)} is not supported in request keys; getters/setters must not run during dep-key construction.`
229
419
  );
230
420
  }
421
+ if (ctx.outputLength + k.length + 3 > REQUEST_KEY_MAX_OUTPUT_LENGTH) {
422
+ throwRequestKeyOutputError();
423
+ }
231
424
  const keyText = JSON.stringify(k);
232
425
  reserveRequestKeyOutput(ctx, keyText.length + 1);
233
- out += `${keyText}:${requestKeyForImpl(desc.value, ctx, depth + 1)}`;
426
+ const child = serializeRequestKeyNode(
427
+ desc.value,
428
+ ctx,
429
+ depth + 1,
430
+ preserveObjectKeyOrder
431
+ );
432
+ hasChild = true;
433
+ if (child.height + 1 > maxChildHeight) {
434
+ maxChildHeight = child.height + 1;
435
+ }
436
+ out += `${keyText}:${child.text}`;
234
437
  }
235
438
  reserveRequestKeyOutput(ctx, 1);
236
439
  out += "}";
237
440
  ctx.stack.delete(value);
238
- ctx.cache.set(value, out);
239
- return out;
441
+ const height = hasChild ? maxChildHeight : 0;
442
+ ctx.cache.set(value, { key: out, height });
443
+ return { text: out, height };
240
444
  }
241
445
  default:
242
446
  throw new RequestKeyError(`requestKeyFor: unsupported value of type ${t}.`);
@@ -245,9 +449,7 @@ function requestKeyForImpl(value, ctx, depth) {
245
449
  function noteRequestKeyNode(ctx) {
246
450
  ctx.nodesVisited += 1;
247
451
  if (ctx.nodesVisited > REQUEST_KEY_MAX_NODES) {
248
- throw new RequestKeyError(
249
- `requestKeyFor: request input exceeds the maximum traversal budget of ${REQUEST_KEY_MAX_NODES} nodes; reduce the filter, params, or requestConfig shape before passing it to a query hook.`
250
- );
452
+ throwRequestKeyNodesError();
251
453
  }
252
454
  }
253
455
  function finalizePrimitiveRequestKey(ctx, key) {
@@ -257,9 +459,7 @@ function finalizePrimitiveRequestKey(ctx, key) {
257
459
  function reserveRequestKeyOutput(ctx, length) {
258
460
  ctx.outputLength += length;
259
461
  if (ctx.outputLength > REQUEST_KEY_MAX_OUTPUT_LENGTH) {
260
- throw new RequestKeyError(
261
- `requestKeyFor: request input exceeds the maximum serialized key length of ${REQUEST_KEY_MAX_OUTPUT_LENGTH} characters; reduce repeated or oversized request data before passing it to a query hook.`
262
- );
462
+ throwRequestKeyOutputError();
263
463
  }
264
464
  }
265
465
  function useMountRef() {
@@ -302,8 +502,9 @@ function reportObserverError(error) {
302
502
  throw error;
303
503
  });
304
504
  }
305
- function runObserverSequence(invocations) {
505
+ function runObserverSequence(invocations, isAlive) {
306
506
  for (const invoke of invocations) {
507
+ if (isAlive && !isAlive()) return;
307
508
  try {
308
509
  invoke();
309
510
  } catch (error) {
@@ -346,13 +547,14 @@ function useAutoQuery({
346
547
  );
347
548
  const fireCallbacksSafely = (0, import_react2.useCallback)(
348
549
  (settle) => {
550
+ const isAlive = () => mountRef.current;
349
551
  if ("result" in settle) {
350
- runObserverSequence([() => onSuccess?.(settle.result), () => onSettled?.(settle.result, null)]);
552
+ runObserverSequence([() => onSuccess?.(settle.result), () => onSettled?.(settle.result, null)], isAlive);
351
553
  return;
352
554
  }
353
- runObserverSequence([() => onError?.(settle.error), () => onSettled?.(null, settle.error)]);
555
+ runObserverSequence([() => onError?.(settle.error), () => onSettled?.(null, settle.error)], isAlive);
354
556
  },
355
- [onSuccess, onError, onSettled]
557
+ [onSuccess, onError, onSettled, mountRef]
356
558
  );
357
559
  const runWithCallbacks = (0, import_react2.useCallback)(
358
560
  async (abortScope, doFetchOverride) => {
@@ -374,6 +576,7 @@ function useAutoQuery({
374
576
  }
375
577
  return res;
376
578
  }
579
+ if (!mountRef.current) return res;
377
580
  applyResult(res);
378
581
  hasDataRef.current = true;
379
582
  setIsLoading(false);
@@ -392,11 +595,12 @@ function useAutoQuery({
392
595
  }
393
596
  throw err;
394
597
  }
598
+ if (!mountRef.current) throw err;
395
599
  setError(err);
396
600
  if (!hasDataRef.current) setIsLoading(false);
397
601
  setIsFetching(false);
398
- fireCallbacksSafely({ error: err });
399
602
  onFailed?.();
603
+ fireCallbacksSafely({ error: err });
400
604
  throw err;
401
605
  } finally {
402
606
  release();
@@ -406,6 +610,8 @@ function useAutoQuery({
406
610
  );
407
611
  (0, import_react2.useEffect)(() => {
408
612
  if (!shouldFetch) {
613
+ ownerIdRef.current += 1;
614
+ manager.abort();
409
615
  setIsLoading(false);
410
616
  setIsFetching(false);
411
617
  onDisabled?.();
@@ -476,20 +682,20 @@ function useMutation(execute, applyData, options) {
476
682
  }
477
683
  try {
478
684
  const result = await execute(...args);
479
- if (mountRef.current) {
480
- runObserverSequence([() => onSuccess?.(result), () => onSettled?.(result, null)]);
481
- if (myId === latestIdRef.current) {
482
- setData(applyData(result));
483
- }
685
+ if (!mountRef.current) return result;
686
+ runObserverSequence([() => onSuccess?.(result), () => onSettled?.(result, null)], () => mountRef.current);
687
+ if (!mountRef.current) return result;
688
+ if (myId === latestIdRef.current) {
689
+ setData(applyData(result));
484
690
  }
485
691
  return result;
486
692
  } catch (err) {
487
693
  const svcErr = err;
488
- if (mountRef.current) {
489
- runObserverSequence([() => onError?.(svcErr), () => onSettled?.(null, svcErr)]);
490
- if (myId === latestIdRef.current) {
491
- setError(svcErr);
492
- }
694
+ if (!mountRef.current) throw svcErr;
695
+ runObserverSequence([() => onError?.(svcErr), () => onSettled?.(null, svcErr)], () => mountRef.current);
696
+ if (!mountRef.current) throw svcErr;
697
+ if (myId === latestIdRef.current) {
698
+ setError(svcErr);
493
699
  }
494
700
  throw svcErr;
495
701
  } finally {
@@ -534,11 +740,12 @@ function createModelHooks(config) {
534
740
  setData(res.data);
535
741
  }, []);
536
742
  let requestKey;
743
+ let sortKey;
537
744
  try {
745
+ sortKey = sortKeyFor(sort);
538
746
  requestKey = requestKeyFor({
539
747
  select,
540
748
  populate,
541
- sort,
542
749
  include,
543
750
  tasks,
544
751
  basicOptions,
@@ -573,12 +780,13 @@ function createModelHooks(config) {
573
780
  // objects, the key is unchanged and `doFetchById` keeps the same
574
781
  // identity — the auto-effect's `deps` therefore stays identical
575
782
  // and the network request is not retried. The `requestKey` is a
576
- // structural digest of `select/populate/sort/include/tasks/
577
- // basicOptions/advancedOptions/requestConfig`; the lint rule can-
783
+ // structural digest of `select/populate/include/tasks/
784
+ // basicOptions/advancedOptions/requestConfig`; `sortKey` is the
785
+ // order-preserving digest of `sort` (ARR-B02). The lint rule can-
578
786
  // not see that derivation, so the missing-deps warning is
579
787
  // silenced here intentionally.
580
788
  // eslint-disable-next-line react-hooks/exhaustive-deps
581
- [modelService, advanced, requestKey]
789
+ [modelService, advanced, requestKey, sortKey]
582
790
  );
583
791
  const doFetch = (0, import_react2.useCallback)(
584
792
  (signal) => {
@@ -604,7 +812,7 @@ function createModelHooks(config) {
604
812
  doFetch,
605
813
  applyResult,
606
814
  shouldFetch,
607
- deps: [id, enabled, advanced, requestKey],
815
+ deps: [id, enabled, advanced, requestKey, sortKey],
608
816
  getRequestSignal,
609
817
  onSuccess: onSuccessStable,
610
818
  onError: onErrorStable,
@@ -649,7 +857,9 @@ function createModelHooks(config) {
649
857
  latestDataRef.current = data;
650
858
  const hasSettledRef = (0, import_react2.useRef)(false);
651
859
  const applyResult = (0, import_react2.useCallback)((res) => {
652
- setData(res.data);
860
+ const next = res.data;
861
+ latestDataRef.current = next;
862
+ setData(next);
653
863
  setTotalCount(res.totalCount);
654
864
  setPreviousData(void 0);
655
865
  hasSettledRef.current = true;
@@ -664,7 +874,7 @@ function createModelHooks(config) {
664
874
  try {
665
875
  listParamsKey = requestKeyFor(listParams);
666
876
  filterKey = requestKeyFor(filter);
667
- sortKey = requestKeyFor(sort);
877
+ sortKey = sortKeyFor(sort);
668
878
  requestKey = requestKeyFor({
669
879
  select,
670
880
  populate,
@@ -714,7 +924,8 @@ function createModelHooks(config) {
714
924
  // `previousData` (ARR-08 req 1) but not in the memo identity.
715
925
  // `filterKey`, `sortKey`, `requestKey` are structural digests of
716
926
  // `filter`/`sort`/`{select, populate, include, tasks,
717
- // basicOptions, advancedOptions, requestConfig}`; the lint rule
927
+ // basicOptions, advancedOptions, requestConfig}` (`sortKey` via
928
+ // order-preserving `sortKeyFor`, ARR-B02); the lint rule
718
929
  // cannot see that derivation, so the missing-deps warning is
719
930
  // silenced here intentionally.
720
931
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -757,7 +968,9 @@ function createModelHooks(config) {
757
968
  [runQuery, baseFetch]
758
969
  );
759
970
  const reset = (0, import_react2.useCallback)(() => {
760
- setData(initialData ?? []);
971
+ const next = initialData ?? [];
972
+ latestDataRef.current = next;
973
+ setData(next);
761
974
  setPreviousData(void 0);
762
975
  setTotalCount(0);
763
976
  hasSettledRef.current = false;