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
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 ## Install
@ -17,7 +17,7 @@ yarn add schedule-later
## Import ## Import
```typescript ```typescript
import { Scheduler, TimeInMS } from 'schedule-later' import { startTimeout, startInterval, TimeInMS } from 'schedule-later'
``` ```
## Key Concepts ## 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). 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 ```typescript
public static startTimeout( function startTimeout(timerFunc: Function, start: TimeUntil): StopFunction
timerFunc: Function,
start: TimeUntil
): StopFunction;
``` ```
### startInterval ### 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). 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 ```typescript
public static startInterval( function startInterval(
intervalFunc: Function, intervalFunc: Function,
intervalMS: number, intervalMS: number,
start?: TimeUntil start?: TimeUntil
): StopFunction; ): StopFunction
``` ```
## Stop Functions ## Stop Functions

View File

@ -1,6 +1,6 @@
{ {
"name": "schedule-later", "name": "schedule-later",
"version": "1.0.6", "version": "1.1.0",
"types": "dist/Scheduler.d.ts", "types": "dist/Scheduler.d.ts",
"main": "dist/Scheduler.js", "main": "dist/Scheduler.js",
"author": "Nightness", "author": "Nightness",
@ -18,6 +18,6 @@
"@types/jest": "^29.5.3", "@types/jest": "^29.5.3",
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"jest": "^29.6.1", "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(() => { beforeEach(() => {
jest.useFakeTimers() jest.useFakeTimers()
}) })
@ -9,10 +9,10 @@ describe('Scheduler', () => {
jest.clearAllTimers() jest.clearAllTimers()
}) })
test('startTimeout should correctly start and stop timeout', () => { test('startTimeout, should start and stop properly', () => {
const callback = jest.fn() 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 // Advance timers by less than the delay and check if callback has not been called
jest.advanceTimersByTime(2000) jest.advanceTimersByTime(2000)
@ -26,10 +26,30 @@ describe('Scheduler', () => {
stop() 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 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 // Advance timers by less than the initial delay and check if callback has not been called
jest.advanceTimersByTime(2000) jest.advanceTimersByTime(2000)
@ -46,4 +66,14 @@ describe('Scheduler', () => {
// Cleanup // Cleanup
stop() 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 type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null
export class Scheduler { function timeUntil(start: TimeUntil) {
private static timeUntil(start: TimeUntil) {
if (start.ms) { if (start.ms) {
return start.ms return start.ms
} }
@ -58,19 +57,19 @@ export class Scheduler {
let delay = targetTime - now let delay = targetTime - now
return delay return delay
} }
/** /**
* Start a timeout * Start a timeout
* @param {Function} timerFunc * @param {Function} timerFunc
* @param {TimeUntil} start * @param {TimeUntil} start
* @return {StopFunction} * @return {StopFunction}
*/ */
public static startTimeout( export function startTimeout(
timerFunc: Function, timerFunc: Function,
start: TimeUntil start: TimeUntil
): StopFunction { ): StopFunction {
let delay = this.timeUntil(start) let delay = timeUntil(start)
// set a timeout to start the interval at the target time // set a timeout to start the interval at the target time
let timeout: NodeJS.Timeout | null = null 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) // stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true)
return (stopRunning: boolean = false) => { return (stopRunning: boolean = false) => {
clearTimeout(stopTimeout) clearTimeout(stopTimeout)
@ -115,21 +114,21 @@ export class Scheduler {
// return a cleanup function // return a cleanup function
return stop return stop
} }
/** /**
* Start an interval * Start an interval
* @param {Function} intervalFunc * @param {Function} intervalFunc
* @param {number} intervalMS * @param {number} intervalMS
* @param {TimeUntil} start * @param {TimeUntil} start
* @return {StopFunction} * @return {StopFunction}
*/ */
public static startInterval( export function startInterval(
intervalFunc: Function, intervalFunc: Function,
intervalMS: number, intervalMS: number,
start?: TimeUntil start?: TimeUntil
): StopFunction { ): 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 // set a timeout to start the interval at the target time
let interval: number | null = null 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) // stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true)
return (stopRunning: boolean = false) => { return (stopRunning: boolean = false) => {
clearTimeout(stopTimeout) clearTimeout(stopTimeout)
@ -177,5 +176,4 @@ export class Scheduler {
// return a cleanup function // return a cleanup function
return stop return stop
}
} }