Parallel

Asynchronous array handle

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/380535/680853/Parallel.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Parallel
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Asynchronous array handle
// @author       You
// ==/UserScript==

class Parallel{
    constructor(count=3){
        this.count=3
    }

    partial(data,count){
        let r=[];
        let l=data.length;
        let dataPerThread=parseInt(l/count);
        if (dataPerThread==0){
            dataPerThread=1;
        }
        for (let i=0;i<l;i+=dataPerThread){
            let to=Math.min(i+dataPerThread,l);
            let d=data.slice(i,to);
            r.push(d);
        }
        return r;
    }

    async run(data,action,isReturn=false){
        let results=[];
        for (let x in data){
			let r=action(data[x]);
			if (r instanceof Promise){
				r=await r;
			}
            if (isReturn){
                results.push(r);
            }
        }
        if (isReturn){
            return results;
        }
    }

    async forEach(data,action){
        data=this.partial(data,this.count);
        let threads=[];
        for (let i in data){
            let d=data[i];
            threads.unshift(this.run(d,action));
        }
        for (let x in threads){
            await threads[x];
        }
    }

    async map(data,action){
        data=this.partial(data,this.count);
        let threads=[];
        for (let i in data){
            let d=data[i];
            threads.unshift(this.run(d,action,true));
        }
        let results=[];
        for (let x in threads){
            results=results.concat(await threads[x]);
        }
        return results;
    }
}