Skip to content

Commit 963e99f

Browse files
committed
Add start and stop methods for polling
1 parent 917aa33 commit 963e99f

File tree

2 files changed

+99
-44
lines changed

2 files changed

+99
-44
lines changed

bundle.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

ledger-bridge.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export default class LedgerBridge {
6161
case 'ledger-sign-typed-data':
6262
this.signTypedData(replyAction, params.hdPath, params.domainSeparatorHex, params.hashStructMessageHex, messageId)
6363
break
64+
case 'ledger-start-polling':
65+
this.startConnectionPolling(replyAction, messageId)
66+
break
67+
case 'ledger-stop-polling':
68+
this.stopConnectionPolling(replyAction, messageId)
69+
break
6470
}
6571
}
6672
}, false)
@@ -143,27 +149,48 @@ export default class LedgerBridge {
143149
this.app = new LedgerEth(this.transport)
144150
}
145151

146-
// The U2F transport is deprecated and the following block will
147-
// throw an error in Firefox, so we cannot detect true
148-
// connection status with U2F
149-
if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
150-
// Upon Ledger disconnect from user's machine, signal disconnect
151-
this.transport.on('disconnect', () => this.onDisconnect())
152-
153-
// We need to poll for connection status because going into
154-
// sleep mode doesn't trigger the "disconnect" event
155-
const connected = await this.checkConnectionStatus()
156-
this.pollingInterval = setInterval(() => {
157-
this.checkConnectionStatus()
158-
}, POLLING_INTERVAL);
159-
}
152+
// Upon Ledger disconnect from user's machine, signal disconnect
153+
this.transport.on('disconnect', () => this.onDisconnect())
160154
} catch (e) {
161155
console.log('LEDGER:::CREATE APP ERROR', e)
162156
throw e
163157
}
164158
}
165159

160+
async startConnectionPolling(replyAction, messageId) {
161+
// Prevent the possibility that there could be more than one
162+
// polling interval if stopConnectionPolling hasn't been called
163+
if(this.pollingInterval) {
164+
return false
165+
}
166+
167+
// The U2F transport is deprecated and the following block will
168+
// throw an error in Firefox, so we cannot detect true
169+
// connection status with U2F
170+
if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
171+
// We need to poll for connection status because going into
172+
// sleep mode doesn't trigger the "disconnect" event
173+
const connected = await this.checkConnectionStatus()
174+
this.pollingInterval = setInterval(() => {
175+
this.checkConnectionStatus()
176+
}, POLLING_INTERVAL);
177+
}
178+
}
179+
180+
stopConnectionPolling(replyAction, messageId) {
181+
if (this.pollingInterval) {
182+
clearInterval(this.pollingInterval)
183+
}
184+
}
185+
166186
async checkConnectionStatus() {
187+
// If there's no app or transport, leave and signal disconnect
188+
if(!this.app || !this.transport) {
189+
this.onDisconnect();
190+
return false;
191+
}
192+
193+
167194
// Ensure the correct (Ethereum) app is open; if not, immediately kill
168195
// the connection as the wrong app is open and switching apps will call
169196
// a disconnect from within the Ledger API
@@ -187,20 +214,21 @@ export default class LedgerBridge {
187214
else {
188215
// Wrong app
189216
this.onDisconnect()
217+
return false
190218
}
191219
}
192220
catch(e) {
193221
console.log('LEDGER:::Transport check error', e)
194222
this.onDisconnect()
195223
throw e
196224
}
197-
198-
return false
199225
}
200226

201227
updateTransportTypePreference (replyAction, transportType, messageId) {
202-
this.transportType = transportType
203-
this.cleanUp()
228+
if (transportType != this.transportType) {
229+
this.transportType = transportType
230+
this.cleanUp()
231+
}
204232
this.sendMessageToExtension({
205233
action: replyAction,
206234
success: true,
@@ -214,9 +242,7 @@ export default class LedgerBridge {
214242
await this.transport.close()
215243
this.transport = null
216244
}
217-
if (this.pollingInterval) {
218-
clearInterval(this.pollingInterval)
219-
}
245+
this.stopConnectionPolling()
220246
if (replyAction) {
221247
this.sendMessageToExtension({
222248
action: replyAction,

0 commit comments

Comments
 (0)