您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Parallel processing module for JavaScript that can specify the number of concurrent executions.
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/398566/785036/concurrent_promise.js
- ( ( exports ) => {
- 'use strict';
- const
- version = '1.0.1',
- execute = async ( promise_functions, max_concurrent_worker = 10 ) => {
- let concurrent_workers = Array( max_concurrent_worker ).fill( null ),
- waiting_worker_queue = [],
- success_list = [],
- failure_list = [],
- log_debug = ( header ) => {
- if ( ! exports.debug_mode ) {
- return;
- }
- if ( header ) {
- console.log( header );
- }
- console.log( '- concurrent_workers:', concurrent_workers );
- console.log( '- waiting_worker_queue:', waiting_worker_queue );
- console.log( '- success_list:', success_list );
- console.log( '- failure_list:', failure_list );
- },
- attempt_work_delegation = () => {
- log_debug( '*** attempt_work_delegation()' );
- if ( waiting_worker_queue.length <= 0 ) {
- return;
- }
- let target_worker_id = -1;
- for ( let worker_id = 0; worker_id < concurrent_workers.length; worker_id ++ ) {
- if ( ! concurrent_workers[ worker_id ] ) {
- target_worker_id = worker_id;
- break;
- }
- }
- if ( target_worker_id < 0 ) {
- return;
- }
- let target_worker = concurrent_workers[ target_worker_id ] = waiting_worker_queue.shift(),
- target_promise = target_worker.promise = target_worker.promise_function(),
- finish = ( is_success, result ) => {
- target_worker.is_success = is_success;
- target_worker.result = result;
- ( is_success ? success_list : failure_list ).push( target_worker );
- target_worker.resolve( result );
- concurrent_workers[ target_worker_id ] = null;
- attempt_work_delegation();
- };
- target_worker.worker_id = target_worker_id;
- target_promise
- .then( ( result ) => {
- finish( true, result );
- } )
- .catch( ( result ) => {
- finish( false, result );
- } );
- },
- add_worker = ( worker ) => {
- waiting_worker_queue.push( worker );
- attempt_work_delegation();
- },
- wrap_promise_function = ( promise_function, call_index ) => {
- return new Promise( ( resolve, reject ) => {
- add_worker( {
- call_index : call_index,
- promise_function : promise_function,
- resolve : resolve,
- reject : reject,
- } );
- } );
- },
- promise_results = await Promise.all( promise_functions.map( ( promise_function, call_index ) => wrap_promise_function( promise_function, call_index ) ) );
- success_list.sort( ( a, b ) => a.call_index - b.call_index );
- failure_list.sort( ( a, b ) => a.call_index - b.call_index );
- log_debug( '*** concurrent_executor() : end' );
- let result_info = {
- promise_results : promise_results,
- success_list : success_list,
- failure_list : failure_list,
- };
- if ( exports.debug_mode ) {
- Object.assign( result_info, {
- waiting_worker_queue : waiting_worker_queue,
- concurrent_workers : concurrent_workers,
- } );
- }
- return result_info;
- };
- Object.assign( exports, {
- version : version,
- execute : execute,
- debug_mode : false,
- } );
- } )( ( typeof exports != 'undefined' ) ? exports : ( ( typeof window != 'undefined' ) ? window : this )[ 'concurrent_promise' ] = {} );
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址