@milaboratories/pl-tree 1.5.7 → 1.6.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.mjs CHANGED
@@ -1,17 +1,17 @@
1
- var W = Object.defineProperty;
2
- var M = (n, e, t) => e in n ? W(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var d = (n, e, t) => M(n, typeof e != "symbol" ? e + "" : e, t);
4
- import { resourceIdToString as R, resourceTypesEqual as A, resourceTypeToString as U, isNotNullResourceId as g, NullResourceId as T, isNullResourceId as b, stringifyWithResourceId as D, isTimeoutOrCancelError as J } from "@milaboratories/pl-client";
5
- import { ChangeSource as m, PollingComputableHooks as q } from "@milaboratories/computable";
6
- import { parsePlError as V } from "@milaboratories/pl-errors";
7
- import { notEmpty as p, deepFreeze as z, msToHumanReadable as H } from "@milaboratories/ts-helpers";
1
+ var M = Object.defineProperty;
2
+ var U = (n, e, t) => e in n ? M(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var d = (n, e, t) => U(n, typeof e != "symbol" ? e + "" : e, t);
4
+ import { resourceIdToString as S, resourceTypesEqual as A, resourceTypeToString as J, isNotNullResourceId as g, NullResourceId as T, isNullResourceId as V, stringifyWithResourceId as D, isTimeoutOrCancelError as q } from "@milaboratories/pl-client";
5
+ import { ChangeSource as m, PollingComputableHooks as z } from "@milaboratories/computable";
6
+ import { parsePlError as P } from "@milaboratories/pl-errors";
7
+ import { notEmpty as p, cachedDeserialize as $, msToHumanReadable as H } from "@milaboratories/ts-helpers";
8
8
  import j from "denque";
9
9
  import * as Y from "node:timers/promises";
10
10
  function ne(n, e) {
11
11
  if (n !== void 0)
12
- return P(n, e);
12
+ return N(n, e);
13
13
  }
14
- function P(n, e) {
14
+ function N(n, e) {
15
15
  const t = {};
16
16
  return n.value !== void 0 && (t.value = e(n.value)), n.error !== void 0 && (t.error = e(n.error)), t;
17
17
  }
@@ -44,10 +44,10 @@ class C {
44
44
  return this.toString();
45
45
  }
46
46
  toString() {
47
- return `[ENTRY:${R(this.rid)}]`;
47
+ return `[ENTRY:${S(this.rid)}]`;
48
48
  }
49
49
  }
50
- function N(n, e, t, r, s) {
50
+ function K(n, e, t, r, s) {
51
51
  const o = new G(
52
52
  n,
53
53
  e,
@@ -57,12 +57,12 @@ function N(n, e, t, r, s) {
57
57
  if (!s.ignoreError) {
58
58
  const a = o.getError();
59
59
  if (a !== void 0)
60
- throw V(p(a.getDataAsString()), o.id, o.resourceType);
60
+ throw P(p(a.getDataAsString()), o.id, o.resourceType);
61
61
  }
62
62
  if (s.assertResourceType !== void 0 && (Array.isArray(s.assertResourceType) ? s.assertResourceType.findIndex((a) => A(a, o.resourceType)) === -1 : !A(s.assertResourceType, o.resourceType)))
63
63
  throw new Error(
64
64
  // eslint-disable-next-line @typescript-eslint/no-base-to-string
65
- `wrong resource type ${U(o.resourceType)} but expected ${s.assertResourceType}`
65
+ `wrong resource type ${J(o.resourceType)} but expected ${s.assertResourceType}`
66
66
  );
67
67
  return o;
68
68
  }
@@ -72,7 +72,7 @@ class E {
72
72
  this.accessorData = e, this.tree = t, this.rid = r, this.instanceData = s;
73
73
  }
74
74
  node(e = {}) {
75
- return this.instanceData.guard(), this.accessorData.hooks !== void 0 && this.instanceData.ctx.attacheHooks(this.accessorData.hooks), N(this.accessorData, this.tree, this.instanceData, this.rid, e);
75
+ return this.instanceData.guard(), this.accessorData.hooks !== void 0 && this.instanceData.ctx.attacheHooks(this.accessorData.hooks), K(this.accessorData, this.tree, this.instanceData, this.rid, e);
76
76
  }
77
77
  }
78
78
  function he(n, e) {
@@ -99,7 +99,7 @@ class G {
99
99
  return { id: this.id, type: this.resourceType };
100
100
  }
101
101
  getResourceFromTree(e, t) {
102
- return N(this.accessorData, this.tree, this.instanceData, e, t);
102
+ return K(this.accessorData, this.tree, this.instanceData, e, t);
103
103
  }
104
104
  traverse(...e) {
105
105
  return this.traverseWithCommon({}, ...e);
@@ -127,7 +127,7 @@ class G {
127
127
  return {
128
128
  ok: !1,
129
129
  // FIXME: in next tickets we'll allow Errors to be thrown.
130
- error: V(
130
+ error: P(
131
131
  p(a.error.getDataAsString()),
132
132
  r.id,
133
133
  r.resourceType,
@@ -138,7 +138,7 @@ class G {
138
138
  if (o.errorIfFieldNotSet)
139
139
  return {
140
140
  ok: !1,
141
- error: new Error(`field have no assigned value ${o.field} of ${R(r.id)}`)
141
+ error: new Error(`field have no assigned value ${o.field} of ${S(r.id)}`)
142
142
  };
143
143
  this.onUnstableLambda("unpopulated_field:" + o.field);
144
144
  return;
@@ -151,7 +151,7 @@ class G {
151
151
  this.instanceData.guard();
152
152
  const t = typeof e == "string" ? { field: e } : e, r = this.resource.getField(this.instanceData.ctx.watcher, t, this.onUnstableLambda);
153
153
  if (r !== void 0)
154
- return P(r, (s) => this.getResourceFromTree(s, { ignoreError: !0 }));
154
+ return N(r, (s) => this.getResourceFromTree(s, { ignoreError: !0 }));
155
155
  }
156
156
  getInputsLocked() {
157
157
  this.instanceData.guard();
@@ -210,12 +210,12 @@ class G {
210
210
  return r === void 0 && t && this.instanceData.ctx.markUnstable("key_not_found_s:" + e), r;
211
211
  }
212
212
  getKeyValueAsJson(e, t = !1) {
213
- const r = this.resource.getKeyValueString(this.instanceData.ctx.watcher, e);
213
+ const r = this.resource.getKeyValueAsJson(this.instanceData.ctx.watcher, e);
214
214
  if (r === void 0) {
215
215
  t && this.instanceData.ctx.markUnstable("key_not_found_j:" + e);
216
216
  return;
217
217
  }
218
- return JSON.parse(r);
218
+ return r;
219
219
  }
220
220
  /**
221
221
  * Can be used to pass a higher level accessor that will wrap the resource and throw its
@@ -229,12 +229,12 @@ class G {
229
229
  return new C(this.accessorData, this.resource.id);
230
230
  }
231
231
  }
232
- class S extends Error {
232
+ class R extends Error {
233
233
  constructor(e) {
234
234
  super(e);
235
235
  }
236
236
  }
237
- class $ {
237
+ class x {
238
238
  constructor(e, t, r, s, o, a, i) {
239
239
  d(this, "change", new m());
240
240
  this.name = e, this.type = t, this.value = r, this.error = s, this.status = o, this.valueIsFinal = a, this.resourceVersion = i;
@@ -250,7 +250,7 @@ class $ {
250
250
  };
251
251
  }
252
252
  }
253
- const L = 0, x = new TextDecoder();
253
+ const L = 0, b = new TextDecoder();
254
254
  class Q {
255
255
  constructor(e, t) {
256
256
  /** Tracks number of other resources referencing this resource. Used to perform garbage collection in tree patching procedure */
@@ -310,7 +310,7 @@ class Q {
310
310
  if (o === void 0) {
311
311
  if (s.errorIfFieldNotFound || s.errorIfFieldNotSet)
312
312
  throw new Error(
313
- `Field "${s.field}" not found in resource ${R(this.id)}`
313
+ `Field "${s.field}" not found in resource ${S(this.id)}`
314
314
  );
315
315
  if (!this.inputsLocked) (a = this.inputAndServiceFieldListChanged) == null || a.attachWatcher(e);
316
316
  else if (s.assertFieldType === "Service" || s.assertFieldType === "Input") {
@@ -356,7 +356,7 @@ class Q {
356
356
  }
357
357
  getError(e) {
358
358
  var t;
359
- if (b(this.error)) {
359
+ if (V(this.error)) {
360
360
  (t = this.resourceStateChange) == null || t.attachWatcher(e);
361
361
  return;
362
362
  } else
@@ -390,15 +390,20 @@ class Q {
390
390
  getKeyValueString(e, t) {
391
391
  const r = this.getKeyValue(e, t);
392
392
  if (r !== void 0)
393
- return x.decode(r);
393
+ return b.decode(r);
394
+ }
395
+ getKeyValueAsJson(e, t) {
396
+ const r = this.getKeyValue(e, t);
397
+ if (r !== void 0)
398
+ return $(r);
394
399
  }
395
400
  getDataAsString() {
396
401
  if (this.data !== void 0)
397
- return this.dataAsString === void 0 && (this.dataAsString = x.decode(this.data)), this.dataAsString;
402
+ return this.dataAsString === void 0 && (this.dataAsString = b.decode(this.data)), this.dataAsString;
398
403
  }
399
404
  getDataAsJson() {
400
405
  if (this.data !== void 0)
401
- return this.dataAsJson === void 0 && (this.dataAsJson = z(JSON.parse(this.getDataAsString()))), this.dataAsJson;
406
+ return this.dataAsJson === void 0 && (this.dataAsJson = $(this.data)), this.dataAsJson;
402
407
  }
403
408
  verifyReadyState() {
404
409
  if (this.resourceReady && !this.inputsLocked)
@@ -456,7 +461,7 @@ class O {
456
461
  this.checkValid();
457
462
  const r = this.resources.get(t);
458
463
  if (r === void 0)
459
- throw this.resourcesAdded.attachWatcher(e), new Error(`resource ${R(t)} not found in the tree`);
464
+ throw this.resourcesAdded.attachWatcher(e), new Error(`resource ${S(t)} not found in the tree`);
460
465
  return r.resourceRemoved.attachWatcher(e), r;
461
466
  }
462
467
  updateFromResourceData(e, t = !1) {
@@ -466,7 +471,7 @@ class O {
466
471
  let i = this.resources.get(a.id);
467
472
  const f = i == null ? void 0 : i.basicState, l = (h) => {
468
473
  const { fields: k, ...u } = a;
469
- throw this.invalidateTree(), new S(
474
+ throw this.invalidateTree(), new R(
470
475
  `Unexpected resource state transition (${h}): ${D(
471
476
  u
472
477
  )} -> ${D(f)}`
@@ -482,7 +487,7 @@ class O {
482
487
  `adding input field "${u.name}", while corresponding list is locked`
483
488
  ), p(i.inputAndServiceFieldListChanged).markChanged()), c.type === "Output" && (i.outputsLocked && l(
484
489
  `adding output field "${u.name}", while corresponding list is locked`
485
- ), p(i.outputFieldListChanged).markChanged()), c.type = u.type, c.change.markChanged(), h = !0), c.value !== u.value && (g(c.value) && s.push(c.value), c.value = u.value, g(u.value) && r.push(u.value), c.change.markChanged(), h = !0), c.error !== u.error && (g(c.error) && s.push(c.error), c.error = u.error, g(u.error) && r.push(u.error), c.change.markChanged(), h = !0), c.status !== u.status && (c.status = u.status, c.change.markChanged(), h = !0), c.valueIsFinal !== u.valueIsFinal && (c.valueIsFinal = u.valueIsFinal, c.change.markChanged(), h = !0), c.resourceVersion = i.version) : (c = new $(
490
+ ), p(i.outputFieldListChanged).markChanged()), c.type = u.type, c.change.markChanged(), h = !0), c.value !== u.value && (g(c.value) && s.push(c.value), c.value = u.value, g(u.value) && r.push(u.value), c.change.markChanged(), h = !0), c.error !== u.error && (g(c.error) && s.push(c.error), c.error = u.error, g(u.error) && r.push(u.error), c.change.markChanged(), h = !0), c.status !== u.status && (c.status = u.status, c.change.markChanged(), h = !0), c.valueIsFinal !== u.valueIsFinal && (c.valueIsFinal = u.valueIsFinal, c.change.markChanged(), h = !0), c.resourceVersion = i.version) : (c = new x(
486
491
  u.name,
487
492
  u.type,
488
493
  u.value,
@@ -519,7 +524,7 @@ class O {
519
524
  } else {
520
525
  i = new Q(a), i.verifyReadyState(), g(i.error) && r.push(i.error);
521
526
  for (const h of a.fields) {
522
- const k = new $(
527
+ const k = new x(
523
528
  h.name,
524
529
  h.type,
525
530
  h.value,
@@ -537,7 +542,7 @@ class O {
537
542
  for (const a of r) {
538
543
  const i = this.resources.get(a);
539
544
  if (!i)
540
- throw this.invalidateTree(), new S(`orphan resource ${a}`);
545
+ throw this.invalidateTree(), new R(`orphan resource ${a}`);
541
546
  i.refCount++;
542
547
  }
543
548
  let o = s;
@@ -546,7 +551,7 @@ class O {
546
551
  for (const i of o) {
547
552
  const f = this.resources.get(i);
548
553
  if (!f)
549
- throw this.invalidateTree(), new S(`orphan resource ${i}`);
554
+ throw this.invalidateTree(), new R(`orphan resource ${i}`);
550
555
  f.refCount--, f.refCount === 0 && f.id !== this.root && (f.fieldsMap.forEach((l) => {
551
556
  g(l.value) && a.push(l.value), g(l.error) && a.push(l.error), l.change.markChanged();
552
557
  }), g(f.error) && a.push(f.error), f.resourceRemoved.markChanged(), this.resources.delete(i));
@@ -556,7 +561,7 @@ class O {
556
561
  if (!t) {
557
562
  for (const a of e)
558
563
  if (!this.resources.has(a.id))
559
- throw this.invalidateTree(), new S(`orphan input resource ${a.id}`);
564
+ throw this.invalidateTree(), new R(`orphan input resource ${a.id}`);
560
565
  }
561
566
  }
562
567
  /** @deprecated use "entry" instead */
@@ -615,17 +620,17 @@ async function Z(n, e, t) {
615
620
  const { seedResources: s, finalResources: o, pruningFunction: a } = e, i = new j();
616
621
  let f = !0, l = 0;
617
622
  const h = /* @__PURE__ */ new Set(), k = (v) => {
618
- if (b(v) || h.has(v)) return;
623
+ if (V(v) || h.has(v)) return;
619
624
  if (o.has(v)) {
620
625
  t && t.finalResourcesSkipped++;
621
626
  return;
622
627
  }
623
628
  h.add(v);
624
- const y = n.getResourceDataIfExists(v, !0), w = n.listKeyValuesIfResourceExists(v), B = f;
629
+ const y = n.getResourceDataIfExists(v, !0), w = n.listKeyValuesIfResourceExists(v), W = f;
625
630
  f && (f = !1), i.push(
626
631
  (async () => {
627
632
  const [I, _] = await Promise.all([y, w]);
628
- if (B && (l++, f = !0), I !== void 0) {
633
+ if (W && (l++, f = !0), I !== void 0) {
629
634
  if (_ === void 0) throw new Error("Inconsistent replies");
630
635
  return { ...I, kv: _ };
631
636
  }
@@ -709,7 +714,7 @@ function ve(n, e, t) {
709
714
  metadata: Object.fromEntries(o)
710
715
  };
711
716
  }
712
- class K {
717
+ class B {
713
718
  constructor(e, t, r, s) {
714
719
  d(this, "finalPredicate");
715
720
  d(this, "state");
@@ -727,7 +732,7 @@ class K {
727
732
  d(this, "terminated", !1);
728
733
  this.pl = e, this.root = t, this.logger = s;
729
734
  const { finalPredicateOverride: o, pruning: a, pollingInterval: i, stopPollingDelay: f, logStat: l } = r;
730
- this.pruning = a, this.pollingInterval = i, this.finalPredicate = o ?? e.finalPredicate, this.logStat = l, this.state = new O(t, this.finalPredicate), this.hooks = new q(
735
+ this.pruning = a, this.pollingInterval = i, this.finalPredicate = o ?? e.finalPredicate, this.logStat = l, this.state = new O(t, this.finalPredicate), this.hooks = new z(
731
736
  () => this.startUpdating(),
732
737
  () => this.stopUpdating(),
733
738
  { stopDebounce: f },
@@ -776,7 +781,7 @@ class K {
776
781
  if (this.logStat === "per-request" && (e = F()), await this.refresh(e), e && this.logger && this.logger.info(`Tree stat (success): ${JSON.stringify(e)}`), s !== void 0) for (const o of s) o.resolve();
777
782
  } catch (o) {
778
783
  if (e && this.logger && this.logger.info(`Tree stat (error): ${JSON.stringify(e)}`), s !== void 0) for (const a of s) a.reject(o);
779
- if (o instanceof S) {
784
+ if (o instanceof R) {
780
785
  (t = this.logger) == null || t.error(o), this.state.invalidateTree("stat update error"), this.state = new O(this.root, this.finalPredicate);
781
786
  continue;
782
787
  } else (r = this.logger) == null || r.warn(o);
@@ -785,7 +790,7 @@ class K {
785
790
  try {
786
791
  await Y.setTimeout(this.pollingInterval, this.abortController.signal);
787
792
  } catch (o) {
788
- if (!J(o)) throw new Error("Unexpected error", { cause: o });
793
+ if (!q(o)) throw new Error("Unexpected error", { cause: o });
789
794
  break;
790
795
  }
791
796
  }
@@ -810,7 +815,7 @@ class K {
810
815
  this.currentLoop !== void 0 && await this.currentLoop;
811
816
  }
812
817
  static async init(e, t, r, s) {
813
- const o = new K(e, t, r, s), a = r.logStat ? F() : void 0;
818
+ const o = new B(e, t, r, s), a = r.logStat ? F() : void 0;
814
819
  let i = !1;
815
820
  try {
816
821
  await o.refresh(a, {
@@ -870,8 +875,8 @@ export {
870
875
  G as PlTreeNodeAccessor,
871
876
  Q as PlTreeResource,
872
877
  O as PlTreeState,
873
- K as SynchronizedTreeState,
874
- S as TreeStateUpdateError,
878
+ B as SynchronizedTreeState,
879
+ R as TreeStateUpdateError,
875
880
  X as constructTreeLoadingRequest,
876
881
  le as formatTreeLoadingStat,
877
882
  F as initialTreeLoadingStat,
@@ -880,7 +885,7 @@ export {
880
885
  ce as isPlTreeNodeAccessor,
881
886
  Z as loadTreeState,
882
887
  ge as makeResourceSnapshot,
883
- P as mapValueAndError,
888
+ N as mapValueAndError,
884
889
  ne as mapValueAndErrorIfDefined,
885
890
  fe as rsSchema,
886
891
  ye as treeDumpStats,