Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.4 #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This timer is very simple and framework independent.

## Install

```
```sh
npm install @shhhplus/timer.js --save
```

Expand All @@ -15,27 +15,23 @@ npm install @shhhplus/timer.js --save
```javascript
import createTimer from '@shhhplus/timer.js';

// init
const timer = createTimer({
interval: 1000,
onElapsed: () => {
console.log('onElapsed ...');
},
});

// start
timer?.start();
timer.start();

// stop
timer?.stop();
timer.stop();
```

### async

```javascript
import createTimer from '@shhhplus/timer.js';

// init
const timer = createTimer({
interval: 5000,
onElapsed: () => {
Expand All @@ -46,9 +42,7 @@ const timer = createTimer({
},
});

// start
timer?.start();
timer.start();

// stop
timer?.stop();
timer.stop();
```
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timer.js",
"version": "2.0.3",
"version": "2.0.4",
"description": "This timer is very simple and framework independent.",
"keywords": [
"timer",
Expand Down
6 changes: 3 additions & 3 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const timer = createTimer({
},
});
console.log('timer start');
timer?.start();
timer.start();

setTimeout(() => {
console.log('timer stop');
timer?.stop();
}, 20000);
timer.stop();
}, 5000);
84 changes: 49 additions & 35 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,7 @@ import createTimer from './index';
// duration: |
// ==|==|==|

test('create timer should return null when interval <= 0', () => {
expect(
createTimer({
interval: 0,
onElapsed: () => {},
}),
).toBe(null);

expect(
createTimer({
interval: -100,
onElapsed: () => {},
}),
).toBe(null);
});

test('create timer should success when interval > 0', () => {
test('create timer should success', () => {
const timer = createTimer({
interval: 100,
onElapsed: () => {},
Expand All @@ -30,7 +14,7 @@ test('create timer should success when interval > 0', () => {
expect(timer).toHaveProperty('stop');
});

test('timer.start can be called multiple times', (done) => {
test('timer.start should be called multiple times', (done) => {
const mockFn = jest.fn(() => {});

const interval = 200;
Expand All @@ -40,19 +24,19 @@ test('timer.start can be called multiple times', (done) => {
interval,
onElapsed: mockFn,
});
timer?.start();
timer?.start();
timer.start();
timer.start();

const duration2stop = interval * (count + 1) - 1;

setTimeout(() => {
timer?.stop();
timer.stop();
expect(mockFn.mock.calls.length).toBe(count);
done();
}, duration2stop);
});

test('timer.stop can be called multiple times', (done) => {
test('timer.stop should be called multiple times', (done) => {
const mockFn = jest.fn(() => {});

const interval = 200;
Expand All @@ -62,19 +46,49 @@ test('timer.stop can be called multiple times', (done) => {
interval,
onElapsed: mockFn,
});
timer?.start();
timer.start();

const duration2stop = interval * (count + 1) - 1;

setTimeout(() => {
timer?.stop();
timer?.stop();
timer.stop();
timer.stop();
expect(mockFn.mock.calls.length).toBe(count);
done();
}, duration2stop);
});

test('sync onElapsed should work', (done) => {
test('onElapsed should not be called when interval = 0', (done) => {
const mockFn = jest.fn(() => {});
const timer = createTimer({
interval: 0,
onElapsed: mockFn,
});
timer.start();

setTimeout(() => {
timer.stop();
expect(mockFn.mock.calls.length).toBe(0);
done();
}, 100);
});

test('onElapsed should not be called when interval < 0', (done) => {
const mockFn = jest.fn(() => {});
const timer = createTimer({
interval: -1,
onElapsed: mockFn,
});
timer.start();

setTimeout(() => {
timer.stop();
expect(mockFn.mock.calls.length).toBe(0);
done();
}, 100);
});

test('sync onElapsed should be called', (done) => {
const mockFn = jest.fn(() => {});

const interval = 200;
Expand All @@ -84,21 +98,21 @@ test('sync onElapsed should work', (done) => {
interval,
onElapsed: mockFn,
});
timer?.start();
timer.start();

const duration2stop = interval * (count + 1) - 1;

setTimeout(() => {
timer?.stop();
timer.stop();
expect(mockFn.mock.calls.length).toBe(count);
done();
}, duration2stop);
});

test('async onElapsed should work', (done) => {
test('async onElapsed should be called', (done) => {
const mockFn = jest.fn(() => {});

const interval = 400;
const interval = 200;
const duration = 100;
const count = 5;

Expand All @@ -113,10 +127,10 @@ test('async onElapsed should work', (done) => {
});
},
});
timer?.start();
timer.start();

setTimeout(() => {
timer?.stop();
timer.stop();
expect(mockFn.mock.calls.length).toBe(count);
done();
}, duration2stop);
Expand All @@ -126,7 +140,7 @@ test('stop timer before onElapsed is finished should work', (done) => {
const mockFn = jest.fn(() => {});

const interval = 100;
const duration = 400;
const duration = 200;
const count = 3;

const timer = createTimer({
Expand All @@ -138,10 +152,10 @@ test('stop timer before onElapsed is finished should work', (done) => {
});
},
});
timer?.start();
timer.start();

setTimeout(() => {
timer?.stop();
timer.stop();
}, (interval + duration) * (count - 1) - duration / 2);

setTimeout(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const doTask = (task: Options['onElapsed']) => {

export default ({ interval, onElapsed }: Options) => {
if (interval <= 0) {
return null;
return {
start: () => {},
stop: () => {},
};
}

let timer: NodeJS.Timeout | null = null;
Expand Down