From 9e9e928c8050e2b17076d0ec5b9452ad49d00591 Mon Sep 17 00:00:00 2001 From: iPromKnight Date: Mon, 5 Feb 2024 05:02:41 +0000 Subject: [PATCH] promises helpers now ts --- src/node/consumer/src/lib/promises.js | 55 ------------------- src/node/consumer/src/lib/promises.ts | 52 ++++++++++++++++++ src/node/consumer/src/lib/torrentEntries.js | 2 +- src/node/consumer/src/lib/torrentFiles.js | 2 +- .../src/repository/database_repository.ts | 2 +- 5 files changed, 55 insertions(+), 58 deletions(-) delete mode 100644 src/node/consumer/src/lib/promises.js create mode 100644 src/node/consumer/src/lib/promises.ts diff --git a/src/node/consumer/src/lib/promises.js b/src/node/consumer/src/lib/promises.js deleted file mode 100644 index 33e3dae..0000000 --- a/src/node/consumer/src/lib/promises.js +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Execute promises in sequence one after another. - */ -export async function sequence(promises) { - return promises.reduce((promise, func) => - promise.then(result => func().then(Array.prototype.concat.bind(result))), Promise.resolve([])); -} - -/** - * Return first resolved promise as the result. - */ -export async function first(promises) { - return Promise.all(promises.map((p) => { - // If a request fails, count that as a resolution so it will keep - // waiting for other possible successes. If a request succeeds, - // treat it as a rejection so Promise.all immediately bails out. - return p.then( - (val) => Promise.reject(val), - (err) => Promise.resolve(err) - ); - })).then( - // If '.all' resolved, we've just got an array of errors. - (errors) => Promise.reject(errors), - // If '.all' rejected, we've got the result we wanted. - (val) => Promise.resolve(val) - ); -} - -/** - * Delay promise - */ -export async function delay(duration) { - return new Promise((resolve) => setTimeout(resolve, duration)); -} - -/** - * Timeout promise after a set time in ms - */ -export async function timeout(timeoutMs, promise, message = 'Timed out') { - return Promise.race([ - promise, - new Promise(function (resolve, reject) { - setTimeout(function () { - reject(message); - }, timeoutMs); - }) - ]); -} - -/** - * Return most common value from given array. - */ -export function mostCommonValue(array) { - return array.sort((a, b) => array.filter(v => v === a).length - array.filter(v => v === b).length).pop(); -} \ No newline at end of file diff --git a/src/node/consumer/src/lib/promises.ts b/src/node/consumer/src/lib/promises.ts new file mode 100644 index 0000000..f7a8dd3 --- /dev/null +++ b/src/node/consumer/src/lib/promises.ts @@ -0,0 +1,52 @@ +/** + * Execute promises in sequence one after another. + */ +export async function sequence(promises: Array<() => Promise>): Promise { + return promises.reduce((promise: Promise, func: () => Promise) => + promise.then(result => func().then(res => result.concat(res))), Promise.resolve([])); +} + +/** + * Return first resolved promise as the result. + */ +export async function first(promises: Array>): Promise { + return Promise.all(promises.map(p => { + // If a request fails, count that as a resolution so it will keep + // waiting for other possible successes. If a request succeeds, + // treat it as a rejection so Promise.all immediately bails out. + return p.then((val) => Promise.reject(val), (err) => Promise.resolve(err)); + })).then( + // If '.all' resolved, we've just got an array of errors. + (errors) => Promise.reject(errors), + // If '.all' rejected, we've got the result we wanted. + (val) => Promise.resolve(val) + ); +} + +/** + * Delay promise + */ +export async function delay(duration: number): Promise { + return new Promise((resolve) => setTimeout(() => resolve(), duration)); +} + +/** + * Timeout promise after a set time in ms + */ +export async function timeout(timeoutMs: number, promise: Promise, message = 'Timed out'): Promise { + return Promise.race([ + promise, + new Promise(function (resolve, reject) { + setTimeout(function () { + reject(message); + }, timeoutMs); + }) + ]); +} + +/** + * Return most common value from given array. + */ +export function mostCommonValue(array: any[]): any { + return array.sort((a, b) => array.filter(v => v === a).length - array.filter(v => v === b).length).pop(); +} \ No newline at end of file diff --git a/src/node/consumer/src/lib/torrentEntries.js b/src/node/consumer/src/lib/torrentEntries.js index 18d4940..3288126 100644 --- a/src/node/consumer/src/lib/torrentEntries.js +++ b/src/node/consumer/src/lib/torrentEntries.js @@ -1,7 +1,7 @@ import { parse } from 'parse-torrent-title'; import { getImdbId, getKitsuId } from './metadata'; import { isPackTorrent } from './parseHelper.js'; -import * as Promises from './promises.js'; +import * as Promises from './promises'; import { repository } from '../repository/database_repository'; import { parseTorrentFiles } from './torrentFiles.js'; import { assignSubtitles } from './torrentSubtitles.js'; diff --git a/src/node/consumer/src/lib/torrentFiles.js b/src/node/consumer/src/lib/torrentFiles.js index 31e8d98..abef29c 100644 --- a/src/node/consumer/src/lib/torrentFiles.js +++ b/src/node/consumer/src/lib/torrentFiles.js @@ -6,7 +6,7 @@ import { metadataConfig } from './config.js'; import { isDisk } from './extension.js'; import { getMetadata, getImdbId, getKitsuId } from './metadata'; import { parseSeriesVideos, isPackTorrent } from './parseHelper.js'; -import * as Promises from './promises.js'; +import * as Promises from './promises'; import {torrentFiles} from "./torrent.js"; import { TorrentType } from './enums/torrent_types'; import {logger} from "./logger"; diff --git a/src/node/consumer/src/repository/database_repository.ts b/src/node/consumer/src/repository/database_repository.ts index 7aabd04..023aa4c 100644 --- a/src/node/consumer/src/repository/database_repository.ts +++ b/src/node/consumer/src/repository/database_repository.ts @@ -2,7 +2,7 @@ import moment from 'moment'; import {literal, Op, WhereOptions} from "sequelize"; import {Model, Sequelize} from 'sequelize-typescript'; import {databaseConfig} from '../lib/config'; -import * as Promises from '../lib/promises.js'; +import * as Promises from '../lib/promises'; import {Provider} from "./models/provider"; import {File} from "./models/file"; import {Torrent} from "./models/torrent";