@d3vtool/hooks
Version:
Collection of custom React hooks to simplify tasks in your React projects.
2 lines (1 loc) • 3.64 kB
JavaScript
"use strict";var h=function(){return h=Object.assign||function(s){for(var r,t=1,i=arguments.length;t<i;t++){r=arguments[t];for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(s[o]=r[o])}return s},h.apply(this,arguments)},y=function(s,r,t,i){function o(e){return e instanceof t?e:new t(function(a){a(e)})}return new(t||(t=Promise))(function(e,a){function c(u){try{n(i.next(u))}catch(l){a(l)}}function f(u){try{n(i.throw(u))}catch(l){a(l)}}function n(u){u.done?e(u.value):o(u.value).then(c,f)}n((i=i.apply(s,r||[])).next())})},g=function(s,r){var t={label:0,sent:function(){if(e[0]&1)throw e[1];return e[1]},trys:[],ops:[]},i,o,e,a=Object.create((typeof Iterator=="function"?Iterator:Object).prototype);return a.next=c(0),a.throw=c(1),a.return=c(2),typeof Symbol=="function"&&(a[Symbol.iterator]=function(){return this}),a;function c(n){return function(u){return f([n,u])}}function f(n){if(i)throw new TypeError("Generator is already executing.");for(;a&&(a=0,n[0]&&(t=0)),t;)try{if(i=1,o&&(e=n[0]&2?o.return:n[0]?o.throw||((e=o.return)&&e.call(o),0):o.next)&&!(e=e.call(o,n[1])).done)return e;switch(o=0,e&&(n=[n[0]&2,e.value]),n[0]){case 0:case 1:e=n;break;case 4:return t.label++,{value:n[1],done:!1};case 5:t.label++,o=n[1],n=[0];continue;case 7:n=t.ops.pop(),t.trys.pop();continue;default:if(e=t.trys,!(e=e.length>0&&e[e.length-1])&&(n[0]===6||n[0]===2)){t=0;continue}if(n[0]===3&&(!e||n[1]>e[0]&&n[1]<e[3])){t.label=n[1];break}if(n[0]===6&&t.label<e[1]){t.label=e[1],e=n;break}if(e&&t.label<e[2]){t.label=e[2],t.ops.push(n);break}e[2]&&t.ops.pop(),t.trys.pop();continue}n=r.call(s,t)}catch(u){n=[6,u],o=0}finally{i=e=0}if(n[0]&5)throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}},p=function(){function s(r,t){this.onChangeListeners=new Set,this.listeners=new Set,this.promiseState={error:null,pending:!1,currentState:r},this.promiseAction=t}return s.prototype.reAction=function(){return y(this,void 0,void 0,function(){var r,t;return g(this,function(i){switch(i.label){case 0:this.setPendingState(!0),i.label=1;case 1:return i.trys.push([1,3,4,5]),[4,this.promiseAction(this.getCurrentState())];case 2:return r=i.sent(),this.setCurrentState(r),[2,r];case 3:return t=i.sent(),this.setErrorState(t),[3,5];case 4:return this.setPendingState(!1),[7];case 5:return[2]}})})},s.prototype.attachListener=function(r){this.listeners.add(r)},s.prototype.detachListener=function(r){this.listeners.delete(r)},s.prototype.notifyListeners=function(r){this.listeners.forEach(function(t){return t(r)}),this.onChangeListeners.forEach(function(t){return t()})},s.prototype.getCurrentState=function(){return this.promiseState.currentState},s.prototype.setCurrentState=function(r){this.promiseState.currentState=r;var t=h({},this.promiseState);this.listeners.forEach(function(i){return i(t)}),this.onChangeListeners.forEach(function(i){return i()})},s.prototype.setPendingState=function(r){this.promiseState.pending=r;var t=h({},this.promiseState);this.listeners.forEach(function(i){return i(t)}),this.onChangeListeners.forEach(function(i){return i()})},s.prototype.getPendingState=function(){return this.promiseState.pending},s.prototype.getErrorState=function(){return this.promiseState.error},s.prototype.setErrorState=function(r){this.promiseState.error=r;var t=h({},this.promiseState);this.listeners.forEach(function(i){return i(t)}),this.onChangeListeners.forEach(function(i){return i()})},s.prototype.onChange=function(r){this.onChangeListeners.add(r)},s.prototype.removeOnChange=function(r){this.onChangeListeners.delete(r)},s}();export{p as PromiseHub};export function createPromiseHub(s,r){return new p(s instanceof Function?s():s,r)}