UNPKG

chain-of-responsibility

Version:

Extendable classes for Chain of Responsibility pattern.

2 lines 12.2 kB
(()=>{var e={828:e=>{(()=>{"use strict";var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},n={};function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}t.r(n),t.d(n,{AbstractChainItem:()=>a,AbstractChainItemContainer:()=>o,AbstractChainItemValueContainer:()=>p});const o=function(){function e(t){var n=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),r(this,"promises",[]),r(this,"valueContainer",null),r(this,"firstChainItem",null),r(this,"promise",null),this.valueContainer=t,this.firstChainItem=this.getFirstChainItem(),this._createItemsChain(this.firstChainItem),this.promise=new Promise((function(e,t){n.resolve=e}))}var t;return(t=[{key:"_createItemsChain",value:function(e){}},{key:"_getFirstChainItem",value:function(){return null}},{key:"getValueContainer",value:function(){return this.valueContainer}},{key:"getFirstChainItem",value:function(){return this.firstChainItem||(this.firstChainItem=this._getFirstChainItem()),this.firstChainItem}},{key:"addPromise",value:function(e){return this.promises.push(e),this}},{key:"run",value:function(){for(var e=this,t=this.getFirstChainItem();t&&!0===t.shouldSkip(this.getValueContainer());)t=t.getNextChainItem();return t&&!1===t.shouldStopBefore(this.getValueContainer())&&t.execute(),Promise.all(this.promises).then((function(){e.resolve(e.getResult())})),this.promise}},{key:"getResult",value:function(){return this.getValueContainer().getResult()}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(e.prototype,t),e}();function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}const a=function(){function e(t){var n=this,r=arguments.length>1&&void 0!==arguments[1]&&arguments[1];i(this,e),u(this,"chainContainer",null),u(this,"nextChainItem",null),u(this,"promise",null),u(this,"resolve",null),this.chainContainer=t,this.promise=null,this.resolve=null,!0===r&&(this.promise=new Promise((function(e,t){n.resolve=e})),this.getChainContainer().addPromise(this.promise))}var t;return(t=[{key:"hasPromise",value:function(){return!!this.promise}},{key:"getPromise",value:function(){return this.promise}},{key:"setNextChainItem",value:function(e){return this.nextChainItem=e,this}},{key:"getNextChainItem",value:function(){return this.nextChainItem}},{key:"getChainContainer",value:function(){return this.chainContainer}},{key:"getValueContainer",value:function(){return this.getChainContainer().getValueContainer()}},{key:"executeNext",value:function(){var e=this.getNextChainItem();if(e)for(;e&&!0===e.shouldSkip(this.getValueContainer());)e=e.getNextChainItem();return e&&!1===e.shouldStopBefore(this.getValueContainer())?e.execute():this.getValueContainer().getResult()}},{key:"execute",value:function(){var e=this;return this.hasPromise()&&this.getPromise().then((function(){!1===e.shouldStopAfter(e.getValueContainer())&&e.executeNext()})),this._execute(this.getValueContainer(),this.resolve),!0===this.shouldStopAfter(this.getValueContainer())?this.getValueContainer().getResult():this.hasPromise()?void 0:this.executeNext()}},{key:"_execute",value:function(e,t){return!1}},{key:"shouldStopBefore",value:function(e){return!1}},{key:"shouldStopAfter",value:function(e){return!1}},{key:"shouldSkip",value:function(e){return!1}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(e.prototype,t),e}(),l=function(e,t){for(var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,r=t.split("."),o=0;o<r.length;o+=1)if(void 0===(e=e[r[o]]))return n;return e},s=function(e,t,n){for(var r=t.split("."),o=e,i=0;i<r.length-1;i++){var u=r[i];u in o||(o[u]={}),o=o[u]}o[r[r.length-1]]=n};function c(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function f(e){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function h(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}const p=function(){function e(t,n){var r;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),h(this,"rawValue",null),h(this,"value",null),h(this,"parameters",{}),r=Array.isArray(t)?t.concat():"object"==f(t)?function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?c(Object(n),!0).forEach((function(t){h(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):c(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}({},t):t,this.rawValue=r,this.value=this._parseValue(t),this.parameters=n}var t;return(t=[{key:"_parseValue",value:function(e){return e}},{key:"getRawValue",value:function(){return this.rawValue}},{key:"getResult",value:function(){return this.getValue()}},{key:"getParameter",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return l(this.parameters,e,t)}},{key:"getValue",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(null===e)return this.value;if("string"==typeof e)return l(this.value,e,t);throw"Wrong usage."}},{key:"setValue",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return"object"==f(this.value)?(s(this.value,e,t),this):(this.value=e,this)}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(e.prototype,t),e}();e.exports=n})()}},t={};function n(r){var o=t[r];if(void 0!==o)return o.exports;var i=t[r]={exports:{}};return e[r](i,i.exports,n),i.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};(()=>{"use strict";n.r(r);var e=n(828);function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t,n){return(o="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var r=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=h(e)););return e}(e,t);if(r){var o=Object.getOwnPropertyDescriptor(r,t);return o.get?o.get.call(n):o.value}})(e,t,n||e)}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t,n){return t&&u(e.prototype,t),n&&u(e,n),e}function l(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&s(e,t)}function s(e,t){return(s=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,r=h(e);if(t){var o=h(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return f(this,n)}}function f(e,n){if(n&&("object"===t(n)||"function"==typeof n))return n;if(void 0!==n)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function h(e){return(h=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}var p=e.AbstractChainItem,y=e.AbstractChainItemValueContainer,v=e.AbstractChainItemContainer,g=function(e){l(n,e);var t=c(n);function n(){i(this,n);for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return t.call.apply(t,[this].concat(r))}return a(n,[{key:"_execute",value:function(e,t){e.addToCharge(e.getParameter("entry"))}}]),n}(p),m=function(e){l(n,e);var t=c(n);function n(){i(this,n);for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return t.call.apply(t,[this].concat(r))}return a(n,[{key:"_execute",value:function(e,t){e.subtractSeconds(3600),e.addToCharge(e.getParameter("first_hour")),setTimeout((function(){t(e.getParameter("first_hour"))}),2e3)}},{key:"shouldStopAfter",value:function(e){return e.getValue("seconds")<=0}}]),n}(p),b=function(e){l(n,e);var t=c(n);function n(){i(this,n);for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return t.call.apply(t,[this].concat(r))}return a(n,[{key:"_execute",value:function(e){var t=e.getValue("seconds");e.subtractSeconds(t),e.addToCharge(Math.ceil(t/60/60)*e.getParameter("after_first_hour"))}},{key:"shouldStopAfter",value:function(e){return e.getValue("seconds")<=0}}]),n}(p),d=function(e){l(n,e);var t=c(n);function n(){i(this,n);for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return t.call.apply(t,[this].concat(r))}return a(n,[{key:"_createItemsChain",value:function(e){var t=e,n=new m(this,!0),r=new b(this);t.setNextChainItem(n),n.setNextChainItem(r)}},{key:"_getFirstChainItem",value:function(){return new g(this)}},{key:"run",value:function(){return o(h(n.prototype),"run",this).call(this)}},{key:"getResult",value:function(){return o(h(n.prototype),"getResult",this).call(this)}}]),n}(v),C=function(e){l(n,e);var t=c(n);function n(){i(this,n);for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return t.call.apply(t,[this].concat(r))}return a(n,[{key:"_parseValue",value:function(e){return{charge:0,seconds:e}}},{key:"getResult",value:function(){return o(h(n.prototype),"getValue",this).call(this,"charge")}},{key:"addToCharge",value:function(e){var t=o(h(n.prototype),"getValue",this).call(this,"charge");return t+=e,o(h(n.prototype),"setValue",this).call(this,"charge",t),this}},{key:"subtractSeconds",value:function(e){var t=o(h(n.prototype),"getValue",this).call(this,"seconds");return t-=e,o(h(n.prototype),"setValue",this).call(this,"seconds",t),this}}]),n}(y);function w(e){var t=e.split(":");return{hour:parseInt(t[0]),minutes:parseInt(t[1])}}function O(e,t){var n=new Date,r=new Date,o=w(e),i=w(t);n.setHours(o.hour),n.setMinutes(o.minutes),r.setHours(i.hour),r.setMinutes(i.minutes);var u=r.getTime()-n.getTime(),a=new d(new C(u/1e3,{entry:2,first_hour:3,after_first_hour:4}));return a.run().then((function(e){console.log("promised",e)})),a.getResult()}var k=O("10:00","13:21"),P=O("09:42","11:42");console.log(k),console.log(P)})();var o=exports;for(var i in r)o[i]=r[i];r.__esModule&&Object.defineProperty(o,"__esModule",{value:!0})})(); //# sourceMappingURL=example.js.map