From 24bbee0109acc43816532278c56e9d440bfbdae6 Mon Sep 17 00:00:00 2001 From: MEUNIER Thibaud Date: Sat, 1 Dec 2018 10:47:03 +0100 Subject: [PATCH] lastbloc.js is now a module --- articles/20180225/header.js | 2 +- index.php | 2 +- js/blockexplorer.js | 2 +- js/lastblock.js | 72 +++++++++++++++++++++---------------- 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/articles/20180225/header.js b/articles/20180225/header.js index bc829ad..87df781 100644 --- a/articles/20180225/header.js +++ b/articles/20180225/header.js @@ -32,5 +32,5 @@ function init_2018025(leblock) } $(document).ready(function(){ - last_block_hooks.push(init_2018025); + blockchainListener.addBlockHook(init_2018025); }); diff --git a/index.php b/index.php index edde9ee..24fd754 100644 --- a/index.php +++ b/index.php @@ -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); }); diff --git a/js/blockexplorer.js b/js/blockexplorer.js index 8ce3b7d..2db7ef4 100644 --- a/js/blockexplorer.js +++ b/js/blockexplorer.js @@ -281,5 +281,5 @@ } $(document).ready(function(){ - addBlockHook(initBlockExplorer); + blockchainListener.addBlockHook(initBlockExplorer); }); diff --git a/js/lastblock.js b/js/lastblock.js index e9fe919..84f82f6 100644 --- a/js/lastblock.js +++ b/js/lastblock.js @@ -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); + }; + + function _addBlockHook(addBlockHook){ + _last_block_hooks.push(addBlockHook); + }; - setTimeout(getLastBlockInfo, 30000); -} + function _init(){ + _last_block_hooks.push(_logBlockHash); + _getLastBlockInfo(); + }; -function addBlockHook(addBlockHook){ - last_block_hooks.push(addBlockHook); -} + return {init: _init, addBlockHook: _addBlockHook}; +}(); $(document).ready(function() { - getLastBlockInfo(); + blockchainListener.init(); });