From 3dcaa5d6b81144747f75b179de68c9d6ab4da9a3 Mon Sep 17 00:00:00 2001 From: molvqingtai Date: Sat, 14 Dec 2024 21:34:31 +0800 Subject: [PATCH] fix: loop triggers error event --- __tests__/index.spec.ts | 29 +++++++++++++++++++---------- src/index.ts | 19 +++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/__tests__/index.spec.ts b/__tests__/index.spec.ts index 2014ed7..a69a5d0 100644 --- a/__tests__/index.spec.ts +++ b/__tests__/index.spec.ts @@ -29,14 +29,10 @@ describe('Test timer', () => { }) }) describe('Test on event', () => { - let timer: Timer - beforeEach(() => { - timer = new Timer(() => 'foobar', { - limit: 3, + test('should emit start event', async () => { + const timer = new Timer(() => 'foobar', { interval: 50 }) - }) - test('should emit start event', async () => { const callback = vi.fn() timer.on('start', callback) timer.start() @@ -44,6 +40,9 @@ describe('Test timer', () => { expect(callback).toHaveBeenCalled() }) test('should emit pause event', async () => { + const timer = new Timer(() => 'foobar', { + interval: 50 + }) const callback = vi.fn() timer.on('pause', callback) timer.start() @@ -52,6 +51,9 @@ describe('Test timer', () => { expect(callback).toHaveBeenCalled() }) test('should emit stop event', async () => { + const timer = new Timer(() => 'foobar', { + interval: 50 + }) const callback = vi.fn() timer.on('stop', callback) timer.start() @@ -59,23 +61,29 @@ describe('Test timer', () => { expect(callback).toHaveBeenCalled() }) test('should emit end event', async () => { + const timer = new Timer(() => 'foobar', { + interval: 1000, + limit: 1 + }) const callback = vi.fn() timer.on('end', callback) timer.start() - await vi.advanceTimersByTimeAsync(200) + await vi.advanceTimersByTimeAsync(2000) timer.stop() expect(callback).toHaveBeenCalled() }) test('should emit tick event', async () => { + const timer = new Timer(() => 'foobar', { + interval: 1000 + }) const callback = vi.fn() timer.on('tick', callback) timer.start() - await vi.advanceTimersByTimeAsync(100) + await vi.advanceTimersByTimeAsync(2000) timer.stop() expect(callback).toHaveBeenCalledWith('foobar') }) test('should emit error event', async () => { - const callback = vi.fn() const timer = new Timer( () => { throw new Error('foobar') @@ -84,9 +92,10 @@ describe('Test timer', () => { limit: 1 } ) + const callback = vi.fn() timer.on('error', callback) timer.start() - await vi.advanceTimersByTimeAsync(100) + await vi.advanceTimersByTimeAsync(1000) timer.stop() expect(callback).toHaveBeenCalledWith(expect.objectContaining({ message: 'foobar' })) }) diff --git a/src/index.ts b/src/index.ts index 759c20f..e7544ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -112,8 +112,8 @@ export default class Timer { if (this.status === 'running') { const elapsedTime = currentTime - this.startTime if (this.limit > 0) { - try { - if (immediate ?? elapsedTime >= this.interval) { + if (immediate ?? elapsedTime >= this.interval) { + try { if (this.includeAsyncTime) { const data = await this.callback(Date.now(), this) this.eventHub.emit('tick', data) @@ -121,18 +121,17 @@ export default class Timer { const data = this.callback(Date.now(), this) this.eventHub.emit('tick', data) } + } catch (error) { + this.eventHub.emit('error', error) + } finally { this.limit-- this.startTime = Date.now() } - } catch (error) { - this.startTime = Date.now() - this.eventHub.emit('error', error) - } finally { - this.timerId && this.adapter.cancelTimer(this.timerId) - this.timerId = this.adapter.setTimer(() => { - this.tick(Date.now()) - }) } + this.timerId && this.adapter.cancelTimer(this.timerId) + this.timerId = this.adapter.setTimer(() => { + this.tick(Date.now()) + }) } else { this.stop() }