Files
www/js/lastblock.js

59 lines
1.3 KiB
JavaScript

/*
* Ce module permet d'écouter la blockchain
* Il maintient un tableau de hooks
* Il vérifie l'existence d'un nouveau block toutes les 30s
* Si un nouveau block est détecté, les hooks sont lancés un par un
*/
blockchainListener = function(){
var _last_block = null;
var _last_block_hooks = [];
function _logBlockHash(leblock)
{
console.log('Last Block detected : '+leblock.hash);
return true;
};
function _isBlockNew(leblock)
{
return ((_last_block == null)||(_last_block.hash != leblock.hash));
};
function _getLastBlockInfo()
{
$.get( "data/getBlockInfo.php", function( data ) {
if (_last_block_hooks.length > 0)
{
if (_isBlockNew(data))
{
_last_block = data;
_last_block_hooks.forEach(function(trigger) {
if (trigger instanceof Function)
trigger(data);
else
console.log(trigger);
});
}
}
}, "json" );
setTimeout(_getLastBlockInfo, 30000);
};
function _addBlockHook(addBlockHook){
_last_block_hooks.push(addBlockHook);
};
function _init(){
_last_block_hooks.push(_logBlockHash);
_getLastBlockInfo();
};
return {init: _init, addBlockHook: _addBlockHook};
}();
$(document).ready(function() {
blockchainListener.init();
});