mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
export named queue creation
This commit is contained in:
11
addon/lib/namedQueue.js
Normal file
11
addon/lib/namedQueue.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import namedQueue from "named-queue";
|
||||
|
||||
export function createNamedQueue(concurrency) {
|
||||
const queue = new namedQueue((task, callback) => task.method()
|
||||
.then(result => callback(false, result))
|
||||
.catch((error => callback(error))), 200);
|
||||
queue.wrap = (id, method) => new Promise(((resolve, reject) => {
|
||||
queue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||
}));
|
||||
return queue;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import StaticResponse, { isStaticUrl } from './static.js';
|
||||
import { cacheWrapResolvedUrl } from '../lib/cache.js';
|
||||
import { timeout } from '../lib/promises.js';
|
||||
import { BadTokenError, streamFilename, AccessDeniedError, enrichMeta } from './mochHelper.js';
|
||||
import { createNamedQueue } from "../lib/namedQueue.js";
|
||||
|
||||
const RESOLVE_TIMEOUT = 2 * 60 * 1000; // 2 minutes
|
||||
const MIN_API_KEY_SYMBOLS = 15;
|
||||
@@ -62,9 +63,7 @@ export const MochOptions = {
|
||||
const unrestrictQueues = {}
|
||||
Object.values(MochOptions)
|
||||
.map(moch => moch.key)
|
||||
.forEach(mochKey => unrestrictQueues[mochKey] = new namedQueue((task, callback) => task.method()
|
||||
.then(result => callback(false, result))
|
||||
.catch((error => callback(error))), 200));
|
||||
.forEach(mochKey => unrestrictQueues[mochKey] = createNamedQueue(50));
|
||||
|
||||
export function hasMochConfigured(config) {
|
||||
return Object.keys(MochOptions).find(moch => config?.[moch])
|
||||
@@ -110,10 +109,7 @@ export async function resolve(parameters) {
|
||||
return StaticResponse.FAILED_UNEXPECTED;
|
||||
})
|
||||
.then(url => isStaticUrl(url) ? `${parameters.host}/${url}` : url);
|
||||
const unrestrictQueue = unrestrictQueues[moch.key];
|
||||
return new Promise(((resolve, reject) => {
|
||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||
}));
|
||||
return unrestrictQueues[moch.key].wrap(id, method);
|
||||
}
|
||||
|
||||
export async function getMochCatalog(mochKey, config) {
|
||||
|
||||
Reference in New Issue
Block a user