UNPKG

callyflower

Version:

A collection of lightweight higher order functions to add customizable event handling to a function execution flow.

3 lines (2 loc) 4.29 kB
var CallyFlower=function(t){"use strict";var n=function(){return n=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t},n.apply(this,arguments)};function e(t,n,e,r){return new(e||(e=Promise))((function(o,u){function a(t){try{l(r.next(t))}catch(t){u(t)}}function c(t){try{l(r.throw(t))}catch(t){u(t)}}function l(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(a,c)}l((r=r.apply(t,n||[])).next())}))}function r(t,n){var e,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=c(0),a.throw=c(1),a.return=c(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(l){return function(c){if(e)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(u=0)),u;)try{if(e=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=n.call(t,u)}catch(t){c=[6,t],r=0}finally{e=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,l])}}}"function"==typeof SuppressedError&&SuppressedError;var o=function(t){return"function"==typeof t},u=function(t){return o(t)&&"Function"===t.constructor.name},a=function(t){if(!o(t))throw new Error("Expected a function, but got ".concat(t))};function c(t,e){if(!e)return t;var r=e(t);return Promise.resolve(r).then((function(e){return n(n({},t),e||{})}))}var l=function(t,e){var r=e?e(t):null;return n(n({},t),r||{})},i=function(t,n){return e(void 0,void 0,void 0,(function(){return r(this,(function(e){return[2,c(t,n)]}))}))},s=function(t,n,o){return e(void 0,void 0,void 0,(function(){var e,a,c,s,f;return r(this,(function(r){switch(r.label){case 0:e=u(t),r.label=1;case 1:return r.trys.push([1,5,,9]),e?(a=t.apply(void 0,n),[3,4]):[3,2];case 2:return[4,t.apply(void 0,n)];case 3:a=r.sent(),r.label=4;case 4:return[2,a];case 5:return c=r.sent(),e?(f=l({callee:t,args:n,caught:c,event:"onCatch"},o),[3,8]):[3,6];case 6:return[4,i({callee:t,args:n,caught:c,event:"onCatch"},o)];case 7:f=r.sent(),r.label=8;case 8:if((s=f).result)return[2,s.result];throw s.caught;case 9:return[2]}}))}))};return t.withOnCall=function(t,n){return a(t),n?u(t)?function(){for(var e,r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];var u=l({callee:t,args:r,event:"onCall"},n);return null!==(e=u.result)&&void 0!==e?e:u.callee.apply(u,u.args)}:function(){for(var o=[],u=0;u<arguments.length;u++)o[u]=arguments[u];return e(void 0,void 0,void 0,(function(){var e,u;return r(this,(function(r){switch(r.label){case 0:return[4,i({callee:t,args:o,event:"onCall"},n)];case 1:return e=r.sent(),[2,null!==(u=e.result)&&void 0!==u?u:e.callee.apply(e,e.args)]}}))}))}:t},t.withOnCatch=function(t,n){return a(t),n?u(t)?function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return s(t,e,n)}:function(){for(var o=[],u=0;u<arguments.length;u++)o[u]=arguments[u];return e(void 0,void 0,void 0,(function(){return r(this,(function(e){return[2,s(t,o,n)]}))}))}:t},t.withOnResult=function(t,n){return a(t),n?u(t)?function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var o=t.apply(void 0,e);return(o?l({callee:t,args:e,result:o,event:"onResult"},n):{result:o}).result}:function(){for(var o=[],u=0;u<arguments.length;u++)o[u]=arguments[u];return e(void 0,void 0,void 0,(function(){var e,u;return r(this,(function(r){switch(r.label){case 0:return[4,t.apply(void 0,o)];case 1:return(e=r.sent())?[4,i({callee:t,args:o,result:e,event:"onResult"},n)]:[3,3];case 2:return u=r.sent(),[3,4];case 3:u={result:e},r.label=4;case 4:return[2,u.result]}}))}))}:t},t}({}); //# sourceMappingURL=callyflower.min.js.map