Skip to content

Commit

Permalink
fix: loop triggers error event
Browse files Browse the repository at this point in the history
  • Loading branch information
molvqingtai committed Dec 14, 2024
1 parent cfe75d7 commit 3dcaa5d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
29 changes: 19 additions & 10 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ 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()
timer.stop()
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()
Expand All @@ -52,30 +51,39 @@ 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()
timer.stop()
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')
Expand All @@ -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' }))
})
Expand Down
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,26 @@ 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)
} else {
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()
}
Expand Down

0 comments on commit 3dcaa5d

Please sign in to comment.