version 1.1.0

This commit is contained in:
Josh Guyette 2023-07-17 19:45:31 -05:00
parent 617fdebee3
commit 1f46191482
4 changed files with 186 additions and 161 deletions

View File

@ -1,6 +1,6 @@
# Schedule Later
schedule-later is a static class that provides methods for managing date-time based tasks, such as starting timeouts and intervals at a specific time of day. Under-the-hood, it uses `setTimeout` and `setInterval`.
schedule-later provides methods for managing date-time based tasks, such as starting timeouts and intervals at a specific time of day. Under-the-hood, it uses `setTimeout` and `setInterval`.
## Install
@ -17,7 +17,7 @@ yarn add schedule-later
## Import
```typescript
import { Scheduler, TimeInMS } from 'schedule-later'
import { startTimeout, startInterval, TimeInMS } from 'schedule-later'
```
## Key Concepts
@ -55,10 +55,7 @@ The `Scheduler` class provides two main static methods: `startTimeout` and `star
The `startTimeout` method starts a timeout that calls a given function after a specific delay. The delay is calculated based on the `TimeUntil` object passed to it. The method returns a `StopFunction` (see below).
```typescript
public static startTimeout(
timerFunc: Function,
start: TimeUntil
): StopFunction;
function startTimeout(timerFunc: Function, start: TimeUntil): StopFunction
```
### startInterval
@ -66,11 +63,11 @@ public static startTimeout(
The `startInterval` method starts an interval that calls a given function repeatedly with a fixed time delay between each call. Like `startTimeout`, the initial delay is calculated based on a `TimeUntil` object. The method returns a `StopFunction` (see below).
```typescript
public static startInterval(
function startInterval(
intervalFunc: Function,
intervalMS: number,
start?: TimeUntil
): StopFunction;
): StopFunction
```
## Stop Functions

View File

@ -1,6 +1,6 @@
{
"name": "schedule-later",
"version": "1.0.6",
"version": "1.1.0",
"types": "dist/Scheduler.d.ts",
"main": "dist/Scheduler.js",
"author": "Nightness",
@ -18,6 +18,6 @@
"@types/jest": "^29.5.3",
"@types/node": "^18.11.18",
"jest": "^29.6.1",
"typescript": "^4.6.4"
"typescript": "^5.1.6"
}
}

View File

@ -1,6 +1,6 @@
import { Scheduler } from './Scheduler'
import { startTimeout, startInterval, TimeInMS } from './Scheduler'
describe('Scheduler', () => {
describe('setTimeout Tests', () => {
beforeEach(() => {
jest.useFakeTimers()
})
@ -9,10 +9,10 @@ describe('Scheduler', () => {
jest.clearAllTimers()
})
test('startTimeout should correctly start and stop timeout', () => {
test('startTimeout, should start and stop properly', () => {
const callback = jest.fn()
const stop = Scheduler.startTimeout(callback, { ms: 5000 })
const stop = startTimeout(callback, { ms: 5000 })
// Advance timers by less than the delay and check if callback has not been called
jest.advanceTimersByTime(2000)
@ -26,10 +26,30 @@ describe('Scheduler', () => {
stop()
})
test('startInterval should correctly start and stop interval', () => {
test('startTimeout, should not call the function if stop function is called', () => {
const fn = jest.fn()
const stop = startTimeout(fn, { ms: TimeInMS.SECOND })
expect(fn).not.toBeCalled()
stop()
jest.advanceTimersByTime(TimeInMS.SECOND)
expect(fn).not.toBeCalled()
})
})
describe('setInterval Tests', () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.clearAllTimers()
})
test('startInterval, should correctly start and repeat interval', () => {
const callback = jest.fn()
const stop = Scheduler.startInterval(callback, 2000, { ms: 5000 })
const stop = startInterval(callback, 2000, { ms: 5000 })
// Advance timers by less than the initial delay and check if callback has not been called
jest.advanceTimersByTime(2000)
@ -46,4 +66,14 @@ describe('Scheduler', () => {
// Cleanup
stop()
})
test('startInterval, should not call the function if stop function is called', () => {
const fn = jest.fn()
const stop = startInterval(fn, 2000, { ms: TimeInMS.SECOND })
expect(fn).not.toBeCalled()
stop()
jest.advanceTimersByTime(TimeInMS.SECOND)
expect(fn).not.toBeCalled()
})
})

View File

@ -26,8 +26,7 @@ export type StopCancelFunction = (stopRunning: boolean) => void
export type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null
export class Scheduler {
private static timeUntil(start: TimeUntil) {
function timeUntil(start: TimeUntil) {
if (start.ms) {
return start.ms
}
@ -66,11 +65,11 @@ export class Scheduler {
* @param {TimeUntil} start
* @return {StopFunction}
*/
public static startTimeout(
export function startTimeout(
timerFunc: Function,
start: TimeUntil
): StopFunction {
let delay = this.timeUntil(start)
let delay = timeUntil(start)
// set a timeout to start the interval at the target time
let timeout: NodeJS.Timeout | null = null
@ -105,7 +104,7 @@ export class Scheduler {
}
}
const stopTimeout = setTimeout(stopNow, this.timeUntil(stopTime))
const stopTimeout = setTimeout(stopNow, timeUntil(stopTime))
// stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true)
return (stopRunning: boolean = false) => {
clearTimeout(stopTimeout)
@ -124,12 +123,12 @@ export class Scheduler {
* @param {TimeUntil} start
* @return {StopFunction}
*/
public static startInterval(
export function startInterval(
intervalFunc: Function,
intervalMS: number,
start?: TimeUntil
): StopFunction {
let delay = start ? this.timeUntil(start) : 0
let delay = start ? timeUntil(start) : 0
// set a timeout to start the interval at the target time
let interval: number | null = null
@ -167,7 +166,7 @@ export class Scheduler {
}
}
const stopTimeout = setTimeout(stopNow, this.timeUntil(stopTime))
const stopTimeout = setTimeout(stopNow, timeUntil(stopTime))
// stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true)
return (stopRunning: boolean = false) => {
clearTimeout(stopTimeout)
@ -178,4 +177,3 @@ export class Scheduler {
// return a cleanup function
return stop
}
}