From 3d93adb3f8e37076d52f4d7591670412b4cdb72c Mon Sep 17 00:00:00 2001 From: molvqingtai Date: Mon, 2 Dec 2024 07:18:48 +0800 Subject: [PATCH] perf: delete reset api --- __tests__/index.spec.ts | 33 --------------------------------- src/index.ts | 17 ++++------------- 2 files changed, 4 insertions(+), 46 deletions(-) diff --git a/__tests__/index.spec.ts b/__tests__/index.spec.ts index 70f5e45..0eddbb8 100644 --- a/__tests__/index.spec.ts +++ b/__tests__/index.spec.ts @@ -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', () => { @@ -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 () => { diff --git a/src/index.ts b/src/index.ts index 5c37a84..d793bb3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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 @@ -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' @@ -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 } } @@ -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