Skip to content

Commit

Permalink
perf: delete reset api
Browse files Browse the repository at this point in the history
  • Loading branch information
molvqingtai committed Dec 1, 2024
1 parent cf47e90 commit 3d93adb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 46 deletions.
33 changes: 0 additions & 33 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,6 @@ describe('Test timer', () => {
await sleep(50)
expect(callback).toHaveBeenCalledTimes(1)
})
test('should call callback reset', async () => {
const callback = vi.fn()
const timer = new Timer(callback, {
interval: 100,
limit: 1,
immediate: false
})
timer.start()
await sleep(150)
timer.reset()
timer.start()
await sleep(150)
expect(callback).toHaveBeenCalledTimes(2)
})
})

describe('Test async callback', () => {
Expand Down Expand Up @@ -304,25 +290,6 @@ describe('Test timer', () => {
await sleep(50)
expect(callback).toHaveBeenCalledTimes(0)
})

test('should call async callback reset', async () => {
const callback = vi.fn()
const promise = async () => {
Promise.resolve(callback())
}
const timer = new Timer(promise, {
interval: 100,
limit: 1,
immediate: false,
includeAsyncTime: true
})
timer.start()
await sleep(150)
timer.reset()
timer.start()
await sleep(150)
expect(callback).toHaveBeenCalledTimes(2)
})
})
describe('Test use timer in Callback', () => {
test('should emit stop event', async () => {
Expand Down
17 changes: 4 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface TimerOptions {
adapter?: TimerAdapter
}

export type TimerEvent = 'start' | 'pause' | 'stop' | 'end' | 'tick' | 'error' | 'reset'
export type TimerEvent = 'start' | 'pause' | 'stop' | 'end' | 'tick' | 'error'

export type TimerStatus = 'running' | 'paused' | 'stopped'

Expand All @@ -41,7 +41,7 @@ export default class Timer {
private readonly includeAsyncTime: boolean
public status: TimerStatus
private readonly eventHub: EventHub
readonly adapter: TimerAdapter
private readonly adapter: TimerAdapter

constructor(callback: TimerCallback, options?: TimerOptions) {
this.startTime = 0
Expand Down Expand Up @@ -69,7 +69,7 @@ export default class Timer {
this.eventHub.on(event, listener)
}

async start() {
start() {
if (this.status === 'stopped' || this.status === 'paused') {
if (this.limit > 0) {
this.status = 'running'
Expand All @@ -89,7 +89,7 @@ export default class Timer {
this.pausedTime = 0
this.limit === 0 && this.eventHub.emit('end', Date.now())
this.adapter.cancelTimer(this.timerId!)
this.limit = this.initLimit
this.limit = this.originalLimit
this.timerId = null
}
}
Expand All @@ -104,15 +104,6 @@ export default class Timer {
}
}

reset() {
this.eventHub.emit('reset', Date.now())
this.adapter.cancelTimer(this.timerId!)
this.limit = this.originalLimit
this.pausedTime = 0
this.status = 'stopped'
this.timerId = null
}

private async tick(currentTime: number, immediate?: boolean) {
if (this.status === 'running') {
const elapsedTime = currentTime - this.startTime
Expand Down

0 comments on commit 3d93adb

Please sign in to comment.