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

@@ -1,39 +1,49 @@
function logBlockHash(leblock)
{
console.log('Last Block detected : '+leblock.hash);
return true;
}
/*
* Ce module permet d'écouter la blockchain
*/
blockchainListener = function(){
var last_block = null;
var last_block_hooks = [logBlockHash];
var last_block_refresh_flag = true;
var _last_block = null;
var _last_block_hooks = [];
function _logBlockHash(leblock)
{
console.log('Last Block detected : '+leblock.hash);
return true;
};
function getLastBlockInfo()
{
$.get( "data/getBlockInfo.php", function( data ) {
if ((last_block == null)||(last_block.hash != data.hash))
{
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);
function _isBlockNew(leblock)
{
return ((_last_block == null)||(_last_block.hash != leblock.hash));
};
function _getLastBlockInfo()
{
$.get( "data/getBlockInfo.php", function( data ) {
if (_isBlockNew(data))
{
_last_block = data;
_last_block_hooks.forEach(function(element) {
element(data);
});
}
}
}, "json" );
}
}, "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();
});