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(){
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;
}
$(document).ready(function(){
last_block_hooks.push(changeExploreBlockDrawing);
blockchainListener.addBlockHook(changeExploreBlockDrawing);
});
</script>

View File

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

View File

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