@altostra/core
Version:
Core library for shared types and logic
1 lines • 5.38 kB
JavaScript
"use strict";var _ConnectionHelper_rootPath,_ConnectionHelper_connUtils,__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(e):n?n.value:t.get(e)},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,r,n,o){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?o.call(e,r):o?o.value=r:t.set(e,r),r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ConnectionHelper=void 0;const util_1=require("util"),AltoError_1=require("../../common/Errors/AltoError"),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"),Connections_1=require("../Connections"),Parameters_1=require("../Parameters"),ConnectionsUtils_1=require("./ConnectionsUtils"),ParametersHelper_1=require("./ParametersHelper");class ConnectionHelper{constructor(e,t,r){this._blueprint=e,this._wrappersCache=t,this._connection=r,_ConnectionHelper_rootPath.set(this,void 0),_ConnectionHelper_connUtils.set(this,void 0),this.id=Connections_1.ConnectionId.fromConnection(r)}get isValid(){return this._blueprint.connectionsFactory.isConnectionValid(this.source.actualType,this.target.actualType,this.type)}get type(){return this._connection.type}get source(){const e=this._blueprint.resources.get(this._connection.from);if(!e)throw new BlueprintError_1.BlueprintError(`Could not find connection source resource [${this._connection.from}]`);return e}set source(e){this.edit(Object.assign(Object.assign({},this._connection),{from:e.id}))}get target(){const e=this._blueprint.resources.get(this._connection.to);if(!e)throw new BlueprintError_1.BlueprintError(`Could not find connection source resource [${this._connection.to}]`);return e}set target(e){this.edit(Object.assign(Object.assign({},this._connection),{to:e.id}))}get sourceId(){return this._connection.from}get targetId(){return this._connection.to}get connection(){return this._connection}get rootPath(){return __classPrivateFieldGet(this,_ConnectionHelper_rootPath,"f")||__classPrivateFieldSet(this,_ConnectionHelper_rootPath,ParametersHelper_1.ParametersHelper.connectionsRoot.append(this.sourceId).append(this.targetId),"f"),__classPrivateFieldGet(this,_ConnectionHelper_rootPath,"f")}get connectionUtils(){return __classPrivateFieldGet(this,_ConnectionHelper_connUtils,"f")||__classPrivateFieldSet(this,_ConnectionHelper_connUtils,(0,ConnectionsUtils_1.getConnectionUtilities)(this._blueprint,this),"f"),__classPrivateFieldGet(this,_ConnectionHelper_connUtils,"f")}edit(e){var t;const r=Object.assign(Object.assign({},this._connection),e);if(r.type!==this.type)throw AltoError_1.AltoError.create({message:"Cannot edit connection type!",data:{edit:e,original:this._connection}});if(r.from!==this._connection.from)delete this._blueprint.blueprint.connections[this._connection.from];else if(r.to!==this._connection.to){const e=this._blueprint.blueprint.connections[r.from];e&&delete e[r.to]}Object.assign(this._connection,r);return(this._blueprint.blueprint.connections[r.from]=null!==(t=this._blueprint.blueprint.connections[r.from])&&void 0!==t?t:{})[r.to]=this._connection,this}parameterizedEdit(e){if(Object.prototype.hasOwnProperty.call(e,"type")&&e.type!==this.type)throw new BlueprintError_1.BlueprintError("Cannot edit connection type");const t=this._blueprint.parameters.setParametersFrom({root:this.rootPath,parameterized:e});return this.edit(t)}is(...e){return e.includes(this.type)}getParameterizedConnection(){const e=(0,Object_1.deepClone)(this.connection),t=this._blueprint.parameters.getPathParameters(this.rootPath);for(const[r,n]of Object.entries(t)){if(!n)continue;const t=(0,from_1.from)(n.paths).pipe((0,map_1.map)(e=>(0,from_1.from)(new Parameters_1.ParameterPathBuilder(e).parts).pipe((0,skip_1.skip)(ParametersHelper_1.CONN_ROOT_PATH_PARTS_COUNT))),(0,map_1.map)(t=>(0,ParametersHelper_1.resolveParameterPath)(t,e)));for(const{parent:e,key:n}of t)e[n]={$param$:r,defaultValue:e[n]}}return e}setParameterizedConnection(e){const t=this._blueprint.parameters.setParametersFrom({root:this.rootPath,parameterized:e});return this._connection=t,e}assertType(...e){if(!this.is(...e))throw new BlueprintError_1.BlueprintError(`Connection is not of asserted type.\nConnection type is [${this.type}] while asserted type is [${e.join(", ")}]`)}validateConnection(){if(!this.isValid)throw new BlueprintError_1.BlueprintError(`Resource of type ${this.target.type} is not supported as a target for a connection with a source of type ${this.source.type}.`)}toJSON(){return this.connection}[(_ConnectionHelper_rootPath=new WeakMap,_ConnectionHelper_connUtils=new WeakMap,util_1.inspect.custom)](){return this.connection}}exports.ConnectionHelper=ConnectionHelper;