UNPKG

extralife-donation-notifier

Version:

A node/browser library for giving realtime events when your extralife campaign receives a donation

138 lines (126 loc) 4.99 kB
'use strict'; var EventEmitter = require('eventemitter3'); var ExtraLife = require('extra-life'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var ExtraLife__namespace = /*#__PURE__*/_interopNamespaceDefault(ExtraLife); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; class ExtraLifeDonationWatcher extends EventEmitter { constructor(options) { super(); this._participantId = options.participantId; this._teamId = options.teamId; this._lastCheck = Date.now(); this._stopped = true; } /** * Begins listening on the configured participant and team IDs */ start() { if (!this._stopped) { return; } this._stopped = false; this._runCycle(); this.emit('started'); } /** * Stops the object from listening for more donations */ stop() { this._stopped = true; clearTimeout(this._donationCheckInterval); } /** * Private methods */ _runCycle() { this._donationCheckInterval = setTimeout(() => __awaiter(this, void 0, void 0, function* () { if (this._stopped) { return; } this.emit('ping'); yield this._processDonations(); this._lastCheck = Date.now(); this._runCycle(); }), 15000); // extralife recommends 15 second poll rate } _processDonations() { return __awaiter(this, void 0, void 0, function* () { try { yield this._emitRecentTeamDonations(); yield this._emitRecentParticipantDonations(); } catch (e) { this.emit('error', e); } }); } _emitRecentTeamDonations() { return __awaiter(this, void 0, void 0, function* () { if (!this._teamId) { return; } const donations = (yield ExtraLife__namespace.getTeamDonations(this._teamId, 10)).records; this._emitValidDonations(donations, 'team-donation'); }); } _emitRecentParticipantDonations() { return __awaiter(this, void 0, void 0, function* () { if (!this._participantId) { return; } const donations = (yield ExtraLife__namespace.getParticipantDonations(this._participantId, 10)).records; this._emitValidDonations(donations, 'participant-donation'); }); } _emitValidDonations(donations, type) { donations .filter((donation) => Date.parse(donation.createdDateUTC) > this._lastCheck) .reverse() .forEach((donation) => { this.emit(type, donation); }); } } exports.ExtraLifeDonationWatcher = ExtraLifeDonationWatcher; //# sourceMappingURL=index.js.map