From cdc5bd4e672f1e85f00a63b4f47b2bbccda97ccd Mon Sep 17 00:00:00 2001 From: Marshall Rose Date: Thu, 15 Sep 2016 14:24:13 -0700 Subject: [PATCH] add network connection notifications notifications on network up/down to the ledger supercedes https://github.com/brave/browser-laptop/pull/3973 --- app/index.js | 8 ++++++++ app/ledger.js | 19 +++++++++++++++++++ docs/appActions.md | 13 +++++++++++++ js/actions/appActions.js | 19 +++++++++++++++++++ js/constants/appConstants.js | 2 ++ 5 files changed, 61 insertions(+) diff --git a/app/index.js b/app/index.js index 6bb60f7b2ab..d8053bea7c6 100644 --- a/app/index.js +++ b/app/index.js @@ -309,6 +309,14 @@ app.on('ready', () => { }, appConfig.quitTimeout) }) + app.on('network-connected', () => { + appActions.networkConnected() + }) + + app.on('network-disconnected', () => { + appActions.networkDisconnected() + }) + // User initiated exit using File->Quit ipcMain.on(messages.RESPONSE_WINDOW_STATE, (wnd, data) => { if (data) { diff --git a/app/ledger.js b/app/ledger.js index a875b8bd1b6..44babd7a43b 100644 --- a/app/ledger.js +++ b/app/ledger.js @@ -109,7 +109,13 @@ const doAction = (action) => { if (action.key === settings.PAYMENTS_ENABLED) return initialize(action.value) if (action.key === settings.PAYMENTS_CONTRIBUTION_AMOUNT) return setPaymentInfo(action.value) break + + case appConstants.APP_NETWORK_CONNECTED: + setTimeout(networkConnected, 1 * msecs.second) + break + default: + break } } @@ -1167,6 +1173,19 @@ var cacheReturnValue = () => { } } +var networkConnected = underscore.debounce(() => { + if (!client) return + + if (runTimeoutId) { + clearTimeout(runTimeoutId) + runTimeoutId = false + } + if (client.sync(callback) === true) run(random.randomInt({ min: msecs.minute, max: 10 * msecs.minute })) + + if (balanceTimeoutId) clearTimeout(balanceTimeoutId) + balanceTimeoutId = setTimeout(getBalance, 5 * msecs.second) +}, 1 * msecs.minute, true) + /* * low-level utilities */ diff --git a/docs/appActions.md b/docs/appActions.md index 26889e446cd..0cc3cbde426 100644 --- a/docs/appActions.md +++ b/docs/appActions.md @@ -403,6 +403,19 @@ Dispatches a message when appWindowId loses focus +### networkConnected() + +Dispatches a message when the network is re-connected +after being disconnected + + + +### networkDisconnected() + +Dispatches a message when the network is disconnected + + + * * * diff --git a/js/actions/appActions.js b/js/actions/appActions.js index e4b01a21b6f..b32c7e478c0 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -464,6 +464,25 @@ const appActions = { actionType: AppConstants.APP_WINDOW_BLURRED, appWindowId: appWindowId }) + }, + + /** + * Dispatches a message when the network is re-connected + * after being disconnected + */ + networkConnected: function () { + AppDispatcher.dispatch({ + actionType: AppConstants.APP_NETWORK_CONNECTED + }) + }, + + /** + * Dispatches a message when the network is disconnected + */ + networkDisconnected: function () { + AppDispatcher.dispatch({ + actionType: AppConstants.APP_NETWORK_DISCONNECTED + }) } } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index baf49dd0370..7fd79a236a4 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -44,6 +44,8 @@ const AppConstants = { APP_SET_LOGIN_RESPONSE_DETAIL: _, APP_WINDOW_BLURRED: _, APP_IDLE_STATE_CHANGED: _, + APP_NETWORK_CONNECTED: _, + APP_NETWORK_DISCONNECTED: _, APP_CHANGE_NEW_TAB_DETAIL: _, APP_TAB_CREATED: _, APP_TAB_DESTROYED: _