@milaboratories/pl-tree 1.4.32 → 1.4.33
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/accessors.d.ts +0 -4
- package/dist/accessors.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +178 -179
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
- package/src/accessors.ts +13 -14
package/dist/accessors.d.ts
CHANGED
|
@@ -4,10 +4,6 @@ import { ResourceId, ResourceType, OptionalResourceId } from '@milaboratories/pl
|
|
|
4
4
|
import { ValueAndError } from './value_and_error';
|
|
5
5
|
import { CommonFieldTraverseOps, FieldTraversalStep, GetFieldStep, ResourceTraversalOps } from './traversal_ops';
|
|
6
6
|
import { ValueOrError } from './value_or_error';
|
|
7
|
-
/** Error encountered during traversal in field or resource. */
|
|
8
|
-
export declare class PlError extends Error {
|
|
9
|
-
constructor(message: string);
|
|
10
|
-
}
|
|
11
7
|
export type TreeAccessorData = {
|
|
12
8
|
readonly treeProvider: () => PlTreeState;
|
|
13
9
|
readonly hooks?: ComputableHooks;
|
package/dist/accessors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accessors.d.ts","sourceRoot":"","sources":["../src/accessors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,eAAe,EACf,UAAU,EACX,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,UAAU,EAEV,YAAY,EAIZ,kBAAkB,
|
|
1
|
+
{"version":3,"file":"accessors.d.ts","sourceRoot":"","sources":["../src/accessors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,eAAe,EACf,UAAU,EAC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kBAAkB,GAAG,SAAS;IAS1C,OAAO,IAAI,UAAU,GAAG,SAAS;IAIjC,eAAe,IAAI,MAAM,GAAG,SAAS;IAIrC,aAAa,CAAC,CAAC,GAAG,OAAO,KAAK,CAAC,GAAG,SAAS;IAI3C,eAAe,IAAI,MAAM,EAAE;IAM3B,gBAAgB,IAAI,MAAM,EAAE;IAM5B,iBAAiB,IAAI,MAAM,EAAE;IAK7B,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,kBAAkB,GAAE,OAAe,GAAG,UAAU,GAAG,SAAS;IAQ5F,kBAAkB;IACX,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIlD,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,kBAAkB,GAAE,OAAe,GAAG,MAAM,GAAG,SAAS;IAQzF,iBAAiB,CAAC,CAAC,GAAG,OAAO,EAClC,GAAG,EAAE,MAAM,EACX,kBAAkB,GAAE,OAAe,GAClC,CAAC,GAAG,SAAS;IAShB;;;SAGK;IACE,eAAe,IAAI,mBAAmB;IAI7C,0CAA0C;IACnC,OAAO,IAAI,WAAW;CAG9B"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";var K=Object.defineProperty;var M=(o,e,t)=>e in o?K(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var c=(o,e,t)=>M(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@milaboratories/pl-client"),m=require("@milaboratories/computable"),v=require("@milaboratories/ts-helpers"),U=require("denque"),q=require("node:timers/promises");function J(o){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const t in o)if(t!=="default"){const i=Object.getOwnPropertyDescriptor(o,t);Object.defineProperty(e,t,i.get?i:{enumerable:!0,get:()=>o[t]})}}return e.default=o,Object.freeze(e)}const B=J(q);function z(o,e){if(o!==void 0)return F(o,e)}function F(o,e){const t={};return o.value!==void 0&&(t.value=e(o.value)),o.error!==void 0&&(t.error=e(o.error)),t}class D extends Error{constructor(e){super(e)}}function j(o){return typeof o=="object"&&o!==null&&o.__pl_tree_type_marker__==="PlTreeEntry"}function H(o){return typeof o=="object"&&o!==null&&o.__pl_tree_type_marker__==="PlTreeEntryAccessor"}function Y(o){return typeof o=="object"&&o!==null&&o.__pl_tree_type_marker__==="PlTreeNodeAccessor"}class S{constructor(e,t){c(this,"__pl_tree_type_marker__","PlTreeEntry");this.accessorData=e,this.rid=t}createAccessor(e,t){return new T(this.accessorData,this.accessorData.treeProvider(),this.rid,{ctx:e,guard:t})}toJSON(){return this.toString()}toString(){return`[ENTRY:${l.resourceIdToString(this.rid)}]`}}function O(o,e,t,i,s){const a=new P(o,e,e.get(t.ctx.watcher,i),t);if(!s.ignoreError){const n=a.getError();if(n!==void 0)throw new D(`error encountered on resource ${l.resourceIdToString(a.id)} (${l.resourceTypeToString(a.resourceType)}): ${n.getDataAsString()}`)}if(s.assertResourceType!==void 0&&(Array.isArray(s.assertResourceType)?s.assertResourceType.findIndex(n=>l.resourceTypesEqual(n,a.resourceType))===-1:!l.resourceTypesEqual(s.assertResourceType,a.resourceType)))throw new Error(`wrong resource type ${l.resourceTypeToString(a.resourceType)} but expected ${s.assertResourceType}`);return a}class T{constructor(e,t,i,s){c(this,"__pl_tree_type_marker__","PlTreeEntryAccessor");this.accessorData=e,this.tree=t,this.rid=i,this.instanceData=s}node(e={}){return this.instanceData.guard(),this.accessorData.hooks!==void 0&&this.instanceData.ctx.attacheHooks(this.accessorData.hooks),O(this.accessorData,this.tree,this.instanceData,this.rid,e)}}function G(o,e){return o instanceof S?e.accessor(o).node().resourceInfo:o}class P{constructor(e,t,i,s){c(this,"__pl_tree_type_marker__","PlTreeNodeAccessor");c(this,"onUnstableLambda",e=>{this.instanceData.ctx.markUnstable(e)});this.accessorData=e,this.tree=t,this.resource=i,this.instanceData=s}get id(){return this.instanceData.guard(),this.resource.id}get originalId(){return this.instanceData.guard(),this.resource.originalResourceId}get resourceType(){return this.instanceData.guard(),this.resource.type}get resourceInfo(){return{id:this.id,type:this.resourceType}}getResourceFromTree(e,t){return O(this.accessorData,this.tree,this.instanceData,e,t)}traverse(...e){return this.traverseWithCommon({},...e)}traverseOrError(...e){return this.traverseOrErrorWithCommon({},...e)}traverseWithCommon(e,...t){const i=this.traverseOrErrorWithCommon(e,...t);if(i!==void 0){if(!i.ok)throw new D(i.error);return i.value}}traverseOrErrorWithCommon(e,...t){let i=this;for(const s of t){const a=typeof s=="string"?{...e,field:s}:{...e,...s},n=i.getField(s);if(n===void 0||a.pureFieldErrorToUndefined&&n.value===void 0&&n.error!==void 0)return;if((!a.ignoreError||n.value===void 0)&&n.error!==void 0)return{ok:!1,error:`error in field ${a.field} of ${l.resourceIdToString(i.id)}: ${n.error.getDataAsString()}`};if(n.value===void 0){if(a.errorIfFieldNotSet)return{ok:!1,error:`field have no assigned value ${a.field} of ${l.resourceIdToString(i.id)}`};this.onUnstableLambda("unpopulated_field:"+a.field);return}i=n.value}return{ok:!0,value:i}}getField(e){this.instanceData.guard();const t=typeof e=="string"?{field:e}:e,i=this.resource.getField(this.instanceData.ctx.watcher,t,this.onUnstableLambda);if(i!==void 0)return F(i,s=>this.getResourceFromTree(s,{ignoreError:!0}))}getInputsLocked(){this.instanceData.guard();const e=this.resource.getInputsLocked(this.instanceData.ctx.watcher);return e||this.instanceData.ctx.markUnstable("inputs_unlocked:"+this.resourceType.name),e}getOutputsLocked(){this.instanceData.guard();const e=this.resource.getOutputsLocked(this.instanceData.ctx.watcher);return e||this.instanceData.ctx.markUnstable("outputs_unlocked:"+this.resourceType.name),e}getIsReadyOrError(){this.instanceData.guard();const e=this.resource.getIsReadyOrError(this.instanceData.ctx.watcher);return e||this.instanceData.ctx.markUnstable("not_ready:"+this.resourceType.name),e}getIsFinal(){return this.instanceData.guard(),this.resource.getIsFinal(this.instanceData.ctx.watcher)}getError(){this.instanceData.guard();const e=this.resource.getError(this.instanceData.ctx.watcher);if(e!==void 0)return this.getResourceFromTree(e,{})}getData(){return this.resource.data}getDataAsString(){return this.resource.getDataAsString()}getDataAsJson(){return this.resource.getDataAsJson()}listInputFields(){return this.instanceData.guard(),this.getInputsLocked(),this.resource.listInputFields(this.instanceData.ctx.watcher)}listOutputFields(){return this.instanceData.guard(),this.getOutputsLocked(),this.resource.listOutputFields(this.instanceData.ctx.watcher)}listDynamicFields(){return this.instanceData.guard(),this.resource.listDynamicFields(this.instanceData.ctx.watcher)}getKeyValue(e,t=!1){this.instanceData.guard();const i=this.resource.getKeyValue(this.instanceData.ctx.watcher,e);return i===void 0&&t&&this.instanceData.ctx.markUnstable("key_not_found_b:"+e),i}getKeyValueString(e){return this.getKeyValueAsString(e)}getKeyValueAsString(e,t=!1){this.instanceData.guard();const i=this.resource.getKeyValueString(this.instanceData.ctx.watcher,e);return i===void 0&&t&&this.instanceData.ctx.markUnstable("key_not_found_s:"+e),i}getKeyValueAsJson(e,t=!1){const i=this.resource.getKeyValueString(this.instanceData.ctx.watcher,e);if(i===void 0){t&&this.instanceData.ctx.markUnstable("key_not_found_j:"+e);return}return JSON.parse(i)}toEntryAccessor(){return new T(this.accessorData,this.tree,this.id,this.instanceData)}persist(){return new S(this.accessorData,this.resource.id)}}class w extends Error{constructor(e){super(e)}}class A{constructor(e,t,i,s,a,n,r){c(this,"change",new m.ChangeSource);this.name=e,this.type=t,this.value=i,this.error=s,this.status=a,this.valueIsFinal=n,this.resourceVersion=r}}const E=0,$=new TextDecoder;class b{constructor(e,t){c(this,"refCount",0);c(this,"version",E);c(this,"dataVersion",E);c(this,"fieldsMap",new Map);c(this,"kv",new Map);c(this,"resourceRemoved",new m.ChangeSource);c(this,"finalChanged",new m.ChangeSource);c(this,"resourceStateChange",new m.ChangeSource);c(this,"lockedChange",new m.ChangeSource);c(this,"inputAndServiceFieldListChanged",new m.ChangeSource);c(this,"outputFieldListChanged",new m.ChangeSource);c(this,"dynamicFieldListChanged",new m.ChangeSource);c(this,"kvChanged",new m.ChangeSource);c(this,"id");c(this,"originalResourceId");c(this,"kind");c(this,"type");c(this,"data");c(this,"dataAsString");c(this,"dataAsJson");c(this,"error");c(this,"inputsLocked");c(this,"outputsLocked");c(this,"resourceReady");c(this,"finalFlag");c(this,"_finalState",!1);c(this,"logger");this.id=e.id,this.originalResourceId=e.originalResourceId,this.kind=e.kind,this.type=e.type,this.data=e.data,this.error=e.error,this.inputsLocked=e.inputsLocked,this.outputsLocked=e.outputsLocked,this.resourceReady=e.resourceReady,this.finalFlag=e.final,this.logger=t}info(e){this.logger!==void 0&&this.logger.info(e)}warn(e){this.logger!==void 0&&this.logger.warn(e)}get final(){return this.finalFlag}get finalState(){return this._finalState}get fields(){return[...this.fieldsMap.values()]}getField(e,t,i=()=>{}){var n,r,g;const s=typeof t=="string"?{field:t}:t,a=this.fieldsMap.get(s.field);if(a===void 0){if(s.errorIfFieldNotFound||s.errorIfFieldNotSet)throw new Error(`Field "${s.field}" not found in resource ${l.resourceIdToString(this.id)}`);if(!this.inputsLocked)(n=this.inputAndServiceFieldListChanged)==null||n.attachWatcher(e);else if(s.assertFieldType==="Service"||s.assertFieldType==="Input"){if(s.allowPermanentAbsence)return;throw new Error(`Service or input field not found ${s.field}.`)}if(!this.outputsLocked)(r=this.outputFieldListChanged)==null||r.attachWatcher(e);else if(s.assertFieldType==="Output"){if(s.allowPermanentAbsence)return;throw new Error(`Output field not found ${s.field}.`)}(g=this.dynamicFieldListChanged)==null||g.attachWatcher(e),!this._finalState&&!s.stableIfNotFound&&i("field_not_found:"+s.field);return}else{if(s.assertFieldType!==void 0&&a.type!==s.assertFieldType)throw new Error(`Unexpected field type: expected ${s.assertFieldType} but got ${a.type} for the field name ${s.field}`);const f={};return l.isNotNullResourceId(a.value)&&(f.value=a.value),l.isNotNullResourceId(a.error)&&(f.error=a.error),f.value===void 0&&f.error===void 0&&i("field_not_resolved:"+s.field),a.change.attachWatcher(e),f}}getInputsLocked(e){var t;return this.inputsLocked||(t=this.resourceStateChange)==null||t.attachWatcher(e),this.inputsLocked}getOutputsLocked(e){var t;return this.outputsLocked||(t=this.resourceStateChange)==null||t.attachWatcher(e),this.outputsLocked}get isReadyOrError(){return this.error!==l.NullResourceId||this.resourceReady||this.originalResourceId!==l.NullResourceId}getIsFinal(e){var t;return(t=this.finalChanged)==null||t.attachWatcher(e),this._finalState}getIsReadyOrError(e){var t;return this.isReadyOrError||(t=this.resourceStateChange)==null||t.attachWatcher(e),this.isReadyOrError}getError(e){var t;if(l.isNullResourceId(this.error)){(t=this.resourceStateChange)==null||t.attachWatcher(e);return}else return this.error}listInputFields(e){var i;const t=[];return this.fieldsMap.forEach((s,a)=>{(s.type==="Input"||s.type==="Service")&&t.push(a)}),this.inputsLocked||(i=this.inputAndServiceFieldListChanged)==null||i.attachWatcher(e),t}listOutputFields(e){var i;const t=[];return this.fieldsMap.forEach((s,a)=>{s.type==="Output"&&t.push(a)}),this.outputsLocked||(i=this.outputFieldListChanged)==null||i.attachWatcher(e),t}listDynamicFields(e){var i;const t=[];return this.fieldsMap.forEach((s,a)=>{s.type!=="Input"&&s.type!=="Output"&&t.push(a)}),(i=this.dynamicFieldListChanged)==null||i.attachWatcher(e),t}getKeyValue(e,t){var i;return(i=this.kvChanged)==null||i.attachWatcher(e),this.kv.get(t)}getKeyValueString(e,t){const i=this.getKeyValue(e,t);if(i!==void 0)return $.decode(i)}getDataAsString(){if(this.data!==void 0)return this.dataAsString===void 0&&(this.dataAsString=$.decode(this.data)),this.dataAsString}getDataAsJson(){if(this.data!==void 0)return this.dataAsJson===void 0&&(this.dataAsJson=JSON.parse(this.getDataAsString())),this.dataAsJson}verifyReadyState(){if(this.resourceReady&&!this.inputsLocked)throw new Error(`ready without input or output lock: ${l.stringifyWithResourceId(this.state)}`)}get state(){return{id:this.id,kind:this.kind,type:this.type,data:this.data,resourceReady:this.resourceReady,inputsLocked:this.inputsLocked,outputsLocked:this.outputsLocked,error:this.error,originalResourceId:this.originalResourceId,final:this.finalFlag}}markFinal(){this._finalState||(this._finalState=!0,v.notEmpty(this.finalChanged).markChanged(),this.finalChanged=void 0,this.resourceStateChange=void 0,this.dynamicFieldListChanged=void 0,this.inputAndServiceFieldListChanged=void 0,this.outputFieldListChanged=void 0,this.lockedChange=void 0)}markAllChanged(){var e,t,i,s,a,n,r;this.fieldsMap.forEach(g=>g.change.markChanged()),(e=this.finalChanged)==null||e.markChanged(),(t=this.resourceStateChange)==null||t.markChanged(),(i=this.lockedChange)==null||i.markChanged(),(s=this.inputAndServiceFieldListChanged)==null||s.markChanged(),(a=this.outputFieldListChanged)==null||a.markChanged(),(n=this.dynamicFieldListChanged)==null||n.markChanged(),(r=this.kvChanged)==null||r.markChanged(),this.resourceRemoved.markChanged()}}class I{constructor(e,t){c(this,"resources",new Map);c(this,"resourcesAdded",new m.ChangeSource);c(this,"_isValid",!0);c(this,"invalidationMessage");this.root=e,this.isFinalPredicate=t}forEachResource(e){this.resources.forEach(t=>e(t))}checkValid(){if(!this._isValid)throw new Error(this.invalidationMessage??"tree is in invalid state")}get(e,t){this.checkValid();const i=this.resources.get(t);if(i===void 0)throw this.resourcesAdded.attachWatcher(e),new Error(`resource ${l.resourceIdToString(t)} not found in the tree`);return i.resourceRemoved.attachWatcher(e),i}updateFromResourceData(e,t=!1){this.checkValid();const i=[],s=[];for(const n of e){let r=this.resources.get(n.id);const g=r==null?void 0:r.state,f=h=>{const{fields:k,...u}=n;throw this.invalidateTree(),new w(`Unexpected resource state transition (${h}): ${l.stringifyWithResourceId(u)} -> ${l.stringifyWithResourceId(g)}`)};if(r!==void 0){r.finalState&&f("resource state can be updated after it is marked as final");let h=!1;r.version+=1,r.originalResourceId!==n.originalResourceId&&(r.originalResourceId!==l.NullResourceId&&f("originalResourceId can't change after it is set"),r.originalResourceId=n.originalResourceId,v.notEmpty(r.resourceStateChange).markChanged(),h=!0),r.error!==n.error&&(l.isNotNullResourceId(r.error)&&f("resource can't change attached error after it is set"),r.error=n.error,i.push(r.error),v.notEmpty(r.resourceStateChange).markChanged(),h=!0);for(const u of n.fields){let d=r.fieldsMap.get(u.name);d?(d.type!==u.type&&(d.type!=="Dynamic"&&f(`field changed type ${d.type} -> ${u.type}`),v.notEmpty(r.dynamicFieldListChanged).markChanged(),(d.type==="Input"||d.type==="Service")&&(r.inputsLocked&&f(`adding input field "${u.name}", while corresponding list is locked`),v.notEmpty(r.inputAndServiceFieldListChanged).markChanged()),d.type==="Output"&&(r.outputsLocked&&f(`adding output field "${u.name}", while corresponding list is locked`),v.notEmpty(r.outputFieldListChanged).markChanged()),d.type=u.type,d.change.markChanged(),h=!0),d.value!==u.value&&(l.isNotNullResourceId(d.value)&&s.push(d.value),d.value=u.value,l.isNotNullResourceId(u.value)&&i.push(u.value),d.change.markChanged(),h=!0),d.error!==u.error&&(l.isNotNullResourceId(d.error)&&s.push(d.error),d.error=u.error,l.isNotNullResourceId(u.error)&&i.push(u.error),d.change.markChanged(),h=!0),d.status!==u.status&&(d.status=u.status,d.change.markChanged(),h=!0),d.valueIsFinal!==u.valueIsFinal&&(d.valueIsFinal=u.valueIsFinal,d.change.markChanged(),h=!0),d.resourceVersion=r.version):(d=new A(u.name,u.type,u.value,u.error,u.status,u.valueIsFinal,r.version),l.isNotNullResourceId(u.value)&&i.push(u.value),l.isNotNullResourceId(u.error)&&i.push(u.error),u.type==="Input"||u.type==="Service"?(r.inputsLocked&&f(`adding ${u.type} (${u.name}) field while inputs locked`),v.notEmpty(r.inputAndServiceFieldListChanged).markChanged()):u.type==="Output"?(r.outputsLocked&&f(`adding ${u.type} (${u.name}) field while outputs locked`),v.notEmpty(r.outputFieldListChanged).markChanged()):v.notEmpty(r.dynamicFieldListChanged).markChanged(),r.fieldsMap.set(u.name,d),h=!0)}if(r.fieldsMap.forEach((u,d,p)=>{u.resourceVersion!==r.version&&((u.type==="Input"||u.type==="Service"||u.type==="Output")&&f(`removal of ${u.type} field ${d}`),u.change.markChanged(),p.delete(d),l.isNotNullResourceId(u.value)&&s.push(u.value),l.isNotNullResourceId(u.error)&&s.push(u.error),v.notEmpty(r.dynamicFieldListChanged).markChanged())}),r.inputsLocked!==n.inputsLocked&&(r.inputsLocked&&f("inputs unlocking is not permitted"),r.inputsLocked=n.inputsLocked,v.notEmpty(r.lockedChange).markChanged(),h=!0),r.outputsLocked!==n.outputsLocked&&(r.outputsLocked&&f("outputs unlocking is not permitted"),r.outputsLocked=n.outputsLocked,v.notEmpty(r.lockedChange).markChanged(),h=!0),r.resourceReady!==n.resourceReady){const u=r.resourceReady;r.resourceReady=n.resourceReady,r.verifyReadyState(),r.isReadyOrError||f(`resource can't lose it's ready or error state (ready state before ${u})`),v.notEmpty(r.resourceStateChange).markChanged(),h=!0}let k=!1;for(const u of n.kv){const d=r.kv.get(u.key);(d===void 0||Buffer.compare(d,u.value)!==0)&&(r.kv.set(u.key,u.value),k=!0)}if(r.kv.size>n.kv.length){const u=new Set(n.kv.map(d=>d.key));r.kv.forEach((d,p,y)=>{u.has(p)||y.delete(p)}),k=!0}k&&v.notEmpty(r.kvChanged).markChanged(),h&&(r.dataVersion=r.version,this.isFinalPredicate(r)&&r.markFinal())}else{r=new b(n),r.verifyReadyState(),l.isNotNullResourceId(r.error)&&i.push(r.error);for(const h of n.fields){const k=new A(h.name,h.type,h.value,h.error,h.status,h.valueIsFinal,E);l.isNotNullResourceId(h.value)&&i.push(h.value),l.isNotNullResourceId(h.error)&&i.push(h.error),r.fieldsMap.set(h.name,k)}for(const h of n.kv)r.kv.set(h.key,h.value);this.isFinalPredicate(r)&&r.markFinal(),this.resources.set(r.id,r),this.resourcesAdded.markChanged()}}for(const n of i){const r=this.resources.get(n);if(!r)throw this.invalidateTree(),new w(`orphan resource ${n}`);r.refCount++}let a=s;for(;a.length>0;){const n=[];for(const r of a){const g=this.resources.get(r);if(!g)throw this.invalidateTree(),new w(`orphan resource ${r}`);g.refCount--,g.refCount===0&&g.id!==this.root&&(g.fieldsMap.forEach(f=>{l.isNotNullResourceId(f.value)&&n.push(f.value),l.isNotNullResourceId(f.error)&&n.push(f.error),f.change.markChanged()}),l.isNotNullResourceId(g.error)&&n.push(g.error),g.resourceRemoved.markChanged(),this.resources.delete(r))}a=n}if(!t){for(const n of e)if(!this.resources.has(n.id))throw this.invalidateTree(),new w(`orphan input resource ${n.id}`)}}accessor(e=this.root){return this.checkValid(),this.entry(e)}entry(e=this.root){return this.checkValid(),new S({treeProvider:()=>this},e)}invalidateTree(e){this._isValid=!1,this.invalidationMessage=e,this.resources.forEach(t=>{t.markAllChanged()})}}function x(o,e){const t=[],i=new Set;return o.forEachResource(s=>{s.finalState?i.add(s.id):t.push(s.id)}),t.length===0&&i.size===0&&t.push(o.root),{seedResources:t,finalResources:i,pruningFunction:e}}function C(){return{requests:0,roundTrips:0,retrievedResources:0,retrievedFields:0,retrievedKeyValues:0,retrievedResourceDataBytes:0,retrievedKeyValueBytes:0,prunedFields:0,finalResourcesSkipped:0,millisSpent:0}}function Q(o){let e=`Requests: ${o.requests}
|
|
2
|
-
`;return e+=`Total time: ${
|
|
1
|
+
"use strict";var K=Object.defineProperty;var M=(o,e,t)=>e in o?K(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var c=(o,e,t)=>M(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("@milaboratories/pl-client"),m=require("@milaboratories/computable"),O=require("@milaboratories/pl-errors"),p=require("@milaboratories/ts-helpers"),q=require("denque"),U=require("node:timers/promises");function J(o){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const t in o)if(t!=="default"){const i=Object.getOwnPropertyDescriptor(o,t);Object.defineProperty(e,t,i.get?i:{enumerable:!0,get:()=>o[t]})}}return e.default=o,Object.freeze(e)}const B=J(U);function z(o,e){if(o!==void 0)return F(o,e)}function F(o,e){const t={};return o.value!==void 0&&(t.value=e(o.value)),o.error!==void 0&&(t.error=e(o.error)),t}function j(o){return typeof o=="object"&&o!==null&&o.__pl_tree_type_marker__==="PlTreeEntry"}function H(o){return typeof o=="object"&&o!==null&&o.__pl_tree_type_marker__==="PlTreeEntryAccessor"}function Y(o){return typeof o=="object"&&o!==null&&o.__pl_tree_type_marker__==="PlTreeNodeAccessor"}class S{constructor(e,t){c(this,"__pl_tree_type_marker__","PlTreeEntry");this.accessorData=e,this.rid=t}createAccessor(e,t){return new T(this.accessorData,this.accessorData.treeProvider(),this.rid,{ctx:e,guard:t})}toJSON(){return this.toString()}toString(){return`[ENTRY:${g.resourceIdToString(this.rid)}]`}}function P(o,e,t,i,s){const n=new $(o,e,e.get(t.ctx.watcher,i),t);if(!s.ignoreError){const a=n.getError();if(a!==void 0)throw O.parsePlError(p.notEmpty(a.getDataAsString()),n.id,n.resourceType)}if(s.assertResourceType!==void 0&&(Array.isArray(s.assertResourceType)?s.assertResourceType.findIndex(a=>g.resourceTypesEqual(a,n.resourceType))===-1:!g.resourceTypesEqual(s.assertResourceType,n.resourceType)))throw new Error(`wrong resource type ${g.resourceTypeToString(n.resourceType)} but expected ${s.assertResourceType}`);return n}class T{constructor(e,t,i,s){c(this,"__pl_tree_type_marker__","PlTreeEntryAccessor");this.accessorData=e,this.tree=t,this.rid=i,this.instanceData=s}node(e={}){return this.instanceData.guard(),this.accessorData.hooks!==void 0&&this.instanceData.ctx.attacheHooks(this.accessorData.hooks),P(this.accessorData,this.tree,this.instanceData,this.rid,e)}}function G(o,e){return o instanceof S?e.accessor(o).node().resourceInfo:o}class ${constructor(e,t,i,s){c(this,"__pl_tree_type_marker__","PlTreeNodeAccessor");c(this,"onUnstableLambda",e=>{this.instanceData.ctx.markUnstable(e)});this.accessorData=e,this.tree=t,this.resource=i,this.instanceData=s}get id(){return this.instanceData.guard(),this.resource.id}get originalId(){return this.instanceData.guard(),this.resource.originalResourceId}get resourceType(){return this.instanceData.guard(),this.resource.type}get resourceInfo(){return{id:this.id,type:this.resourceType}}getResourceFromTree(e,t){return P(this.accessorData,this.tree,this.instanceData,e,t)}traverse(...e){return this.traverseWithCommon({},...e)}traverseOrError(...e){return this.traverseOrErrorWithCommon({},...e)}traverseWithCommon(e,...t){const i=this.traverseOrErrorWithCommon(e,...t);if(i!==void 0){if(!i.ok)throw i.error;return i.value}}traverseOrErrorWithCommon(e,...t){let i=this;for(const s of t){const n=typeof s=="string"?{...e,field:s}:{...e,...s},a=i.getField(s);if(a===void 0||n.pureFieldErrorToUndefined&&a.value===void 0&&a.error!==void 0)return;if((!n.ignoreError||a.value===void 0)&&a.error!==void 0)return{ok:!1,error:O.parsePlError(p.notEmpty(a.error.getDataAsString()),i.id,i.resourceType,n.field)};if(a.value===void 0){if(n.errorIfFieldNotSet)return{ok:!1,error:`field have no assigned value ${n.field} of ${g.resourceIdToString(i.id)}`};this.onUnstableLambda("unpopulated_field:"+n.field);return}i=a.value}return{ok:!0,value:i}}getField(e){this.instanceData.guard();const t=typeof e=="string"?{field:e}:e,i=this.resource.getField(this.instanceData.ctx.watcher,t,this.onUnstableLambda);if(i!==void 0)return F(i,s=>this.getResourceFromTree(s,{ignoreError:!0}))}getInputsLocked(){this.instanceData.guard();const e=this.resource.getInputsLocked(this.instanceData.ctx.watcher);return e||this.instanceData.ctx.markUnstable("inputs_unlocked:"+this.resourceType.name),e}getOutputsLocked(){this.instanceData.guard();const e=this.resource.getOutputsLocked(this.instanceData.ctx.watcher);return e||this.instanceData.ctx.markUnstable("outputs_unlocked:"+this.resourceType.name),e}getIsReadyOrError(){this.instanceData.guard();const e=this.resource.getIsReadyOrError(this.instanceData.ctx.watcher);return e||this.instanceData.ctx.markUnstable("not_ready:"+this.resourceType.name),e}getIsFinal(){return this.instanceData.guard(),this.resource.getIsFinal(this.instanceData.ctx.watcher)}getError(){this.instanceData.guard();const e=this.resource.getError(this.instanceData.ctx.watcher);if(e!==void 0)return this.getResourceFromTree(e,{})}getData(){return this.resource.data}getDataAsString(){return this.resource.getDataAsString()}getDataAsJson(){return this.resource.getDataAsJson()}listInputFields(){return this.instanceData.guard(),this.getInputsLocked(),this.resource.listInputFields(this.instanceData.ctx.watcher)}listOutputFields(){return this.instanceData.guard(),this.getOutputsLocked(),this.resource.listOutputFields(this.instanceData.ctx.watcher)}listDynamicFields(){return this.instanceData.guard(),this.resource.listDynamicFields(this.instanceData.ctx.watcher)}getKeyValue(e,t=!1){this.instanceData.guard();const i=this.resource.getKeyValue(this.instanceData.ctx.watcher,e);return i===void 0&&t&&this.instanceData.ctx.markUnstable("key_not_found_b:"+e),i}getKeyValueString(e){return this.getKeyValueAsString(e)}getKeyValueAsString(e,t=!1){this.instanceData.guard();const i=this.resource.getKeyValueString(this.instanceData.ctx.watcher,e);return i===void 0&&t&&this.instanceData.ctx.markUnstable("key_not_found_s:"+e),i}getKeyValueAsJson(e,t=!1){const i=this.resource.getKeyValueString(this.instanceData.ctx.watcher,e);if(i===void 0){t&&this.instanceData.ctx.markUnstable("key_not_found_j:"+e);return}return JSON.parse(i)}toEntryAccessor(){return new T(this.accessorData,this.tree,this.id,this.instanceData)}persist(){return new S(this.accessorData,this.resource.id)}}class w extends Error{constructor(e){super(e)}}class _{constructor(e,t,i,s,n,a,r){c(this,"change",new m.ChangeSource);this.name=e,this.type=t,this.value=i,this.error=s,this.status=n,this.valueIsFinal=a,this.resourceVersion=r}}const E=0,A=new TextDecoder;class b{constructor(e,t){c(this,"refCount",0);c(this,"version",E);c(this,"dataVersion",E);c(this,"fieldsMap",new Map);c(this,"kv",new Map);c(this,"resourceRemoved",new m.ChangeSource);c(this,"finalChanged",new m.ChangeSource);c(this,"resourceStateChange",new m.ChangeSource);c(this,"lockedChange",new m.ChangeSource);c(this,"inputAndServiceFieldListChanged",new m.ChangeSource);c(this,"outputFieldListChanged",new m.ChangeSource);c(this,"dynamicFieldListChanged",new m.ChangeSource);c(this,"kvChanged",new m.ChangeSource);c(this,"id");c(this,"originalResourceId");c(this,"kind");c(this,"type");c(this,"data");c(this,"dataAsString");c(this,"dataAsJson");c(this,"error");c(this,"inputsLocked");c(this,"outputsLocked");c(this,"resourceReady");c(this,"finalFlag");c(this,"_finalState",!1);c(this,"logger");this.id=e.id,this.originalResourceId=e.originalResourceId,this.kind=e.kind,this.type=e.type,this.data=e.data,this.error=e.error,this.inputsLocked=e.inputsLocked,this.outputsLocked=e.outputsLocked,this.resourceReady=e.resourceReady,this.finalFlag=e.final,this.logger=t}info(e){this.logger!==void 0&&this.logger.info(e)}warn(e){this.logger!==void 0&&this.logger.warn(e)}get final(){return this.finalFlag}get finalState(){return this._finalState}get fields(){return[...this.fieldsMap.values()]}getField(e,t,i=()=>{}){var a,r,f;const s=typeof t=="string"?{field:t}:t,n=this.fieldsMap.get(s.field);if(n===void 0){if(s.errorIfFieldNotFound||s.errorIfFieldNotSet)throw new Error(`Field "${s.field}" not found in resource ${g.resourceIdToString(this.id)}`);if(!this.inputsLocked)(a=this.inputAndServiceFieldListChanged)==null||a.attachWatcher(e);else if(s.assertFieldType==="Service"||s.assertFieldType==="Input"){if(s.allowPermanentAbsence)return;throw new Error(`Service or input field not found ${s.field}.`)}if(!this.outputsLocked)(r=this.outputFieldListChanged)==null||r.attachWatcher(e);else if(s.assertFieldType==="Output"){if(s.allowPermanentAbsence)return;throw new Error(`Output field not found ${s.field}.`)}(f=this.dynamicFieldListChanged)==null||f.attachWatcher(e),!this._finalState&&!s.stableIfNotFound&&i("field_not_found:"+s.field);return}else{if(s.assertFieldType!==void 0&&n.type!==s.assertFieldType)throw new Error(`Unexpected field type: expected ${s.assertFieldType} but got ${n.type} for the field name ${s.field}`);const l={};return g.isNotNullResourceId(n.value)&&(l.value=n.value),g.isNotNullResourceId(n.error)&&(l.error=n.error),l.value===void 0&&l.error===void 0&&i("field_not_resolved:"+s.field),n.change.attachWatcher(e),l}}getInputsLocked(e){var t;return this.inputsLocked||(t=this.resourceStateChange)==null||t.attachWatcher(e),this.inputsLocked}getOutputsLocked(e){var t;return this.outputsLocked||(t=this.resourceStateChange)==null||t.attachWatcher(e),this.outputsLocked}get isReadyOrError(){return this.error!==g.NullResourceId||this.resourceReady||this.originalResourceId!==g.NullResourceId}getIsFinal(e){var t;return(t=this.finalChanged)==null||t.attachWatcher(e),this._finalState}getIsReadyOrError(e){var t;return this.isReadyOrError||(t=this.resourceStateChange)==null||t.attachWatcher(e),this.isReadyOrError}getError(e){var t;if(g.isNullResourceId(this.error)){(t=this.resourceStateChange)==null||t.attachWatcher(e);return}else return this.error}listInputFields(e){var i;const t=[];return this.fieldsMap.forEach((s,n)=>{(s.type==="Input"||s.type==="Service")&&t.push(n)}),this.inputsLocked||(i=this.inputAndServiceFieldListChanged)==null||i.attachWatcher(e),t}listOutputFields(e){var i;const t=[];return this.fieldsMap.forEach((s,n)=>{s.type==="Output"&&t.push(n)}),this.outputsLocked||(i=this.outputFieldListChanged)==null||i.attachWatcher(e),t}listDynamicFields(e){var i;const t=[];return this.fieldsMap.forEach((s,n)=>{s.type!=="Input"&&s.type!=="Output"&&t.push(n)}),(i=this.dynamicFieldListChanged)==null||i.attachWatcher(e),t}getKeyValue(e,t){var i;return(i=this.kvChanged)==null||i.attachWatcher(e),this.kv.get(t)}getKeyValueString(e,t){const i=this.getKeyValue(e,t);if(i!==void 0)return A.decode(i)}getDataAsString(){if(this.data!==void 0)return this.dataAsString===void 0&&(this.dataAsString=A.decode(this.data)),this.dataAsString}getDataAsJson(){if(this.data!==void 0)return this.dataAsJson===void 0&&(this.dataAsJson=JSON.parse(this.getDataAsString())),this.dataAsJson}verifyReadyState(){if(this.resourceReady&&!this.inputsLocked)throw new Error(`ready without input or output lock: ${g.stringifyWithResourceId(this.state)}`)}get state(){return{id:this.id,kind:this.kind,type:this.type,data:this.data,resourceReady:this.resourceReady,inputsLocked:this.inputsLocked,outputsLocked:this.outputsLocked,error:this.error,originalResourceId:this.originalResourceId,final:this.finalFlag}}markFinal(){this._finalState||(this._finalState=!0,p.notEmpty(this.finalChanged).markChanged(),this.finalChanged=void 0,this.resourceStateChange=void 0,this.dynamicFieldListChanged=void 0,this.inputAndServiceFieldListChanged=void 0,this.outputFieldListChanged=void 0,this.lockedChange=void 0)}markAllChanged(){var e,t,i,s,n,a,r;this.fieldsMap.forEach(f=>f.change.markChanged()),(e=this.finalChanged)==null||e.markChanged(),(t=this.resourceStateChange)==null||t.markChanged(),(i=this.lockedChange)==null||i.markChanged(),(s=this.inputAndServiceFieldListChanged)==null||s.markChanged(),(n=this.outputFieldListChanged)==null||n.markChanged(),(a=this.dynamicFieldListChanged)==null||a.markChanged(),(r=this.kvChanged)==null||r.markChanged(),this.resourceRemoved.markChanged()}}class I{constructor(e,t){c(this,"resources",new Map);c(this,"resourcesAdded",new m.ChangeSource);c(this,"_isValid",!0);c(this,"invalidationMessage");this.root=e,this.isFinalPredicate=t}forEachResource(e){this.resources.forEach(t=>e(t))}checkValid(){if(!this._isValid)throw new Error(this.invalidationMessage??"tree is in invalid state")}get(e,t){this.checkValid();const i=this.resources.get(t);if(i===void 0)throw this.resourcesAdded.attachWatcher(e),new Error(`resource ${g.resourceIdToString(t)} not found in the tree`);return i.resourceRemoved.attachWatcher(e),i}updateFromResourceData(e,t=!1){this.checkValid();const i=[],s=[];for(const a of e){let r=this.resources.get(a.id);const f=r==null?void 0:r.state,l=h=>{const{fields:k,...u}=a;throw this.invalidateTree(),new w(`Unexpected resource state transition (${h}): ${g.stringifyWithResourceId(u)} -> ${g.stringifyWithResourceId(f)}`)};if(r!==void 0){r.finalState&&l("resource state can be updated after it is marked as final");let h=!1;r.version+=1,r.originalResourceId!==a.originalResourceId&&(r.originalResourceId!==g.NullResourceId&&l("originalResourceId can't change after it is set"),r.originalResourceId=a.originalResourceId,p.notEmpty(r.resourceStateChange).markChanged(),h=!0),r.error!==a.error&&(g.isNotNullResourceId(r.error)&&l("resource can't change attached error after it is set"),r.error=a.error,i.push(r.error),p.notEmpty(r.resourceStateChange).markChanged(),h=!0);for(const u of a.fields){let d=r.fieldsMap.get(u.name);d?(d.type!==u.type&&(d.type!=="Dynamic"&&l(`field changed type ${d.type} -> ${u.type}`),p.notEmpty(r.dynamicFieldListChanged).markChanged(),(d.type==="Input"||d.type==="Service")&&(r.inputsLocked&&l(`adding input field "${u.name}", while corresponding list is locked`),p.notEmpty(r.inputAndServiceFieldListChanged).markChanged()),d.type==="Output"&&(r.outputsLocked&&l(`adding output field "${u.name}", while corresponding list is locked`),p.notEmpty(r.outputFieldListChanged).markChanged()),d.type=u.type,d.change.markChanged(),h=!0),d.value!==u.value&&(g.isNotNullResourceId(d.value)&&s.push(d.value),d.value=u.value,g.isNotNullResourceId(u.value)&&i.push(u.value),d.change.markChanged(),h=!0),d.error!==u.error&&(g.isNotNullResourceId(d.error)&&s.push(d.error),d.error=u.error,g.isNotNullResourceId(u.error)&&i.push(u.error),d.change.markChanged(),h=!0),d.status!==u.status&&(d.status=u.status,d.change.markChanged(),h=!0),d.valueIsFinal!==u.valueIsFinal&&(d.valueIsFinal=u.valueIsFinal,d.change.markChanged(),h=!0),d.resourceVersion=r.version):(d=new _(u.name,u.type,u.value,u.error,u.status,u.valueIsFinal,r.version),g.isNotNullResourceId(u.value)&&i.push(u.value),g.isNotNullResourceId(u.error)&&i.push(u.error),u.type==="Input"||u.type==="Service"?(r.inputsLocked&&l(`adding ${u.type} (${u.name}) field while inputs locked`),p.notEmpty(r.inputAndServiceFieldListChanged).markChanged()):u.type==="Output"?(r.outputsLocked&&l(`adding ${u.type} (${u.name}) field while outputs locked`),p.notEmpty(r.outputFieldListChanged).markChanged()):p.notEmpty(r.dynamicFieldListChanged).markChanged(),r.fieldsMap.set(u.name,d),h=!0)}if(r.fieldsMap.forEach((u,d,y)=>{u.resourceVersion!==r.version&&((u.type==="Input"||u.type==="Service"||u.type==="Output")&&l(`removal of ${u.type} field ${d}`),u.change.markChanged(),y.delete(d),g.isNotNullResourceId(u.value)&&s.push(u.value),g.isNotNullResourceId(u.error)&&s.push(u.error),p.notEmpty(r.dynamicFieldListChanged).markChanged())}),r.inputsLocked!==a.inputsLocked&&(r.inputsLocked&&l("inputs unlocking is not permitted"),r.inputsLocked=a.inputsLocked,p.notEmpty(r.lockedChange).markChanged(),h=!0),r.outputsLocked!==a.outputsLocked&&(r.outputsLocked&&l("outputs unlocking is not permitted"),r.outputsLocked=a.outputsLocked,p.notEmpty(r.lockedChange).markChanged(),h=!0),r.resourceReady!==a.resourceReady){const u=r.resourceReady;r.resourceReady=a.resourceReady,r.verifyReadyState(),r.isReadyOrError||l(`resource can't lose it's ready or error state (ready state before ${u})`),p.notEmpty(r.resourceStateChange).markChanged(),h=!0}let k=!1;for(const u of a.kv){const d=r.kv.get(u.key);(d===void 0||Buffer.compare(d,u.value)!==0)&&(r.kv.set(u.key,u.value),k=!0)}if(r.kv.size>a.kv.length){const u=new Set(a.kv.map(d=>d.key));r.kv.forEach((d,y,v)=>{u.has(y)||v.delete(y)}),k=!0}k&&p.notEmpty(r.kvChanged).markChanged(),h&&(r.dataVersion=r.version,this.isFinalPredicate(r)&&r.markFinal())}else{r=new b(a),r.verifyReadyState(),g.isNotNullResourceId(r.error)&&i.push(r.error);for(const h of a.fields){const k=new _(h.name,h.type,h.value,h.error,h.status,h.valueIsFinal,E);g.isNotNullResourceId(h.value)&&i.push(h.value),g.isNotNullResourceId(h.error)&&i.push(h.error),r.fieldsMap.set(h.name,k)}for(const h of a.kv)r.kv.set(h.key,h.value);this.isFinalPredicate(r)&&r.markFinal(),this.resources.set(r.id,r),this.resourcesAdded.markChanged()}}for(const a of i){const r=this.resources.get(a);if(!r)throw this.invalidateTree(),new w(`orphan resource ${a}`);r.refCount++}let n=s;for(;n.length>0;){const a=[];for(const r of n){const f=this.resources.get(r);if(!f)throw this.invalidateTree(),new w(`orphan resource ${r}`);f.refCount--,f.refCount===0&&f.id!==this.root&&(f.fieldsMap.forEach(l=>{g.isNotNullResourceId(l.value)&&a.push(l.value),g.isNotNullResourceId(l.error)&&a.push(l.error),l.change.markChanged()}),g.isNotNullResourceId(f.error)&&a.push(f.error),f.resourceRemoved.markChanged(),this.resources.delete(r))}n=a}if(!t){for(const a of e)if(!this.resources.has(a.id))throw this.invalidateTree(),new w(`orphan input resource ${a.id}`)}}accessor(e=this.root){return this.checkValid(),this.entry(e)}entry(e=this.root){return this.checkValid(),new S({treeProvider:()=>this},e)}invalidateTree(e){this._isValid=!1,this.invalidationMessage=e,this.resources.forEach(t=>{t.markAllChanged()})}}function x(o,e){const t=[],i=new Set;return o.forEachResource(s=>{s.finalState?i.add(s.id):t.push(s.id)}),t.length===0&&i.size===0&&t.push(o.root),{seedResources:t,finalResources:i,pruningFunction:e}}function C(){return{requests:0,roundTrips:0,retrievedResources:0,retrievedFields:0,retrievedKeyValues:0,retrievedResourceDataBytes:0,retrievedKeyValueBytes:0,prunedFields:0,finalResourcesSkipped:0,millisSpent:0}}function Q(o){let e=`Requests: ${o.requests}
|
|
2
|
+
`;return e+=`Total time: ${p.msToHumanReadable(o.millisSpent)}
|
|
3
3
|
`,e+=`Round-trips: ${o.roundTrips}
|
|
4
4
|
`,e+=`Resources: ${o.retrievedResources}
|
|
5
5
|
`,e+=`Fields: ${o.retrievedFields}
|
|
@@ -7,5 +7,5 @@
|
|
|
7
7
|
`,e+=`Data Bytes: ${o.retrievedResourceDataBytes}
|
|
8
8
|
`,e+=`KV Bytes: ${o.retrievedKeyValueBytes}
|
|
9
9
|
`,e+=`Pruned fields: ${o.prunedFields}
|
|
10
|
-
`,e+=`Final resources skipped: ${o.finalResourcesSkipped}`,e}async function V(o,e,t){var d;const i=Date.now();t&&t.requests++;const{seedResources:s,finalResources:
|
|
10
|
+
`,e+=`Final resources skipped: ${o.finalResourcesSkipped}`,e}async function V(o,e,t){var d;const i=Date.now();t&&t.requests++;const{seedResources:s,finalResources:n,pruningFunction:a}=e,r=new q;let f=!0,l=0;const h=new Set,k=y=>{if(g.isNullResourceId(y)||h.has(y))return;if(n.has(y)){t&&t.finalResourcesSkipped++;return}h.add(y);const v=o.getResourceDataIfExists(y,!0),R=o.listKeyValuesIfResourceExists(y),W=f;f&&(f=!1),r.push((async()=>{const[L,N]=await Promise.all([v,R]);if(W&&(l++,f=!0),L!==void 0){if(N===void 0)throw new Error("Inconsistent replies");return{...L,kv:N}}})())};s.forEach(y=>k(y));const u=[];for(;;){const y=r.shift();if(y===void 0)break;let v=await y;if(v!==void 0){if(a!==void 0){const R=a(v);t&&(t.prunedFields+=v.fields.length-R.length),v={...v,fields:R}}k(v.error);for(const R of v.fields)k(R.value),k(R.error);if(t){t.retrievedResources++,t.retrievedFields+=v.fields.length,t.retrievedKeyValues+=v.kv.length,t.retrievedResourceDataBytes+=((d=v.data)==null?void 0:d.length)??0;for(const R of v.kv)t.retrievedKeyValueBytes+=R.value.length}u.push(v)}}return t&&(t.millisSpent+=Date.now()-i,t.roundTrips+=l),u}function X(o){return o}function Z(o,e,t){var a;const i=o instanceof S?p.notEmpty(t).accessor(o).node():o instanceof T?o.node():o,n={...i.resourceInfo};if(e.data!==void 0&&(e.data==="raw"?n.data=i.getData():n.data=e.data.parse(i.getDataAsJson())),e.fields!==void 0){const r={};for(const[f,l]of Object.entries(e.fields))r[f]=(a=i.traverse({field:f,errorIfFieldNotSet:l,stableIfNotFound:!l}))==null?void 0:a.id;n.fields=r}if(e.kv!==void 0){const r={};for(const[f,l]of Object.entries(e.kv)){const h=i.getKeyValue(f);if(h===void 0)throw new Error(`Key not found ${f}`);l==="raw"?r[f]=h:r[f]=l.parse(JSON.parse(Buffer.from(h).toString("utf-8")))}n.kv=r}return n}function ee(o,e,t){if(o instanceof S){const i=t.accessor(o).node(),s=i.resourceInfo,n=e.map(a=>{var r,f;return[a,(f=(r=i.getField(a))==null?void 0:r.value)==null?void 0:f.id]});return{...s,fields:new Map(n),data:i.getData()??new Uint8Array}}return o}function te(o,e,t){if(!(o instanceof S))return o;const i=t.accessor(o).node(),s=i.resourceInfo,n=e.map(a=>[a,i.getKeyValue(a)]);return{...s,metadata:Object.fromEntries(n)}}class D{constructor(e,t,i,s){c(this,"finalPredicate");c(this,"state");c(this,"pollingInterval");c(this,"pruning");c(this,"logStat");c(this,"hooks");c(this,"abortController",new AbortController);c(this,"scheduledOnNextState",[]);c(this,"keepRunning",!1);c(this,"currentLoop");c(this,"terminated",!1);this.pl=e,this.root=t,this.logger=s;const{finalPredicateOverride:n,pruning:a,pollingInterval:r,stopPollingDelay:f,logStat:l}=i;this.pruning=a,this.pollingInterval=r,this.finalPredicate=n??e.finalPredicate,this.logStat=l,this.state=new I(t,this.finalPredicate),this.hooks=new m.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:f},(h,k)=>this.scheduleOnNextState(h,k))}accessor(e=this.root){if(this.terminated)throw new Error("tree synchronization is terminated");return this.entry(e)}entry(e=this.root){if(this.terminated)throw new Error("tree synchronization is terminated");return new S({treeProvider:()=>this.state,hooks:this.hooks},e)}async refreshState(){if(this.terminated)throw new Error("tree synchronization is terminated");await this.hooks.refreshState()}scheduleOnNextState(e,t){this.terminated?t(new Error("tree synchronization is terminated")):this.scheduledOnNextState.push({resolve:e,reject:t})}startUpdating(){this.terminated||(this.keepRunning=!0,this.currentLoop===void 0&&(this.currentLoop=this.mainLoop()))}stopUpdating(){this.keepRunning=!1}async refresh(e){if(this.terminated)throw new Error("tree synchronization is terminated");const t=x(this.state,this.pruning),i=await this.pl.withReadTx("ReadingTree",async s=>await V(s,t,e));this.state.updateFromResourceData(i,!0)}async mainLoop(){var t,i;let e=this.logStat?C():void 0;for(;this.keepRunning;){let s;this.scheduledOnNextState.length>0&&(s=this.scheduledOnNextState,this.scheduledOnNextState=[]);try{if(this.logStat==="per-request"&&(e=C()),await this.refresh(e),e&&this.logger&&this.logger.info(`Tree stat (success): ${JSON.stringify(e)}`),s!==void 0)for(const n of s)n.resolve()}catch(n){if(e&&this.logger&&this.logger.info(`Tree stat (error): ${JSON.stringify(e)}`),s!==void 0)for(const a of s)a.reject(n);if(n instanceof w){(t=this.logger)==null||t.error(n),this.state.invalidateTree("stat update error"),this.state=new I(this.root,this.finalPredicate);continue}else(i=this.logger)==null||i.warn(n)}if(!this.keepRunning||this.terminated)break;try{await B.setTimeout(this.pollingInterval,this.abortController.signal)}catch(n){if(!g.isTimeoutOrCancelError(n))throw new Error("Unexpected error",{cause:n});break}}this.currentLoop=void 0}async terminate(){this.keepRunning=!1,this.terminated=!0,this.abortController.abort(),this.currentLoop!==void 0&&(await this.currentLoop,this.state.invalidateTree("synchronization terminated for the tree"))}async awaitSyncLoopTermination(){this.currentLoop!==void 0&&await this.currentLoop}static async init(e,t,i,s){const n=new D(e,t,i,s);let a=i.logStat?C():void 0,r=!1;try{await n.refresh(a),r=!0}finally{a&&s&&s.info(`Tree stat (initial load, ${r?"success":"failure"}): ${JSON.stringify(a)}`)}return n}}exports.PlTreeEntry=S;exports.PlTreeEntryAccessor=T;exports.PlTreeNodeAccessor=$;exports.PlTreeResource=b;exports.PlTreeState=I;exports.SynchronizedTreeState=D;exports.TreeStateUpdateError=w;exports.constructTreeLoadingRequest=x;exports.formatTreeLoadingStat=Q;exports.initialTreeLoadingStat=C;exports.isPlTreeEntry=j;exports.isPlTreeEntryAccessor=H;exports.isPlTreeNodeAccessor=Y;exports.loadTreeState=V;exports.makeResourceSnapshot=Z;exports.mapValueAndError=F;exports.mapValueAndErrorIfDefined=z;exports.rsSchema=X;exports.treeEntryToResourceInfo=G;exports.treeEntryToResourceWithData=ee;exports.treeEntryToResourceWithMetadata=te;
|
|
11
11
|
//# sourceMappingURL=index.js.map
|