UNPKG

@splode/obake

Version:

Check merchants for deals.

174 lines 9.78 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var events_1 = __importDefault(require("events")); var banner_1 = __importDefault(require("./banner")); var MerchantFactory_1 = __importDefault(require("./merchant/MerchantFactory")); var Obake = /** @class */ (function () { function Obake(store) { this.merchants = []; this.store = null; this.interval = null; this.store = store; } Obake.prototype.init = function () { var _a; return __awaiter(this, void 0, void 0, function () { var gl; return __generator(this, function (_b) { if (!((_a = this.store) === null || _a === void 0 ? void 0 : _a.config)) { throw new Error("failed to find config from store"); } this.merchants = MerchantFactory_1.default.create(this.store.config.goods); gl = this.merchants.length; // needed for disabling node warnings due to async browser launching if (gl > events_1.default.defaultMaxListeners) { events_1.default.defaultMaxListeners = gl; } return [2 /*return*/]; }); }); }; Obake.prototype.run = function () { var _a, _b, _c, _d, _e; return __awaiter(this, void 0, void 0, function () { var int_1; var _this = this; return __generator(this, function (_f) { switch (_f.label) { case 0: if ((_b = (_a = this.store) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.verbose) { console.log(banner_1.default); (_c = this.logger) === null || _c === void 0 ? void 0 : _c.info("starting Obake..."); } return [4 /*yield*/, this.check()]; case 1: _f.sent(); if (!((_e = (_d = this.store) === null || _d === void 0 ? void 0 : _d.config) === null || _e === void 0 ? void 0 : _e.interval)) return [3 /*break*/, 3]; int_1 = this.store.config.interval * 60 * 1e3; return [4 /*yield*/, new Promise(function () { _this.interval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!!((_b = (_a = this.store) === null || _a === void 0 ? void 0 : _a.state) === null || _b === void 0 ? void 0 : _b.isRunning)) return [3 /*break*/, 2]; return [4 /*yield*/, this.check()]; case 1: _c.sent(); _c.label = 2; case 2: return [2 /*return*/]; } }); }); }, int_1); })]; case 2: return [2 /*return*/, _f.sent()]; case 3: return [2 /*return*/]; } }); }); }; Obake.prototype.teardown = function () { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_d) { switch (_d.label) { case 0: if ((_b = (_a = this.store) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.verbose) { (_c = this.logger) === null || _c === void 0 ? void 0 : _c.info("cleaning up and shutting down Obake..."); } if (this.interval) { clearInterval(this.interval); } return [4 /*yield*/, Promise.all(this.merchants.map(function (merchant) { return merchant.close(); })).catch(function (err) { throw err; })]; case 1: _d.sent(); return [2 /*return*/]; } }); }); }; Obake.prototype.check = function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; return __awaiter(this, void 0, void 0, function () { var verbose, gl; var _this = this; return __generator(this, function (_l) { switch (_l.label) { case 0: verbose = (_b = (_a = this.store) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.verbose; gl = ((_d = (_c = this.store) === null || _c === void 0 ? void 0 : _c.config) === null || _d === void 0 ? void 0 : _d.goods.length) || 0; if (gl < 1) { return [2 /*return*/]; } (_f = (_e = this.store) === null || _e === void 0 ? void 0 : _e.state) === null || _f === void 0 ? void 0 : _f.setIsRunning(true); if (verbose) { (_g = this.logger) === null || _g === void 0 ? void 0 : _g.info("checking for " + gl + " good" + (gl > 1 ? "s" : "") + "..."); } return [4 /*yield*/, Promise.all(this.merchants.map(function (merchant) { return merchant.checkGoods(); })).catch(function (err) { var _a; (_a = _this.logger) === null || _a === void 0 ? void 0 : _a.error(err.message); })]; case 1: _l.sent(); if (verbose) { (_h = this.logger) === null || _h === void 0 ? void 0 : _h.info("Obake finished checking"); } (_k = (_j = this.store) === null || _j === void 0 ? void 0 : _j.state) === null || _k === void 0 ? void 0 : _k.setIsRunning(false); return [2 /*return*/]; } }); }); }; Object.defineProperty(Obake.prototype, "logger", { get: function () { var _a; return ((_a = this.store) === null || _a === void 0 ? void 0 : _a.logger) || null; }, enumerable: false, configurable: true }); return Obake; }()); exports.default = Obake; //# sourceMappingURL=Obake.js.map