refactor: simplify resolving of promises
This commit is contained in:
parent
89e5bb1978
commit
8d003dd174
6 changed files with 13 additions and 42 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import * as cheerio from 'cheerio';
|
||||
import { Handler } from './types';
|
||||
import { ImdbId, fulfillAllPromises, parseImdbId, Fetcher } from '../utils';
|
||||
import { ImdbId, parseImdbId, Fetcher } from '../utils';
|
||||
import { EmbedExtractors } from '../embed-extractor';
|
||||
import { Context } from '../types';
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ export class KinoKiste implements Handler {
|
|||
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
return fulfillAllPromises(
|
||||
return Promise.all(
|
||||
$(`[data-num="${imdbId.series}x${imdbId.episode}"]`)
|
||||
.siblings('.mirrors')
|
||||
.children('[data-link]')
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import * as cheerio from 'cheerio';
|
||||
import { Handler } from './types';
|
||||
import { Fetcher, fulfillAllPromises, parseImdbId } from '../utils';
|
||||
import { Fetcher, parseImdbId } from '../utils';
|
||||
import { EmbedExtractors } from '../embed-extractor';
|
||||
import { Context } from '../types';
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ export class MeineCloud implements Handler {
|
|||
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
return fulfillAllPromises(
|
||||
return Promise.all(
|
||||
$('[data-link!=""]')
|
||||
.map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://')))
|
||||
.toArray()
|
||||
|
|
|
|||
14
src/index.ts
14
src/index.ts
|
|
@ -4,7 +4,7 @@ import { flag } from 'country-emoji';
|
|||
import { landingTemplate } from './landingTemplate';
|
||||
import { Handler, KinoKiste, MeineCloud } from './handler';
|
||||
import { Dropload, EmbedExtractors, SuperVideo } from './embed-extractor';
|
||||
import { buildManifest, Fetcher, fulfillAllPromises, logInfo } from './utils';
|
||||
import { buildManifest, Fetcher, logError, logInfo } from './utils';
|
||||
import { Config, UrlResult } from './types';
|
||||
import fs from 'node:fs';
|
||||
import * as os from 'node:os';
|
||||
|
|
@ -104,12 +104,16 @@ addon.get('/:config/stream/:type/:id.json', async function (req: Request, res: R
|
|||
return;
|
||||
}
|
||||
|
||||
const handlerUrlResults = await handler.handle({ ip: req.ip as string }, id);
|
||||
logInfo(`${handler.id} returned ${handlerUrlResults.length} urls`);
|
||||
try {
|
||||
const handlerUrlResults = await handler.handle({ ip: req.ip as string }, id);
|
||||
logInfo(`${handler.id} returned ${handlerUrlResults.length} urls`);
|
||||
|
||||
urlResults.push(...handlerUrlResults);
|
||||
urlResults.push(...handlerUrlResults);
|
||||
} catch (err) {
|
||||
logError(`${handler.id} error: ` + err);
|
||||
}
|
||||
});
|
||||
await fulfillAllPromises(handlerPromises);
|
||||
await Promise.all(handlerPromises);
|
||||
|
||||
urlResults.sort((a, b) => {
|
||||
const heightComparison = (b.height ?? 0) - (a.height ?? 0);
|
||||
|
|
|
|||
|
|
@ -3,4 +3,3 @@ export * from './embed';
|
|||
export * from './imdb';
|
||||
export * from './log';
|
||||
export * from './manifest';
|
||||
export * from './promise';
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
import { fulfillAllPromises } from './promise';
|
||||
|
||||
describe('promise', () => {
|
||||
test('fulfillAllPromises returns all resolved promise values and logs rejected ones', async () => {
|
||||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
|
||||
|
||||
const results = await fulfillAllPromises([
|
||||
Promise.resolve('ok'),
|
||||
Promise.reject('not ok'),
|
||||
]);
|
||||
|
||||
expect(results).toStrictEqual(['ok']);
|
||||
expect(consoleErrorSpy).toHaveBeenCalled();
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('Could not fulfill promise: not ok'));
|
||||
});
|
||||
});
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
import { logError } from './log';
|
||||
|
||||
export const fulfillAllPromises = async <T>(promises: Promise<T>[]) => {
|
||||
const resultValues: T[] = [];
|
||||
|
||||
(await Promise.allSettled(promises)).forEach((result) => {
|
||||
if (result.status === 'fulfilled') {
|
||||
resultValues.push(result.value);
|
||||
return;
|
||||
}
|
||||
|
||||
logError(`Could not fulfill promise: ${result.reason}`);
|
||||
});
|
||||
|
||||
return resultValues;
|
||||
};
|
||||
Loading…
Reference in a new issue