@altostra/core
Version:
Core library for shared types and logic
1 lines • 8.91 kB
JavaScript
"use strict";var _ResourceHelper_instances,_ResourceHelper_resourceUtils,_ResourceHelper_disposed,_ResourceHelper_incomingConnections,_ResourceHelper_outgoingConnections,_ResourceHelper_resCache,_ResourceHelper_connCache,_ResourceHelper_rootPath,_ResourceHelper_throwIfDisposed,__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,s,r,t,i){if("m"===t)throw new TypeError("Private method is not writable");if("a"===t&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof s?e!==s||!i:!s.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===t?i.call(e,r):i?i.value=r:s.set(e,r),r},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,s,r,t){if("a"===r&&!t)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?e!==s||!t:!s.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?t:"a"===r?t.call(e):t?t.value:s.get(e)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ResourceHelper=void 0;const util_1=require("util"),Errors_1=require("../../common/Errors"),Object_1=require("../../common/Utils/Object"),from_1=require("@reactivex/ix-es2015-cjs/iterable/from"),map_1=require("@reactivex/ix-es2015-cjs/iterable/operators/map"),skip_1=require("@reactivex/ix-es2015-cjs/iterable/operators/skip"),BlueprintError_1=require("../Blueprint/BlueprintError"),Parameters_1=require("../Parameters"),ConnectionCollection_1=require("./ConnectionCollection"),ParametersHelper_1=require("./ParametersHelper"),ResourcesUtils_1=require("./ResourcesUtils");class ResourceHelper{constructor(e,s,r,t){this._blueprint=e,this._resource=t,_ResourceHelper_instances.add(this),_ResourceHelper_resourceUtils.set(this,void 0),_ResourceHelper_disposed.set(this,!1),_ResourceHelper_incomingConnections.set(this,void 0),_ResourceHelper_outgoingConnections.set(this,void 0),_ResourceHelper_resCache.set(this,void 0),_ResourceHelper_connCache.set(this,void 0),_ResourceHelper_rootPath.set(this,void 0),__classPrivateFieldSet(this,_ResourceHelper_resCache,s,"f"),__classPrivateFieldSet(this,_ResourceHelper_connCache,r,"f"),__classPrivateFieldSet(this,_ResourceHelper_outgoingConnections,new ConnectionCollection_1.ResourceConnectionsCollection(r,e,t.id,"outgoing"),"f"),__classPrivateFieldSet(this,_ResourceHelper_incomingConnections,new ConnectionCollection_1.ResourceConnectionsCollection(r,e,t.id,"incoming"),"f")}get id(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this._resource.id}get type(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this._resource.type}get actualType(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this.is("resource.external")?this._resource.externalResourceType:this._resource.type}get name(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this._resource.name}get description(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this._resource.description}set description(e){__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this._resource.description=e}get position(){__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this);const e=this._blueprint.blueprint.metadata;if(null==e?void 0:e.designer)return e.designer.resourcePositions[this.id]}set position(e){var s,r;__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this);let t=this._blueprint.blueprint.metadata,i=null==t?void 0:t.designer;e?(t=this._blueprint.blueprint.metadata=null!==(s=this._blueprint.blueprint.metadata)&&void 0!==s?s:{},i=t.designer=null!==(r=t.designer)&&void 0!==r?r:{resourcePositions:{}},i.resourcePositions[this.id]=e):i&&delete i.resourcePositions[this.id]}get resource(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this._resource}get rootPath(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),__classPrivateFieldGet(this,_ResourceHelper_rootPath,"f")||__classPrivateFieldSet(this,_ResourceHelper_rootPath,ParametersHelper_1.ParametersHelper.resourcesRoot.append(this.id),"f"),__classPrivateFieldGet(this,_ResourceHelper_rootPath,"f")}get incomingConnections(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),__classPrivateFieldGet(this,_ResourceHelper_incomingConnections,"f")}get outgoingConnections(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),__classPrivateFieldGet(this,_ResourceHelper_outgoingConnections,"f")}get resourceUtils(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),__classPrivateFieldGet(this,_ResourceHelper_resourceUtils,"f")||__classPrivateFieldSet(this,_ResourceHelper_resourceUtils,(0,ResourcesUtils_1.getResourceUtilities)(this._blueprint,this),"f"),__classPrivateFieldGet(this,_ResourceHelper_resourceUtils,"f")}edit(e){__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this);const s=Object.assign(Object.assign({},this._resource),e);if(delete s.id,s.type!==this.type)throw new BlueprintError_1.BlueprintError("Cannot edit resource type");return Object.assign(this._resource,s),this}parameterizedEdit(e){if(__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),Object.prototype.hasOwnProperty.call(e,"type")&&e.type!==this.type)throw new BlueprintError_1.BlueprintError("Cannot edit resource type");const s=this._blueprint.parameters.setParametersFrom({root:this.rootPath,parameterized:e});return this.edit(s)}is(...e){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),e.includes(this.type)}getParameterizedResource(){__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this);const e=(0,Object_1.deepClone)(this.resource),s=this._blueprint.parameters.getPathParameters(this.rootPath);for(const[r,t]of Object.entries(s)){if(!t)continue;const s=(0,from_1.from)(t.paths).pipe((0,map_1.map)(e=>(0,from_1.from)(new Parameters_1.ParameterPathBuilder(e).parts).pipe((0,skip_1.skip)(ParametersHelper_1.RESOURCE_ROOT_PATH_PARTS_COUNT))),(0,map_1.map)(s=>(0,ParametersHelper_1.resolveParameterPath)(s,e)));for(const{parent:e,key:t}of s)e[t]={$param$:r,defaultValue:e[t]}}return e}setParameterizedResource(e){__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this);const s=this._blueprint.parameters.setParametersFrom({root:this.rootPath,parameterized:e});return this._resource=s,e}assertType(...e){if(__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),!this.is(...e))throw new BlueprintError_1.BlueprintError(`Resource type is not asserted resource type.\nResource type is [${this.type}] while asserted types are [${e.join(", ")}]`)}mapResource(e,s){__classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this);const r=Object.assign(Object.assign({},s(this.resource)),{type:e});this._blueprint.blueprint.resources[r.id]=r;const t=new ResourceHelper(this._blueprint,__classPrivateFieldGet(this,_ResourceHelper_resCache,"f"),__classPrivateFieldGet(this,_ResourceHelper_connCache,"f"),r);return __classPrivateFieldGet(this,_ResourceHelper_resCache,"f").set(r,t),t.id===this.id&&(__classPrivateFieldGet(this,_ResourceHelper_resCache,"f").delete(this.resource),this.dispose()),t}toJSON(){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this.resource}[(_ResourceHelper_resourceUtils=new WeakMap,_ResourceHelper_disposed=new WeakMap,_ResourceHelper_incomingConnections=new WeakMap,_ResourceHelper_outgoingConnections=new WeakMap,_ResourceHelper_resCache=new WeakMap,_ResourceHelper_connCache=new WeakMap,_ResourceHelper_rootPath=new WeakMap,_ResourceHelper_instances=new WeakSet,util_1.inspect.custom)](){return __classPrivateFieldGet(this,_ResourceHelper_instances,"m",_ResourceHelper_throwIfDisposed).call(this),this.resource}dispose(){__classPrivateFieldSet(this,_ResourceHelper_disposed,!0,"f")}}exports.ResourceHelper=ResourceHelper,_ResourceHelper_throwIfDisposed=function(){if(__classPrivateFieldGet(this,_ResourceHelper_disposed,"f"))throw Errors_1.AltoError.create("Cannot use a disposed resource")};