PHP Classes

File: src/templates/template_multi.php

Recommend this page to a friend!
  Classes of Jorge Castro   MultiOne   src/templates/template_multi.php   Download  
File: src/templates/template_multi.php
Role: Auxiliary script
Content type: text/plain
Description: Configuration script
Class: MultiOne
Execute many tasks in parallel using JavaScript
Author: By
Last change:
Date: 3 days ago
Size: 4,440 bytes
 

Contents

Class file image Download
<?php http_response_code(404); die(1); // It is a template file, not a code to execute directly. This line is used to avoid to execute or read it. ?>
HTMLPREFIX
<script>
const obj_PREFIX_={
    interValId:[],
    divWorker:[],
    divWorkerError:[],
    divWorkerEnd:{},
    numWorkers:NUMWORKERS,
    onError:'ONERROR',
    maxTry:MAXTRY,
    coordinateMatrix:{workerInUse:[],bodyBase:BODYINIT,workerActive:0,numTry:[]},
    afterFetchWorker:AFTERFETCHWORKER,
    afterFetchWorkerError:AFTERFETCHERROR,
    start:function () {
        for(let nw=0;nw<this.numWorkers;nw++){
            this.coordinateMatrix.workerInUse.push('waiting');
            this.coordinateMatrix.numTry.push(0);
        }
        this.coordinateMatrix.workerActive=this.numWorkers;
        for(let nw=0;nw<this.numWorkers;nw++){
            this.divWorkerEnd = document.getElementById("PREFIX_worker_end");
            this.divWorker[nw] = document.getElementById("PREFIX_worker_"+nw);
            this.divWorkerError[nw] = document.getElementById("PREFIX_worker_error_"+nw);
            this.interValId[nw] = window.setInterval(()=>{
                if(this.coordinateMatrix.workerInUse[nw]==='waiting'){
                    this.coordinateMatrix.workerInUse[nw]='running';
                    HTMLLOADING
                    this.fetchWorker(nw);
                }
            }, TIMEPREFIX)
        }
    },
    fetchWorker: function(nw) {
        let bodyJson=JSON.stringify(this.coordinateMatrix.bodyBase[nw]);
        fetch("CURRENTWEB?type=PREFIX_load&worker="+nw,{'method':'post', body: bodyJson,headers:{SES:"SESSIONID"}})
            .then((response) => {
            if(response.ok) {
                return response.json();
            }
            return Promise.reject(response);
        })
            .then((json) => {
            this.coordinateMatrix.bodyBase[nw]=json.result;
            if(json.error) {
                this.divWorkerError[nw].innerHTML=json.error;
                this.divWorkerError[nw].style.visibility='visible';
                this.whenError(nw);
            } else {
                this.divWorkerError[nw].style.visibility='hidden';
                this.divWorkerError[nw].innerHTML='';
            }
            json=this.afterFetchWorker(nw,json);
            this.divWorker[nw].innerHTML =json.ui;

            if(json.type==='end'){ // end worker.
                this.endWorker(nw);
            }
            this.coordinateMatrix.numTry[nw]=0;
            this.coordinateMatrix.workerInUse[nw]='waiting';
        })
            .catch((response) => {
            response=this.afterFetchWorkerError(nw,response);
            this.divWorkerError[nw].style.visibility='visible';
            this.divWorkerError[nw].innerHTML ="Error: "+response.statusText+" ("+response.status+")";
            this.whenError(nw);
        });

    },
    whenError: function(nw) {
        switch(this.onError) {
            case 'try':
                this.coordinateMatrix.numTry[nw]++;
                if(this.coordinateMatrix.numTry[nw]>this.maxTry) {
                    this.endWorker(nw);
                } else {
                    this.coordinateMatrix.workerInUse[nw]='waiting';
                }
                break;
            case 'end':
                this.endAllWorkers(nw);
                break;
            case 'stop':
                this.endWorker(nw);
                break;
        }
    },
    endWorker:function(nw) {
        window.clearInterval(this.interValId[nw]);
        this.coordinateMatrix.workerActive--;
        if(this.coordinateMatrix.workerActive<=0) { // end all workers
            this.endAllWorkers();
        }
    },
    endAllWorkers:function() {
        for(let nw=0;nw<this.numWorkers;nw++){
            window.clearInterval(this.interValId[nw]);

        }
        let bodyJson=JSON.stringify(this.coordinateMatrix.bodyBase);
           fetch("CURRENTWEB?type=PREFIX_end",{'method':'post', body: bodyJson,headers:{SES:"SESSIONID"}})
           .then((response) => {
            if(response.ok) {
                return response.text();
            }
            return Promise.reject(response);
        })
           .then((text) => {
            this.divWorkerEnd.innerHTML=text;
        })
           .catch((response) => {
            this.divWorkerEnd.innerHTML ="Error: "+response.statusText+" ("+response.status+")";

        });
    }

}
obj_PREFIX_.start();


</script>