UNPKG

ayakashi

Version:

The next generation web scraping framework

116 lines (115 loc) 4.79 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.attachYields = void 0; function attachYields(ayakashiInstance, pipeprocClient, saveTopic, selfTopic, yieldWatcher) { ayakashiInstance.yield = function (extracted) { return __awaiter(this, void 0, void 0, function* () { if (extracted instanceof Promise) { const actualExtracted = yield extracted; if (!actualExtracted) return; yield pipeprocClient.commit({ topic: saveTopic, body: { value: actualExtracted } }); yieldWatcher.yieldedAtLeastOnce = true; } else { if (!extracted) return; yield pipeprocClient.commit({ topic: saveTopic, body: { value: extracted } }); yieldWatcher.yieldedAtLeastOnce = true; } }); }; ayakashiInstance.yieldEach = function (extracted) { return __awaiter(this, void 0, void 0, function* () { if (extracted instanceof Promise) { const actualExtracted = yield extracted; if (!Array.isArray(actualExtracted) || actualExtracted.length === 0) { throw new Error("<yieldEach> requires an array"); } yield pipeprocClient.commit(actualExtracted.map(ex => { return { topic: saveTopic, body: { value: ex } }; })); yieldWatcher.yieldedAtLeastOnce = true; } else { if (!Array.isArray(extracted) || extracted.length === 0) { throw new Error("<yieldEach> requires an array"); } yield pipeprocClient.commit(extracted.map(ex => { return { topic: saveTopic, body: { value: ex } }; })); yieldWatcher.yieldedAtLeastOnce = true; } }); }; ayakashiInstance.recursiveYield = function (extracted) { return __awaiter(this, void 0, void 0, function* () { if (extracted instanceof Promise) { const actualExtracted = yield extracted; if (!actualExtracted) return; yield pipeprocClient.commit({ topic: selfTopic, body: { value: actualExtracted } }); } else { if (!extracted) return; yield pipeprocClient.commit({ topic: selfTopic, body: { value: extracted } }); } }); }; ayakashiInstance.recursiveYieldEach = function (extracted) { return __awaiter(this, void 0, void 0, function* () { if (extracted instanceof Promise) { const actualExtracted = yield extracted; if (!Array.isArray(actualExtracted) || actualExtracted.length === 0) { throw new Error("<recursiveYieldEach> requires an array"); } yield pipeprocClient.commit(actualExtracted.map(ex => { return { topic: selfTopic, body: { value: ex } }; })); } else { if (!Array.isArray(extracted) || extracted.length === 0) { throw new Error("<recursiveYieldEach> requires an array"); } yield pipeprocClient.commit(extracted.map(ex => { return { topic: selfTopic, body: { value: ex } }; })); } }); }; } exports.attachYields = attachYields;