@@ -94,6 +94,12 @@ var LedgerBridge = function () {
9494 case 'ledger-sign-typed-data':
9595 _this.signTypedData(replyAction, params.hdPath, params.domainSeparatorHex, params.hashStructMessageHex, messageId);
9696 break;
97+ case 'ledger-start-polling':
98+ _this.startConnectionPolling(replyAction, messageId);
99+ break;
100+ case 'ledger-stop-polling':
101+ _this.stopConnectionPolling(replyAction, messageId);
102+ break;
97103 }
98104 }
99105 }, false);
@@ -187,30 +193,54 @@ var LedgerBridge = function () {
187193 this.app = new _hwAppEth2.default(this.transport);
188194 }
189195
190- // The U2F transport is deprecated and the following block will
191- // throw an error in Firefox, so we cannot detect true
192- // connection status with U2F
193- if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
194- // Upon Ledger disconnect from user's machine, signal disconnect
195- this.transport.on('disconnect', function () {
196- return _this3.onDisconnect();
197- });
198-
199- // We need to poll for connection status because going into
200- // sleep mode doesn't trigger the "disconnect" event
201- var connected = await this.checkConnectionStatus();
202- this.pollingInterval = setInterval(function () {
203- _this3.checkConnectionStatus();
204- }, POLLING_INTERVAL);
205- }
196+ // Upon Ledger disconnect from user's machine, signal disconnect
197+ this.transport.on('disconnect', function () {
198+ return _this3.onDisconnect();
199+ });
206200 } catch (e) {
207201 console.log('LEDGER:::CREATE APP ERROR', e);
208202 throw e;
209203 }
210204 }
205+ }, {
206+ key: 'startConnectionPolling',
207+ value: async function startConnectionPolling(replyAction, messageId) {
208+ var _this4 = this;
209+
210+ // Prevent the possibility that there could be more than one
211+ // polling interval if stopConnectionPolling hasn't been called
212+ if (this.pollingInterval) {
213+ return false;
214+ }
215+
216+ // The U2F transport is deprecated and the following block will
217+ // throw an error in Firefox, so we cannot detect true
218+ // connection status with U2F
219+ if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
220+ // We need to poll for connection status because going into
221+ // sleep mode doesn't trigger the "disconnect" event
222+ var connected = await this.checkConnectionStatus();
223+ this.pollingInterval = setInterval(function () {
224+ _this4.checkConnectionStatus();
225+ }, POLLING_INTERVAL);
226+ }
227+ }
228+ }, {
229+ key: 'stopConnectionPolling',
230+ value: function stopConnectionPolling(replyAction, messageId) {
231+ if (this.pollingInterval) {
232+ clearInterval(this.pollingInterval);
233+ }
234+ }
211235 }, {
212236 key: 'checkConnectionStatus',
213237 value: async function checkConnectionStatus() {
238+ // If there's no app or transport, leave and signal disconnect
239+ if (!this.app || !this.transport) {
240+ this.onDisconnect();
241+ return false;
242+ }
243+
214244 // Ensure the correct (Ethereum) app is open; if not, immediately kill
215245 // the connection as the wrong app is open and switching apps will call
216246 // a disconnect from within the Ledger API
@@ -234,20 +264,21 @@ var LedgerBridge = function () {
234264 } else {
235265 // Wrong app
236266 this.onDisconnect();
267+ return false;
237268 }
238269 } catch (e) {
239270 console.log('LEDGER:::Transport check error', e);
240271 this.onDisconnect();
241272 throw e;
242273 }
243-
244- return false;
245274 }
246275 }, {
247276 key: 'updateTransportTypePreference',
248277 value: function updateTransportTypePreference(replyAction, transportType, messageId) {
249- this.transportType = transportType;
250- this.cleanUp();
278+ if (transportType != this.transportType) {
279+ this.transportType = transportType;
280+ this.cleanUp();
281+ }
251282 this.sendMessageToExtension({
252283 action: replyAction,
253284 success: true,
@@ -262,9 +293,7 @@ var LedgerBridge = function () {
262293 await this.transport.close();
263294 this.transport = null;
264295 }
265- if (this.pollingInterval) {
266- clearInterval(this.pollingInterval);
267- }
296+ this.stopConnectionPolling();
268297 if (replyAction) {
269298 this.sendMessageToExtension({
270299 action: replyAction,
0 commit comments