lastbloc.js is now a module

This commit is contained in:
2018-12-01 10:47:03 +01:00
parent 1d90cd185a
commit 24bbee0109
4 changed files with 44 additions and 34 deletions

View File

@@ -32,5 +32,5 @@ function init_2018025(leblock)
} }
$(document).ready(function(){ $(document).ready(function(){
last_block_hooks.push(init_2018025); blockchainListener.addBlockHook(init_2018025);
}); });

View File

@@ -127,7 +127,7 @@
downloadingImage.src = 'images/block_image.php?methode=hasard&hash='+le_block.hash; downloadingImage.src = 'images/block_image.php?methode=hasard&hash='+le_block.hash;
} }
$(document).ready(function(){ $(document).ready(function(){
last_block_hooks.push(changeExploreBlockDrawing); blockchainListener.addBlockHook(changeExploreBlockDrawing);
}); });
</script> </script>

View File

@@ -281,5 +281,5 @@
} }
$(document).ready(function(){ $(document).ready(function(){
addBlockHook(initBlockExplorer); blockchainListener.addBlockHook(initBlockExplorer);
}); });

View File

@@ -1,39 +1,49 @@
function logBlockHash(leblock) /*
{ * Ce module permet d'écouter la blockchain
console.log('Last Block detected : '+leblock.hash); */
return true; blockchainListener = function(){
}
var last_block = null; var _last_block = null;
var last_block_hooks = [logBlockHash]; var _last_block_hooks = [];
var last_block_refresh_flag = true;
function _logBlockHash(leblock)
{
console.log('Last Block detected : '+leblock.hash);
return true;
};
function getLastBlockInfo() function _isBlockNew(leblock)
{ {
$.get( "data/getBlockInfo.php", function( data ) { return ((_last_block == null)||(_last_block.hash != leblock.hash));
if ((last_block == null)||(last_block.hash != data.hash)) };
{
if (last_block_refresh_flag && (last_block != null)) function _getLastBlockInfo()
last_block_refresh_flag = window.confirm('New block detected, apply change ?'); {
$.get( "data/getBlockInfo.php", function( data ) {
last_block = data; if (_isBlockNew(data))
{
if (last_block_refresh_flag) _last_block = data;
{ _last_block_hooks.forEach(function(element) {
last_block_hooks.forEach(function(element) { element(data);
element(last_block);
}); });
} }
} }, "json" );
}, "json" );
setTimeout(getLastBlockInfo, 30000); setTimeout(_getLastBlockInfo, 30000);
} };
function addBlockHook(addBlockHook){ function _addBlockHook(addBlockHook){
last_block_hooks.push(addBlockHook); _last_block_hooks.push(addBlockHook);
} };
function _init(){
_last_block_hooks.push(_logBlockHash);
_getLastBlockInfo();
};
return {init: _init, addBlockHook: _addBlockHook};
}();
$(document).ready(function() { $(document).ready(function() {
getLastBlockInfo(); blockchainListener.init();
}); });