EasyUI Forum
April 20, 2024, 03:46:03 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: messager.progress with Chrome  (Read 8051 times)
cbutaru
Newbie
*
Posts: 4


View Profile
« on: June 29, 2017, 11:18:31 AM »

Hi,
I have a problem with messager.progress
I am changing the progress bar value
after each page loading, doing this in
sync and async manner
Works well in Firefox sync and async
but fail in Chrome sync mode.

Please, help !
Thank you.

Code:
	<div id="p" class="easyui-panel" title="Messages" style="width:700px;height:200px;padding:10px;">
</div>

<div style="margin:20px 0;">
<a href="#" class="easyui-linkbutton" onclick="processAsync()">Async flow</a>
<a href="#" class="easyui-linkbutton" onclick="processSync()">Sync flow</a>
</div>
<script type="text/javascript">

function showWaitMsg(message) {
var win = $.messager.progress({
title:'Please wait ...',
msg: message
});
}   

function closeWaitMsg() {
var bar = $.messager.progress('bar');
$.messager.progress('close');
}   

function changeProgressBar(index, max) {
var bar = $.messager.progress('bar');
var prval =  Math.floor((10000 * index) / max)/100;
bar.progressbar('setValue', prval);
}

function loadPage(aurl, fCallback) {
var xhr = new XMLHttpRequest();
if(typeof fCallback === 'function') {
xhr.callback = fCallback;
function xhrSuccess () {
this.callback.apply(this, this.arguments);
}
xhr.onload = xhrSuccess;
}
xhr.open('get', aurl, false);
xhr.send();
}

function processSync() {
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
setTimeout(function() {
for(var i=0; i<100; i++) {
changeProgressBar(i,100);
loadPage('/');
$('#p').append('Page: '+i.toString()+'<br>');
}
closeWaitMsg();
$('#p').append('Stop page processing <br>');
},1);
}

function processPage(i) {
setTimeout(function() {
changeProgressBar(i,100);
loadPage('/',
function() {     
$('#p').append('Page: '+i.toString()+'<br>');
if(i<100) {
processPage(i+1); // recursion
} else {
closeWaitMsg();
$('#p').append('Stop page processing <br>');       
}
}
);
},1);
}

function processAsync() { 
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
processPage(0);
}
</script>
« Last Edit: June 29, 2017, 11:21:09 AM by cbutaru » Logged
jarry
Administrator
Hero Member
*****
Posts: 2262


View Profile Email
« Reply #1 on: June 30, 2017, 08:26:58 PM »

Please look at these examples, they work fine on chrome browser.

https://www.jeasyui.com/demo/main/index.php?plugin=ProgressBar&theme=default&dir=ltr&pitem=&sort=#
Logged
cbutaru
Newbie
*
Posts: 4


View Profile
« Reply #2 on: July 11, 2017, 11:01:44 AM »

Ok , let's try another approach, the generator flow control
The generator/yield version looks like synchronous code, but it is
in fact a true async code, because it yields the promise, exits the function,
and free the ES6/7 engine to do other tasks.
Not a surprise, Firefox pass also the generator test, but Chrome failed again, as in sync flow.

Help, help, help ...  Shocked

Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Progress Messager</title>
<link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../../themes/icon.css">
<link rel="stylesheet" type="text/css" href="../demo.css">
<script type="text/javascript" src="../../jquery.min.js"></script>
<script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
<h2>Progress Messager</h2>

<div id="p" class="easyui-panel" title="Messages" style="width:700px;height:200px;padding:10px;">
</div>

<div style="margin:20px 0;">
<a href="#" class="easyui-linkbutton" onclick="processAsync()">Async flow</a>
<a href="#" class="easyui-linkbutton" onclick="processSync()">Sync flow</a>
<a href="#" class="easyui-linkbutton" onclick="processGen()">Gen flow</a>
</div>

<script type="text/javascript">
    "use strict";
     
  function showWaitMsg(message) {
var win = $.messager.progress({
title:'Please wait ...',
msg: message
});
}   

function closeWaitMsg() {
var bar = $.messager.progress('bar');
$.messager.progress('close');
}   

function changeProgressBar(index, max) {
var bar = $.messager.progress('bar');
var prval =  Math.floor((10000 * index) / max)/100;
bar.progressbar('setValue', prval);
}

function loadPage(aurl, fCallback) {
var xhr = new XMLHttpRequest();
if(typeof fCallback === 'function') {
xhr.callback = fCallback;
function xhrSuccess () {
this.callback.apply(this, this.arguments);
}
xhr.onload = xhrSuccess;
}
xhr.open('get', aurl, false);
xhr.send();
}

function processSync() {
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
setTimeout(function() {
for(var i=0; i<100; i++) {
changeProgressBar(i,100);
loadPage('/');
$('#p').append('Page: '+i.toString()+'<br>');
}
closeWaitMsg();
$('#p').append('Stop page processing <br>');
},1);
}

function processPage(i) {
setTimeout(function() {
changeProgressBar(i,100);
loadPage('/',
function() {     
$('#p').append('Page: '+i.toString()+'<br>');
if(i<100) {
processPage(i+1); // recursion
} else {
closeWaitMsg();
$('#p').append('Stop page processing <br>');       
}
}
);
},1);
}

function processAsync() { 
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
processPage(0);
}

  function getPage(i) {
    changeProgressBar(i,100);
    loadPage('/');
    $('#p').append('Page: '+i.toString()+'<br>');
  }
 
  function* getPages() {
    var i=0;
    while(i<100) {
      yield getPage(i);
      i++;
    }
  };

  function runner(f) {
    var fn = f();
    var next = function (err, arg) {
      if (err) return fn.throw(err);
      var res = fn.next(arg);
      if (res.done) return;
      var func = res.value
      if (typeof func == 'function')
        func(next);
      else
        next(null, func);
    }
    next();
  }

  function processGen() {
    $('#p').html('');
    $('#p').append('Start page processing<br>');
    showWaitMsg('Processing pages ...'); 
    setTimeout(function() { 
      runner(getPages);
      closeWaitMsg();
      $('#p').append('Stop page processing <br>');
    },1);
  }

</script>
</body>
</html>
Logged
cbutaru
Newbie
*
Posts: 4


View Profile
« Reply #3 on: July 12, 2017, 10:34:51 AM »

Two more approaches
First, the newest async/await flow, promised based, also looklike sync flow
enabled in FF52+ and Chrome55+, works with FF , again failed with Chrome
And last one tested, the old (and good) worker aproach.
Works with FF (althought not so good), and also work well with Chrome (thank God !!!)
So, if you are stuck into the "callback hell", workers seems to be the last chance

But, one question remain: different  behavior FF vs Chrome means different JS engine
implementation ? It seems like it means, because I did a test with the equivalent
messagebox and progressbar from another JS UI (Sencha ExtJS) and the results
are identically with EasyUI, exactly the same behavior.

Ok, these being said, I hope someone can find these tests useful, and I think this
topic can be closed,  if there's nothing else to add.

Thanks to @jarry for help ! Smiley

Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Progress Messager</title>
<link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../../themes/icon.css">
<link rel="stylesheet" type="text/css" href="../demo.css">
<script type="text/javascript" src="../../jquery.min.js"></script>
<script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
<h2>Progress Messager</h2>

<div id="p" class="easyui-panel" title="Messages" style="width:700px;height:200px;padding:10px;">
</div>

<div style="margin:20px 0;">
<a href="#" class="easyui-linkbutton" onclick="processAsync()">Async flow</a>
<a href="#" class="easyui-linkbutton" onclick="processSync()">Sync flow</a>
<a href="#" class="easyui-linkbutton" onclick="processGen()">Gen flow</a>
<a href="#" class="easyui-linkbutton" onclick="processAwait()">Await flow</a>
<a href="#" class="easyui-linkbutton" onclick="processWorker()">Worker flow</a>
</div>

<script type="text/javascript">
      
  function showWaitMsg(message) {
var win = $.messager.progress({
title:'Please wait ...',
msg: message
});
}    

function closeWaitMsg() {
var bar = $.messager.progress('bar');
$.messager.progress('close');
}    

function changeProgressBar(index, max) {
var bar = $.messager.progress('bar');
var prval =  Math.floor((10000 * index) / max)/100;
bar.progressbar('setValue', prval);
}

function loadPage(aurl, fCallback) {
var xhr = new XMLHttpRequest();
if(typeof fCallback === 'function') {
xhr.callback = fCallback;
function xhrSuccess () {
this.callback.apply(this, this.arguments);
}
xhr.onload = xhrSuccess;
}
xhr.open('get', aurl, false);
xhr.send();
}

function processSync() {
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
setTimeout(function() {
for(var i=0; i<100; i++) {
changeProgressBar(i,100);
loadPage('/');
$('#p').append('Page: '+i.toString()+'<br>');
}
closeWaitMsg();
$('#p').append('Stop page processing <br>');
},1);
}

function processPage(i) {
setTimeout(function() {
changeProgressBar(i,100);
loadPage('/',
function() {      
$('#p').append('Page: '+i.toString()+'<br>');
if(i<100) {
processPage(i+1); // recursion
} else {
closeWaitMsg();
$('#p').append('Stop page processing <br>');        
}
}
);
},1);
}

function processAsync() {  
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
processPage(0);
}

  function getPage(i) {
    changeProgressBar(i,100);
    loadPage('/');
    $('#p').append('Page: '+i.toString()+'<br>');
  }
  
  function* getPages() {
    var i=0;
    while(i<100) {
      yield getPage(i);
      i++;
    }
  };

  function runner(f) {
    var fn = f();
    var next = function (err, arg) {
      if (err) return fn.throw(err);
      var res = fn.next(arg);
      if (res.done) return;
      var func = res.value
      if (typeof func == 'function')
        func(next);
      else
        next(null, func);
    }
    next();
  }

  function processGen() {
    $('#p').html('');
    $('#p').append('Start page processing<br>');
    showWaitMsg('Processing pages ...');  
    setTimeout(function() {  
      runner(getPages);
      closeWaitMsg();
      $('#p').append('Stop page processing <br>');
    },1);
  }

  function processWorker() {
    $('#p').html('');
    showWaitMsg('Processing pages ...');  
    var worker = new Worker('worker.js');
    var i = 0;
    worker.onmessage = function (event) {
      i++;
      if(i<100) {
        changeProgressBar(i,100);
        $('#p').append('Page: '+i.toString()+'<br>');
      } else {
        closeWaitMsg();
        $('#p').append('Stop page processing <br>');          
      }
    };  
  }


function promisePage() {
    return new Promise(resolve => {
        loadPage('/');
    });
  }
  
  async function getPromisePage() {
     await promisePage();
  }

function processAwait() {
$('#p').html('');
$('#p').append('Start page processing<br>');
showWaitMsg('Processing pages ...');
setTimeout(function() {
for(var i=0; i<100; i++) {
changeProgressBar(i,100);
getPromisePage();
$('#p').append('Page: '+i.toString()+'<br>');
}
closeWaitMsg();
$('#p').append('Stop page processing <br>');
},1);  
}

</script>
</body>
</html>

worker.js
Code:
function loadPage(aurl, fCallback) {
  var xhr = new XMLHttpRequest();
  if(typeof fCallback === 'function') {
    xhr.callback = fCallback;
    function xhrSuccess () {
      this.callback.apply(this, this.arguments);
    }
    xhr.onload = xhrSuccess;
  }
  xhr.open('get', aurl, false);
  xhr.send();
}

for(var i=0; i<100; i++) {
  loadPage('/');
  postMessage(i);
}
« Last Edit: July 12, 2017, 11:43:13 AM by cbutaru » Logged
lloyd
Newbie
*
Posts: 29


View Profile Email
« Reply #4 on: July 25, 2017, 09:02:57 AM »

This working for me in Firefox and Chrome:-

Code:
    function selectionsExport() {
        if (typeof(EventSource) !== "undefined") {
            $.messager.show({
                title: 'Please waiting',
                msg: 'Generating data...'
            });
           
            $.messager.progress({
                title: 'Please waiting',
                msg: 'Writing data...',
                interval: 0
            });
           
            var es = new EventSource("selectionsExportToCSV.php?selectionID="
                    + selectedRow.IFM_SELECTION_ID);
            es.onmessage = function(e) {
                var result = JSON.parse(e.data);
               
                if (result.errorMsg) {
                    $.messager.alert({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                    result.progress = 100;
                }
               
                $.messager.progress('bar').progressbar('setValue', result.progress);
               
                if (result.progress === 100) {
                    es.close();
                    $.messager.progress('close');

                    if (result.errorMsg === false) {
                        window.location.href = "selectionsExportToCSV.php?selectionID="
                                + selectedRow.IFM_SELECTION_ID + "&action=fetch";
                    }
                }
            };
           
            es.onerror = function(e) {
                es.close();
                $.messager.progress('close');
               
                $.messager.alert({
                    title: 'Error',
                    msg: "Export failed with unknown error!"
                });
            }
        }
        else {
            $.messager.alert({
                title: 'Error',
                msg: "Server sent events, not supported!"
            });
        }
    }

Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!