|
2 | 2 |
|
3 | 3 | var pkg = require('../package.json');
|
4 | 4 | var app = require('ampersand-app');
|
| 5 | +var backoff = require('backoff'); |
| 6 | + |
5 | 7 | app.extend({
|
6 | 8 | // @todo (imlucas) Move to config
|
7 | 9 | // `scout-server` to point at.
|
@@ -30,6 +32,31 @@ var Statusbar = require('./statusbar');
|
30 | 32 |
|
31 | 33 | var debug = require('debug')('scout:app');
|
32 | 34 |
|
| 35 | +function getConnection(model, done) { |
| 36 | + function _fetch(fn) { |
| 37 | + model.fetch({ |
| 38 | + success: function() { |
| 39 | + debug('_fetch connection succeeded!'); |
| 40 | + fn(); |
| 41 | + }, |
| 42 | + error: function() { |
| 43 | + debug('_fetch connection failed', arguments); |
| 44 | + fn(new Error('Error retrieving connection details')); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + var call = backoff.call(_fetch, done); |
| 50 | + call.setStrategy(new backoff.ExponentialStrategy({ |
| 51 | + randomisationFactor: 0, |
| 52 | + initialDelay: 10, |
| 53 | + maxDelay: 500 |
| 54 | + })); |
| 55 | + call.failAfter(10); |
| 56 | + call.start(); |
| 57 | +} |
| 58 | + |
| 59 | + |
33 | 60 | // Inter-process communication with main process (Electron window)
|
34 | 61 | var ipc = window.require('ipc');
|
35 | 62 |
|
@@ -253,25 +280,21 @@ app.extend({
|
253 | 280 | });
|
254 | 281 |
|
255 | 282 | debug('looking up connection `%s`...', connection_id);
|
256 |
| - state.connection.fetch({ |
257 |
| - success: function() { |
258 |
| - app.statusbar.show('Connection details loaded! Initializing client...'); |
259 |
| - |
260 |
| - var endpoint = app.endpoint; |
261 |
| - var connection = state.connection.serialize(); |
262 |
| - |
263 |
| - app.client = getOrCreateClient(endpoint, connection) |
264 |
| - .on('readable', state.onClientReady.bind(state)) |
265 |
| - .on('error', state.onFatalError.bind(state, 'create client')); |
266 |
| - |
267 |
| - state.startClientStalledTimer(); |
268 |
| - }, |
269 |
| - error: function() { |
270 |
| - // @todo (imlucas) `ampersand-sync-localforage` currently drops |
271 |
| - // the real error so for now just use a generic. |
272 |
| - state.onFatalError(state, 'fetch connection', |
273 |
| - new Error('Error retrieving connection. Please reload the page.')); |
| 283 | + getConnection(state.connection, function(err) { |
| 284 | + if (err) { |
| 285 | + state.onFatalError('fetch connection', err); |
| 286 | + return; |
274 | 287 | }
|
| 288 | + app.statusbar.show('Connecting to MongoDB...'); |
| 289 | + |
| 290 | + var endpoint = app.endpoint; |
| 291 | + var connection = state.connection.serialize(); |
| 292 | + |
| 293 | + app.client = getOrCreateClient(endpoint, connection) |
| 294 | + .on('readable', state.onClientReady.bind(state)) |
| 295 | + .on('error', state.onFatalError.bind(state, 'create client')); |
| 296 | + |
| 297 | + state.startClientStalledTimer(); |
275 | 298 | });
|
276 | 299 | });
|
277 | 300 | // set up ipc
|
|
0 commit comments