extralife-donation-notifier
Version:
A node/browser library for giving realtime events when your extralife campaign receives a donation
3 lines (2 loc) • 6.34 kB
JavaScript
var ExtraLifeDonationWatcher=function(b){"use strict";function c(a,b,c,d){function e(a){return a instanceof c?a:new c(function(b){b(a)})}return new(c||(c=Promise))(function(c,f){function g(a){try{i(d.next(a))}catch(a){f(a)}}function h(a){try{i(d["throw"](a))}catch(a){f(a)}}function i(a){a.done?c(a.value):e(a.value).then(g,h)}i((d=d.apply(a,b||[])).next())})}var d=function c(a,b){return b={exports:{}},a(b,b.exports),b.exports}(function(a){function b(){}function c(a,b,c){this.fn=a,this.context=b,this.once=c||!1}function d(a,b,d,e,f){if("function"!=typeof d)throw new TypeError("The listener must be a function");var g=new c(d,e||a,f),i=h?h+b:b;return a._events[i]?a._events[i].fn?a._events[i]=[a._events[i],g]:a._events[i].push(g):(a._events[i]=g,a._eventsCount++),a}function e(a,c){0==--a._eventsCount?a._events=new b:delete a._events[c]}function f(){this._events=new b,this._eventsCount=0}var g=Object.prototype.hasOwnProperty,h="~";Object.create&&(b.prototype=Object.create(null),!new b().__proto__&&(h=!1)),f.prototype.eventNames=function a(){var b,c,d=[];if(0===this._eventsCount)return d;for(c in b=this._events)g.call(b,c)&&d.push(h?c.slice(1):c);return Object.getOwnPropertySymbols?d.concat(Object.getOwnPropertySymbols(b)):d},f.prototype.listeners=function b(a){var c=h?h+a:a,d=this._events[c];if(!d)return[];if(d.fn)return[d.fn];for(var e=0,f=d.length,g=Array(f);e<f;e++)g[e]=d[e].fn;return g},f.prototype.listenerCount=function b(a){var c=h?h+a:a,d=this._events[c];return d?d.fn?1:d.length:0},f.prototype.emit=function g(a,b,c,d,e,f){var k=h?h+a:a;if(!this._events[k])return!1;var l,m,n=this._events[k],o=arguments.length;if(n.fn){switch(n.once&&this.removeListener(a,n.fn,void 0,!0),o){case 1:return n.fn.call(n.context),!0;case 2:return n.fn.call(n.context,b),!0;case 3:return n.fn.call(n.context,b,c),!0;case 4:return n.fn.call(n.context,b,c,d),!0;case 5:return n.fn.call(n.context,b,c,d,e),!0;case 6:return n.fn.call(n.context,b,c,d,e,f),!0}for(m=1,l=Array(o-1);m<o;m++)l[m-1]=arguments[m];n.fn.apply(n.context,l)}else{var p,q=n.length;for(m=0;m<q;m++)switch(n[m].once&&this.removeListener(a,n[m].fn,void 0,!0),o){case 1:n[m].fn.call(n[m].context);break;case 2:n[m].fn.call(n[m].context,b);break;case 3:n[m].fn.call(n[m].context,b,c);break;case 4:n[m].fn.call(n[m].context,b,c,d);break;default:if(!l)for(p=1,l=Array(o-1);p<o;p++)l[p-1]=arguments[p];n[m].fn.apply(n[m].context,l)}}return!0},f.prototype.on=function e(a,b,c){return d(this,a,b,c,!1)},f.prototype.once=function e(a,b,c){return d(this,a,b,c,!0)},f.prototype.removeListener=function f(a,b,c,d){var g=h?h+a:a;if(!this._events[g])return this;if(!b)return e(this,g),this;var j=this._events[g];if(j.fn)j.fn!==b||d&&!j.once||c&&j.context!==c||e(this,g);else{for(var k=0,l=[],m=j.length;k<m;k++)(j[k].fn!==b||d&&!j[k].once||c&&j[k].context!==c)&&l.push(j[k]);l.length?this._events[g]=1===l.length?l[0]:l:e(this,g)}return this},f.prototype.removeAllListeners=function c(a){var d;return a?(d=h?h+a:a,this._events[d]&&e(this,d)):(this._events=new b,this._eventsCount=0),this},f.prototype.off=f.prototype.removeListener,f.prototype.addListener=f.prototype.on,f.prefixed=h,f.EventEmitter=f,a.exports=f}),f=b=>fetch(b).then(b=>b.json().then(a=>({headers:b.headers,status:b.status,json:a}))).then(({headers:b,status:a,json:c})=>{if(400<=a)return new Error(`Bad Request: ${a}`);const d=b.get("num-records");return null===d?c:{totalRecords:parseInt(d),records:c}}).catch(b=>b);const a="https://www.extra-life.org/api",g=(b,c)=>{const d=parseInt(b),e=parseInt(c);return`?limit=${b=d||100}&offset=${1===(c=e||1||1)?1:b*(c-1)}`};var e=Object.freeze({__proto__:null,getParticipant:async b=>await f(`${a}/participants/${b}`),getParticipantActivity:async b=>await f(`${a}/participants/${b}/activity`),getParticipantBadges:async b=>await f(`${a}/participants/${b}/badges`),getParticipantDonations:async(b,c,d)=>await f(`${a}/participants/${b}/donations${g(c,d)}`),getParticipantDonors:async(b,c,d)=>await f(`${a}/participants/${b}/donors${g(c,d)}`),getParticipants:async(b,c)=>await f(`${a}/participants${g(b,c)}`),getTeam:async b=>await f(`${a}/teams/${b}`),getTeamActivity:async b=>await f(`${a}/teams/${b}/activity`),getTeamBadges:async b=>await f(`${a}/teams/${b}/badges`),getTeamDonations:async(b,c,d)=>await f(`${a}/teams/${b}/donations${g(c,d)}`),getTeamDonors:async(b,c,d)=>await f(`${a}/teams/${b}/donors${g(c,d)}`),getTeamParticipants:async(b,c,d)=>await f(`${a}/teams/${b}/participants${g(c,d)}`),getTeams:async(b,c)=>await f(`${a}/teams${g(b,c)}`)}),h=function b(a){var c=Object.create(null);return a&&Object.keys(a).forEach(function(b){if("default"!==b){var e=Object.getOwnPropertyDescriptor(a,b);Object.defineProperty(c,b,e.get?e:{enumerable:!0,get:function(){return a[b]}})}}),c.default=a,Object.freeze(c)}(e);"function"==typeof SuppressedError?SuppressedError:function(a,b,c){var d=new Error(c);return d.name="SuppressedError",d.error=a,d.suppressed=b,d};var i=class a extends d{constructor(a){super(),this._participantId=a.participantId,this._teamId=a.teamId,this._lastCheck=Date.now(),this._stopped=!0}start(){this._stopped&&(this._stopped=!1,this._runCycle(),this.emit("started"))}stop(){this._stopped=!0,clearTimeout(this._donationCheckInterval)}_runCycle(){this._donationCheckInterval=setTimeout(()=>c(this,void 0,void 0,function*(){this._stopped||(this.emit("ping"),yield this._processDonations(),this._lastCheck=Date.now(),this._runCycle())}),15e3)}_processDonations(){return c(this,void 0,void 0,function*(){try{yield this._emitRecentTeamDonations(),yield this._emitRecentParticipantDonations()}catch(a){this.emit("error",a)}})}_emitRecentTeamDonations(){return c(this,void 0,void 0,function*(){if(this._teamId){const a=(yield h.getTeamDonations(this._teamId,10)).records;this._emitValidDonations(a,"team-donation")}})}_emitRecentParticipantDonations(){return c(this,void 0,void 0,function*(){if(this._participantId){const a=(yield h.getParticipantDonations(this._participantId,10)).records;this._emitValidDonations(a,"participant-donation")}})}_emitValidDonations(a,b){a.filter(a=>Date.parse(a.createdDateUTC)>this._lastCheck).reverse().forEach(a=>{this.emit(b,a)})}};return b.ExtraLifeDonationWatcher=i,b.default={ExtraLifeDonationWatcher:i},Object.defineProperty(b,"__esModule",{value:!0}),b}({});
//# sourceMappingURL=extra-life-donation-watcher.min.js.map