@compute.ts/core
Version:
Core package for libraries creation
1 lines • 5.12 kB
JavaScript
"use strict";var __values=this&&this.__values||function(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],i=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&i>=e.length?void 0:e)&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},__read=this&&this.__read||function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var i,n,a=r.call(e),o=[];try{for(;(void 0===t||0<t--)&&!(i=a.next()).done;)o.push(i.value)}catch(e){n={error:e}}finally{try{i&&!i.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return o},__spread=this&&this.__spread||function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(__read(arguments[t]));return e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ComputeApiNode=void 0;var rxjs_1=require("rxjs"),treeify=require("treeify"),ComputeError_1=require("./ComputeError"),ComputeApiNode=function(){function e(e,t,r){var i,n;this.children=e=void 0===e?[]:e,this.evalFn=t,this.value=r,this.valueChanges$=new rxjs_1.Subject,this.validityChanges$=new rxjs_1.Subject,this._isDirty=!0,this.parents=[];try{for(var a=__values(e),o=a.next();!o.done;o=a.next())o.value.addParents(this)}catch(e){i={error:e}}finally{try{o&&!o.done&&(n=a.return)&&n.call(a)}finally{if(i)throw i.error}}this.currentValue=r}return Object.defineProperty(e.prototype,"isDirty",{get:function(){return this._isDirty},set:function(e){this._isDirty=e,this.validityChanges$.next(!e)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"currentValue",{get:function(){return this._currentValue},set:function(e){this._currentValue=e,this.valueChanges$.next(e)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"evalFnName",{get:function(){var e="";return e+=this.isDirty?"☐":"☑",void 0!==this.evalFn&&(e+=" "+this.evalFn.name,!1!==this.isDirty&&void 0===this._error||(e+="(",e+=this.children.slice(0,10).map(function(e){return e._currentValue}).join(", "),10<this.children.length&&(e+=", and "+(this.children.length-10)+" others"),e+=")")),e},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"formatedMetadata",{get:function(){var e="";return void 0===this._alias&&void 0===this._description||(e+="["),void 0!==this._alias&&(e+=this._alias),void 0!==this._alias&&void 0!==this._description&&(e+=": "),void 0!==this._description&&(e+=this._description),void 0===this._alias&&void 0===this._description||(e+="]"),"\t "+e},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"formatedValue",{get:function(){return void 0!==this._error?"⚠ « "+this._error+" »":!0===this.isDirty?void 0===this.evalFn&&void 0===this._currentValue?"∅":void 0===this.evalFn&&void 0!==this._currentValue?JSON.stringify(this._currentValue):"":void 0===this._currentValue?"➔ ∅":void 0===this.evalFn?JSON.stringify(this._currentValue):"➔ "+JSON.stringify(this._currentValue)},enumerable:!1,configurable:!0}),e.prototype.affect=function(e){e!==this.currentValue&&(this.valueChanges$.next(e),this.currentValue=e,this.propagateDirty())},e.prototype.buildFreeKey=function(e,t){for(;void 0!==e[t];)t+=" ";return t},e.prototype.treeify=function(){var t,e,r={};try{for(var i=__values(this.children),n=i.next();!n.done;n=i.next()){var a=n.value,o=a.evalFnName;o+=" "+a.formatedValue,o+=" "+a.formatedMetadata,r[this.buildFreeKey(r,o)]=a.treeify()}}catch(e){t={error:e}}finally{try{n&&!n.done&&(e=i.return)&&e.call(i)}finally{if(t)throw t.error}}return r},e.prototype.addParents=function(e){this.parents.push(e)},e.prototype.propagateDirty=function(){var t,e;if(!0!==this.isDirty){this.validityChanges$.next(!1),this.isDirty=!0;try{for(var r=__values(this.parents),i=r.next();!i.done;i=r.next())i.value.propagateDirty()}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(t)throw t.error}}}},e.prototype.eval=function(){var t,e;if(void 0===this.evalFn)return this.isDirty=!1,this.validityChanges$.next(!0),this.currentValue;if(!1===this.isDirty)return this.currentValue;var r=[];try{for(var i=__values(this.children),n=i.next();!n.done;n=i.next()){var a=n.value.eval();r.push(a)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(e=i.return)&&e.call(i)}finally{if(t)throw t.error}}try{var o=this.evalFn.apply(this,__spread(r));return o!==this.currentValue&&(this.valueChanges$.next(o),this.currentValue=o),this.isDirty=!1,this.currentValue}catch(e){throw this._error=e.message,new ComputeError_1.ComputeError(e.message,this)}},Object.defineProperty(e.prototype,"valueChanges",{get:function(){return this.valueChanges$},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"validityChanges",{get:function(){return this.validityChanges$},enumerable:!1,configurable:!0}),e.prototype.alias=function(e){return this._alias=e,this},e.prototype.describe=function(e){return this._description=e,this},e.prototype.toString=function(){var e=this.evalFnName;e+=" "+this.formatedValue,e+=" "+this.formatedMetadata,e+="\n";var t=this.treeify();return e+=treeify.asTree(t,!0)},e}();exports.ComputeApiNode=ComputeApiNode;