Skip to content

Commit 59eb28c

Browse files
fix: add clearInterval in timers API
Add clearInterval in timers API, so we can safely remove the cached timers from our internal cache. Without this, we end up calling many clearIntervals for timers that have already been cleared.
1 parent ec59a35 commit 59eb28c

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

lib/common/definitions/timers.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
interface ITimers {
22
setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
3+
clearInterval(intervalId: NodeJS.Timer): void;
34
}

lib/common/mobile/mobile-core/devices-service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { CONNECTED_STATUS } from "../../constants";
1010
import { isInteractive } from "../../helpers";
1111
import { DebugCommandErrors } from "../../../constants";
1212
import { performanceLog } from "../../decorators";
13-
import { Timers } from "../../timers";
1413

1514
export class DevicesService extends EventEmitter implements Mobile.IDevicesService {
1615
private static DEVICE_LOOKING_INTERVAL = 200;
@@ -44,7 +43,7 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
4443
private $androidEmulatorDiscovery: Mobile.IDeviceDiscovery,
4544
private $emulatorHelper: Mobile.IEmulatorHelper,
4645
private $prompter: IPrompter,
47-
private $timers: Timers) {
46+
private $timers: ITimers) {
4847
super();
4948
this.attachToKnownDeviceDiscoveryEvents();
5049
this.attachToKnownEmulatorDiscoveryEvents();
@@ -337,14 +336,14 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
337336
@exported("devicesService")
338337
public stopDeviceDetectionInterval(): void {
339338
if (this.deviceDetectionInterval) {
340-
clearInterval(this.deviceDetectionInterval);
339+
this.$timers.clearInterval(this.deviceDetectionInterval);
341340
}
342341
}
343342

344343
@exported("devicesService")
345344
public stopEmulatorDetectionInterval(): void {
346345
if (this.emulatorDetectionInterval) {
347-
clearInterval(this.emulatorDetectionInterval);
346+
this.$timers.clearInterval(this.emulatorDetectionInterval);
348347
}
349348
}
350349

lib/common/timers.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
export class Timers implements ITimers {
2+
private pendingIntervalIds: NodeJS.Timer[] = [];
3+
24
constructor(private $processService: IProcessService) {
5+
this.$processService.attachToProcessExitSignals(this, () => {
6+
_.each(this.pendingIntervalIds, clearInterval);
7+
});
38
}
49

510
public setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer {
611
const timer = setInterval(callback, ms, args);
7-
this.$processService.attachToProcessExitSignals(this, () => clearInterval(timer));
12+
13+
this.pendingIntervalIds.push(timer);
14+
815
return timer;
916
}
17+
18+
public clearInterval(intervalId: NodeJS.Timer): void {
19+
clearInterval(intervalId);
20+
21+
_.remove(this.pendingIntervalIds, id => id === intervalId);
22+
}
1023
}
1124

1225
$injector.register("timers", Timers);

0 commit comments

Comments
 (0)