@pravosleva/reactive-engine 0.3.10-beta → 0.3.12-beta

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.mjs CHANGED
@@ -3,7 +3,7 @@ import e, { useCallback as t, useEffect as n, useMemo as r, useRef as i, useStat
3
3
  var s = class {
4
4
  engine;
5
5
  constructor(e) {
6
- this.engine = e;
6
+ this.engine = e, this.onInit();
7
7
  }
8
8
  createNumericSignal(e, t) {
9
9
  return this.engine.signal(e, {
@@ -11,13 +11,20 @@ var s = class {
11
11
  validate: (e) => typeof e == "number" && !isNaN(e)
12
12
  });
13
13
  }
14
- }, c = class {
14
+ createComputed(e) {
15
+ return this.engine.computed(e);
16
+ }
17
+ createSignal(e, t) {
18
+ return this.engine.signal(e, t);
19
+ }
20
+ onInit() {}
21
+ }, c = s, l = class {
15
22
  engine;
16
23
  constructor(e) {
17
24
  this.engine = e, setTimeout(() => this.onInit(), 0);
18
25
  }
19
26
  onInit() {}
20
- }, l = ({ tested: e, expectedKey: t, valueType: n }) => {
27
+ }, u = ({ tested: e, expectedKey: t, valueType: n }) => {
21
28
  let r = [];
22
29
  for (let i = 0, a = e.length; i < a; i++) {
23
30
  let a = e[i], o;
@@ -32,7 +39,7 @@ var s = class {
32
39
  for (let e of a.matchAll(o)) e?.groups && r.push(e.groups.value);
33
40
  }
34
41
  return r;
35
- }, u = class {
42
+ }, d = class {
36
43
  activeEffect = null;
37
44
  isBatching = !1;
38
45
  pendingEffects = /* @__PURE__ */ new Set();
@@ -171,19 +178,22 @@ var s = class {
171
178
  });
172
179
  }
173
180
  resource(e, t, n) {
174
- let r = n && typeof n == "object", i = r ? n.name : n || "unnamed_resource", a = r ? n : {}, o = a.resetDataOnSourceChange ?? !0, s = a.responseValidate, c = a.validateBeforeFetch, u = a.retryCount ?? 0, d = a.retryDelay ?? 1e3, f = a.isExponentialBackoffEnabled ?? !1, p = a.maxRetryDelay ?? 3e4, m = a.timeout, h = this.signal({
181
+ let r = n && typeof n == "object", i = r ? n.name : n || "unnamed_resource", a = r ? n : {}, o = a.resetDataOnSourceChange ?? !0, s = a.responseValidate, c = a.validateBeforeFetch, l = a.retryCount ?? 0, d = a.retryDelay ?? 1e3, f = a.isExponentialBackoffEnabled ?? !1, p = a.maxRetryDelay ?? 3e4, m = a.timeout, h = this.signal({
175
182
  data: null,
176
183
  loading: !0,
177
184
  error: null,
178
185
  isRetrying: !1
179
186
  }, i), g = async (t, n, r = !1) => {
187
+ if (n.aborted) return;
180
188
  if (c) {
181
189
  let e = c(t);
182
190
  if (e === !1 || typeof e == "string") {
191
+ if (n.aborted) return;
192
+ let t = typeof e == "string" ? e : "Pre-fetch validation failed for resource", i = r ? null : this.untrack(() => h.value.data);
183
193
  h.value = {
184
- data: null,
194
+ data: i,
185
195
  loading: !1,
186
- error: Error(typeof e == "string" ? e : "Pre-fetch validation failed for resource"),
196
+ error: Error(t),
187
197
  isRetrying: !1
188
198
  };
189
199
  return;
@@ -196,23 +206,21 @@ var s = class {
196
206
  error: null,
197
207
  isRetrying: !1
198
208
  };
199
- for (let r = 0; r <= u; r++) {
209
+ for (let r = 0; r <= l; r++) {
210
+ if (n.aborted) return;
200
211
  let a = n;
201
212
  try {
202
- if (n.aborted) return;
203
213
  if (m && m > 0) {
204
214
  let e = AbortSignal.timeout(m);
205
215
  a = AbortSignal.any([n, e]);
206
216
  }
207
217
  let r = await e(t, a);
208
- if (a.aborted) {
209
- if (!n.aborted) throw new DOMException("The operation timed out.", "TimeoutError");
210
- return;
211
- }
218
+ if (n.aborted) return;
219
+ if (a.aborted) throw new DOMException("The operation timed out.", "TimeoutError");
212
220
  if (s) {
213
221
  let e = s(r);
214
222
  if (e === !1 || typeof e == "string") {
215
- if (a.aborted) return;
223
+ if (n.aborted) return;
216
224
  h.value = {
217
225
  data: null,
218
226
  loading: !1,
@@ -222,7 +230,7 @@ var s = class {
222
230
  return;
223
231
  }
224
232
  }
225
- if (a.aborted) return;
233
+ if (n.aborted) return;
226
234
  h.value = {
227
235
  data: r,
228
236
  loading: !1,
@@ -232,16 +240,16 @@ var s = class {
232
240
  return;
233
241
  } catch (e) {
234
242
  if (n.aborted || e.name === "AbortError" && n.aborted) return;
235
- let t = e.name === "TimeoutError" || e.name === "AbortError" || e.message?.includes("timeout"), a = l({
243
+ let t = e.name === "TimeoutError" || e.name === "AbortError" || e.message?.includes("timeout"), a = u({
236
244
  tested: [e.message],
237
245
  expectedKey: "THROW_CUSTOM_VALIDATION_ERROR_NO_RETRY",
238
246
  valueType: "number"
239
- })?.[0] === "1" || !1, o = a && l({
247
+ })?.[0] === "1" || !1, o = a && u({
240
248
  tested: [e.message],
241
249
  expectedKey: "MESSAGE",
242
250
  valueType: "string"
243
251
  })?.[0] || "Custom validation error", s = a || e.name === "ValidationError";
244
- if (r === u || s) {
252
+ if (r === l || s) {
245
253
  if (n.aborted) return;
246
254
  let r = t ? Error(`Request timed out after ${m}ms`, { cause: e }) : a ? Error(o, { cause: e }) : e;
247
255
  h.value = {
@@ -261,7 +269,7 @@ var s = class {
261
269
  loading: !0,
262
270
  error: null,
263
271
  isRetrying: !0
264
- }, console.warn(`[Resource Retry] "${i}" сбой по ${t ? "таймауту" : "ошибке сети"}. Попытка ${r + 1}/${u + 1}...`);
272
+ }, console.warn(`[Resource Retry] "${i}" сбой по ${t ? "таймауту" : "ошибке сети"}. Попытка ${r + 1}/${l + 1}...`);
265
273
  try {
266
274
  if (await this.delay(e, n), n.aborted) return;
267
275
  } catch {
@@ -329,7 +337,7 @@ var s = class {
329
337
  console.error("%c[Reactive Error] %cОшибка в эффекте/computed:", "color: white; background: red; padding: 2px 4px; border-radius: 3px;", "font-weight: bold;", e);
330
338
  }
331
339
  }
332
- }, d = (e) => ({ children: t }) => {
340
+ }, f = (e) => ({ children: t }) => {
333
341
  let a = i(0), s = i(null), c = i(null), l = i(null), u = r(() => ({
334
342
  subscribe(e) {
335
343
  return s.current = e, () => {
@@ -356,7 +364,7 @@ var s = class {
356
364
  e.activeEffect = d;
357
365
  }
358
366
  return o(u.subscribe, u.getSnapshot), f;
359
- }, f = (t) => (n) => {
367
+ }, p = (t) => (n) => {
360
368
  let a = (a) => {
361
369
  let s = i(0), c = t, l = "activeEffect" in c ? "activeEffect" : "_activeEffect", u = i(null), { subscribe: d, getSnapshot: f } = r(() => ({
362
370
  subscribe: (e) => {
@@ -384,7 +392,7 @@ var s = class {
384
392
  return g;
385
393
  };
386
394
  return a.displayName = `Observer(${n.displayName || n.name || "Component"})`, a;
387
- }, p = (e, t = {}) => {
395
+ }, m = (e, t = {}) => {
388
396
  let n = t.ttl ?? 300 * 1e3, r = /* @__PURE__ */ new Map();
389
397
  return async (t, i) => {
390
398
  let a = typeof t == "object" && t ? JSON.stringify(t) : String(t), o = Date.now(), s = r.get(a);
@@ -395,19 +403,125 @@ var s = class {
395
403
  timestamp: Date.now()
396
404
  }), c;
397
405
  };
398
- }, m = (e, t) => {
406
+ }, h = (e, t = {}) => {
407
+ let n = t.delay ?? 300, r = null, i = null;
408
+ return (t, a) => (r && clearTimeout(r), i && i(new DOMException("Aborted due to debounce", "AbortError")), new Promise((o, s) => {
409
+ i = s;
410
+ let c = () => {
411
+ r && clearTimeout(r), s(new DOMException("Aborted by resource signal", "AbortError"));
412
+ };
413
+ if (a.aborted) return c();
414
+ a.addEventListener("abort", c), r = setTimeout(async () => {
415
+ a.removeEventListener("abort", c), i = null, r = null;
416
+ try {
417
+ o(await e(t, a));
418
+ } catch (e) {
419
+ s(e);
420
+ }
421
+ }, n);
422
+ }));
423
+ }, g = (e, t) => {
424
+ let { onNextTick: n, onError: r, externalSignal: i, delay: a = 500, errorInitialDelay: o = 2e3, errorMaxDelay: s = 8e3 } = t, c = o, l = 0;
425
+ return (t, u) => {
426
+ let d = ++l;
427
+ return new Promise((f, p) => {
428
+ let m = async () => {
429
+ if (u.aborted || i?.aborted) {
430
+ p(new DOMException("Aborted by long polling lifecycle", "AbortError"));
431
+ return;
432
+ }
433
+ try {
434
+ let r = await e(t, u);
435
+ if (c = o, u.aborted || i?.aborted) {
436
+ p(new DOMException("Aborted by long polling lifecycle", "AbortError"));
437
+ return;
438
+ }
439
+ f(r), setTimeout(() => {
440
+ d === l && !u.aborted && !i?.aborted && n();
441
+ }, a);
442
+ } catch (e) {
443
+ if (e instanceof DOMException && e.name === "AbortError") {
444
+ p(e);
445
+ return;
446
+ }
447
+ p(e);
448
+ let t = c;
449
+ r(t, () => {
450
+ d === l && (c = Math.min(c * 2, s));
451
+ }), setTimeout(() => {
452
+ d === l && !u.aborted && !i?.aborted && n();
453
+ }, t);
454
+ }
455
+ }, h = () => {
456
+ p(new DOMException("Aborted by lifecycle event", "AbortError"));
457
+ };
458
+ if (u.addEventListener("abort", h), i && i.addEventListener("abort", h), u.aborted || i?.aborted) return h();
459
+ m();
460
+ });
461
+ };
462
+ }, _ = (e, t = {}) => {
463
+ let n = t.limit ?? 300, r = 0, i = null, a = null, o = null, s = null, c = null;
464
+ return (t, l) => {
465
+ let u = Date.now(), d = n - (u - r), f = () => {
466
+ i &&= (clearTimeout(i), null), s &&= (s(new DOMException("Aborted by resource signal during throttle", "AbortError")), o = null, null);
467
+ };
468
+ return d <= 0 ? (i &&= (clearTimeout(i), null), s &&= (s(new DOMException("Aborted due to newer direct throttle execution", "AbortError")), o = null, null), r = u, e(t, l)) : (s && s(new DOMException("Aborted due to newer throttled value", "AbortError")), new Promise((n, u) => {
469
+ if (a = t, o = n, s = u, c = l, l.aborted) return f();
470
+ l.addEventListener("abort", f), i ||= setTimeout(async () => {
471
+ i = null;
472
+ let t = a, n = o, l = s, u = c;
473
+ a = null, o = null, s = null, c = null, u.removeEventListener("abort", f);
474
+ try {
475
+ r = Date.now(), n(await e(t, u));
476
+ } catch (e) {
477
+ l(e);
478
+ }
479
+ }, d);
480
+ }));
481
+ };
482
+ }, v = (e, t = {}) => {
483
+ let n = t.limit ?? 300, r = t.ttl ?? 300 * 1e3, i = 0, a = null, o = null, s = null, c = null, l = null, u = /* @__PURE__ */ new Map(), d = (e) => typeof e == "object" && e ? JSON.stringify(e) : String(e);
484
+ return (t, f) => {
485
+ let p = Date.now(), m = n - (p - i);
486
+ d(t);
487
+ let h = () => {
488
+ a &&= (clearTimeout(a), null), c &&= (c(new DOMException("Aborted by resource signal during throttle", "AbortError")), s = null, null);
489
+ }, g = async (t, n) => {
490
+ let i = Date.now(), a = d(t), o = u.get(a);
491
+ if (o && i - o.timestamp < r) return o.data;
492
+ let s = await e(t, n);
493
+ return u.set(a, {
494
+ data: s,
495
+ timestamp: Date.now()
496
+ }), s;
497
+ };
498
+ return m <= 0 ? (a &&= (clearTimeout(a), null), c &&= (c(new DOMException("Aborted due to newer direct execution", "AbortError")), s = null, null), i = p, g(t, f)) : (c && c(new DOMException("Aborted due to newer throttled value", "AbortError")), new Promise((e, n) => {
499
+ if (o = t, s = e, c = n, l = f, f.aborted) return h();
500
+ f.addEventListener("abort", h), a ||= setTimeout(async () => {
501
+ a = null;
502
+ let e = o, t = s, n = c, r = l;
503
+ o = null, s = null, c = null, l = null, r.removeEventListener("abort", h);
504
+ try {
505
+ i = Date.now(), t(await g(e, r));
506
+ } catch (e) {
507
+ n(e);
508
+ }
509
+ }, m);
510
+ }));
511
+ };
512
+ }, y = (e, t) => {
399
513
  let r = i(t);
400
514
  n(() => {
401
515
  r.current = t;
402
516
  }, [t]), n(() => e.subscribe((e) => {
403
517
  r.current(e);
404
518
  }), [e]);
405
- }, h = (e) => {
519
+ }, b = (e) => {
406
520
  let i = r(() => typeof e == "function" ? e() : e, [e]), a = t((e) => i.subscribe(e), [i]), s = t(() => i.value, [i]);
407
521
  return n(() => () => {
408
522
  i && typeof i.destroy == "function" && i.destroy();
409
523
  }, [i]), o(a, s);
410
- }, g = (e) => {
524
+ }, x = (e) => {
411
525
  let t = r(() => typeof e == "function" ? e() : e, [e]), [o, s] = a(t.value), c = i(s);
412
526
  return n(() => {
413
527
  c.current = s;
@@ -422,6 +536,6 @@ var s = class {
422
536
  }, [t]), o;
423
537
  };
424
538
  //#endregion
425
- export { s as BaseREService, c as BaseREServiceEnhanced, u as ReactiveEngine, f as createObserver, d as createObserverComponent, m as useReactiveSubscription, h as useReactiveValue, g as useReactiveValue0, p as withCache };
539
+ export { s as AbstractService, c as BaseREService, l as BaseREServiceEnhanced, d as ReactiveEngine, p as createObserver, f as createObserverComponent, y as useReactiveSubscription, b as useReactiveValue, x as useReactiveValue0, m as withCache, h as withDebounce, g as withLongPolling, _ as withThrottle, v as withThrottleAndCache };
426
540
 
427
541
  //# sourceMappingURL=index.mjs.map