Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
molvqingtai committed Nov 30, 2024
2 parents 30fdc34 + 19bcf46 commit 665ad28
Show file tree
Hide file tree
Showing 14 changed files with 2,238 additions and 2,739 deletions.
27 changes: 0 additions & 27 deletions .eslintrc

This file was deleted.

2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://github.com/dependabot/dependabot-core/issues/1736

version: 2
updates:
# Enable version updates for npm
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
Expand All @@ -20,12 +20,12 @@ jobs:
version: latest
- run: pnpm install --ignore-scripts
- run: pnpm run lint
- run: pnpm run tsc
- run: pnpm run check
tests:
needs: linter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
Expand All @@ -38,10 +38,7 @@ jobs:
needs: tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# https://github.com/semantic-release/semantic-release/issues/2636
with:
persist-credentials: false
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
Expand All @@ -20,12 +20,12 @@ jobs:
version: latest
- run: pnpm install --ignore-scripts
- run: pnpm run lint
- run: pnpm run tsc
- run: pnpm run check
tests:
needs: linter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
Expand Down
5 changes: 1 addition & 4 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm commitlint --edit ""
pnpm commitlint --edit "$1"
5 changes: 1 addition & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm run lint & pnpm run tsc
pnpm lint-staged && pnpm check
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"eslint.options": {
"flags": ["unstable_ts_config"]
},
"eslint.useFlatConfig": true
}
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# Timer

[![version](https://img.shields.io/github/v/release/molvqingtai/timer)](https://www.npmjs.com/package/timer) [![workflow](https://github.com/molvqingtai/timer/actions/workflows/ci.yml/badge.svg)](https://github.com/molvqingtai/timer/actions) [![download](https://img.shields.io/npm/dt/@resreq/timer)](https://www.npmjs.com/package/@resreq/timer) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

⏰ Short and sweet timer based on requestAnimationFrame API

[![version](https://img.shields.io/github/v/release/molvqingtai/timer)](https://www.npmjs.com/package/@resreq/timer) [![workflow](https://github.com/molvqingtai/timer/actions/workflows/ci.yml/badge.svg)](https://github.com/molvqingtai/timer/actions) [![download](https://img.shields.io/npm/dt/@resreq/timer)](https://www.npmjs.com/package/@resreq/timer) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

⏰ Short and sweet timer

## Install

```shell
npm install @resreq/timer
```



## Usage

```typescript
Expand All @@ -22,7 +18,7 @@ import Timer from '@resreq/timer'
const log = (time: number) => console.log('time:', time)

const timer = new Timer(log, {
delay: 1000,
interval: 1000,
immediate: true
})

Expand Down Expand Up @@ -50,7 +46,17 @@ setTimeout(() => timer.stop(), 3000)
// => stop: 1712160515855
```

**Adapter**
SetTimeout is used by default, and custom adapters are supported, such as requestAnimationFrame, cancelIdleCallback, etc...

```typescript
const timer = new Timer(log, {
adapter: {
setTimer: globalThis.requestAnimationFrame.bind(globalThis),
cancelTimer: globalThis.cancelAnimationFrame.bind(globalThis)
}
})
```

## LICENSE

Expand Down
66 changes: 35 additions & 31 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ describe('Test timer', () => {

test('Status should work properly', async () => {
timer.start()
expect(timer.status).toBe('started')
expect(timer.status).toBe('running')
timer.pause()
expect(timer.status).toBe('paused')
timer.start()
expect(timer.status).toBe('started')
expect(timer.status).toBe('running')
timer.pause()
expect(timer.status).toBe('paused')
timer.stop()
Expand All @@ -27,7 +27,7 @@ describe('Test timer', () => {
beforeEach(() => {
timer = new Timer(() => 'foobar', {
limit: 3,
delay: 50
interval: 50
})
})
test('should emit start event', async () => {
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('Test timer', () => {
const callback = vi.fn()
const timer = new Timer(callback, {
limit: 3,
delay: 100
interval: 100
})
timer.start()
await sleep(250)
Expand All @@ -112,7 +112,7 @@ describe('Test timer', () => {
const callback = vi.fn()
const timer = new Timer(callback, {
limit: 3,
delay: 100
interval: 100
})
timer.start()
await sleep(350)
Expand All @@ -123,7 +123,7 @@ describe('Test timer', () => {
const callback = vi.fn()
const timer = new Timer(callback, {
limit: 3,
delay: 100
interval: 100
})
timer.start()
await sleep(250)
Expand All @@ -135,11 +135,11 @@ describe('Test timer', () => {
const callback = vi.fn()
const timer = new Timer(callback, {
limit: 5,
delay: 100
interval: 100
})
timer.start()
await sleep(150)
// delay 100 < sleep 150 = +1 times
// interval 100 < sleep 150 = +1 times
expect(callback).toHaveBeenCalledTimes(1)
// last = 50
timer.pause()
Expand All @@ -150,24 +150,24 @@ describe('Test timer', () => {
timer.start()
await sleep(75)
timer.stop()
// delay < (last 50 + sleep 75)= +1 times
// interval < (last 50 + sleep 75)= +1 times
expect(callback).toHaveBeenCalledTimes(2)
})

test('should call callback immediately', async () => {
const callback = vi.fn()
const timer = new Timer(callback, {
delay: 100,
interval: 100,
immediate: true
})
timer.start()
await sleep(0)
await sleep(50)
expect(callback).toHaveBeenCalledTimes(1)
})
test('should call callback reset', async () => {
const callback = vi.fn()
const timer = new Timer(callback, {
delay: 100,
interval: 100,
limit: 1,
immediate: false
})
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('Test timer', () => {
}
const timer = new Timer(promise, {
limit: 3,
delay: 100,
interval: 100,
includeAsyncTime: true
})
timer.start()
Expand All @@ -221,7 +221,7 @@ describe('Test timer', () => {
}
const timer = new Timer(promise, {
limit: 3,
delay: 100,
interval: 100,
includeAsyncTime: true
})
timer.start()
Expand All @@ -237,7 +237,7 @@ describe('Test timer', () => {
}
const timer = new Timer(promise, {
limit: 3,
delay: 100,
interval: 100,
includeAsyncTime: true
})
timer.start()
Expand All @@ -254,24 +254,24 @@ describe('Test timer', () => {
}
const timer = new Timer(promise, {
limit: 5,
// delay = delay 100 + promise 100 = 200
delay: 100
// interval = interval 100 + promise 100 = 200
interval: 100
})
timer.start()
await sleep(150)
// delay 200 > sleep 150 = +0 times
// interval 200 > sleep 150 = +0 times
expect(callback).toHaveBeenCalledTimes(0)
// Time is paused, but promise continues
timer.pause()
await sleep(100)
// delay 200 < (sleep 150 + sleep 100) = +1 times
// interval 200 < (sleep 150 + sleep 100) = +1 times
// last = 50
expect(callback).toHaveBeenCalledTimes(1)
// inherit last 50
timer.start()
await sleep(180)
timer.stop()
// delay 200 < (last 50 + sleep 180) = +1 times
// interval 200 < (last 50 + sleep 180) = +1 times
expect(callback).toHaveBeenCalledTimes(2)
})

Expand All @@ -281,12 +281,12 @@ describe('Test timer', () => {
Promise.resolve(callback())
}
const timer = new Timer(promise, {
delay: 100,
interval: 100,
immediate: true,
includeAsyncTime: true
})
timer.start()
await sleep(0)
await sleep(50)
expect(callback).toHaveBeenCalledTimes(1)
})

Expand All @@ -296,12 +296,12 @@ describe('Test timer', () => {
Promise.resolve(callback())
}
const timer = new Timer(promise, {
delay: 100,
interval: 100,
immediate: false,
includeAsyncTime: true
})
timer.start()
await sleep(0)
await sleep(50)
expect(callback).toHaveBeenCalledTimes(0)
})

Expand All @@ -311,7 +311,7 @@ describe('Test timer', () => {
Promise.resolve(callback())
}
const timer = new Timer(promise, {
delay: 100,
interval: 100,
limit: 1,
immediate: false,
includeAsyncTime: true
Expand All @@ -327,13 +327,17 @@ describe('Test timer', () => {
describe('Test use timer in Callback', () => {
test('should emit stop event', async () => {
const callback = vi.fn()
const timer = new Timer((_, timer: Timer) => {
timer.stop()
})
timer.start()
const timer = new Timer(
(_, timer: Timer) => {
timer.stop()
},
{ limit: 1 }
)
timer.on('stop', callback)
await sleep(0)
expect(callback).toHaveBeenCalled()

timer.start()
await sleep(100)
expect(callback).toHaveBeenCalledTimes(1)
})
})
})
Loading

0 comments on commit 665ad28

Please sign in to comment.