version 1.1.1
This commit is contained in:
parent
1f46191482
commit
48440fccb0
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "schedule-later",
|
"name": "schedule-later",
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"types": "dist/Scheduler.d.ts",
|
"types": "dist/Scheduler.d.ts",
|
||||||
"main": "dist/Scheduler.js",
|
"main": "dist/Scheduler.js",
|
||||||
"author": "Nightness",
|
"author": "Nightness",
|
||||||
|
|
|
@ -35,6 +35,19 @@ describe('setTimeout Tests', () => {
|
||||||
jest.advanceTimersByTime(TimeInMS.SECOND)
|
jest.advanceTimersByTime(TimeInMS.SECOND)
|
||||||
expect(fn).not.toBeCalled()
|
expect(fn).not.toBeCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('startTimeout, should still call the function if stop function is called and canceled', () => {
|
||||||
|
const fn = jest.fn()
|
||||||
|
const stop = startTimeout(fn, { ms: TimeInMS.SECOND })
|
||||||
|
|
||||||
|
expect(fn).not.toBeCalled()
|
||||||
|
const stopCancel = stop({
|
||||||
|
ms: TimeInMS.SECOND * 5,
|
||||||
|
})!
|
||||||
|
jest.advanceTimersByTime(TimeInMS.SECOND)
|
||||||
|
stopCancel()
|
||||||
|
expect(fn).toBeCalled()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('setInterval Tests', () => {
|
describe('setInterval Tests', () => {
|
||||||
|
|
|
@ -22,7 +22,7 @@ export type TimeUntil = {
|
||||||
ms?: number
|
ms?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type StopCancelFunction = (stopRunning: boolean) => void
|
export type StopCancelFunction = (stopRunning?: boolean) => void
|
||||||
|
|
||||||
export type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null
|
export type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null
|
||||||
|
|
||||||
|
@ -84,29 +84,25 @@ export function startTimeout(
|
||||||
if (timeout) clearTimeout(timeout)
|
if (timeout) clearTimeout(timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop() function to stop the interval and timeout
|
/**
|
||||||
// stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds
|
* Stops a timeout
|
||||||
// stop(stopHour, stopMinute) will stop the interval and timeout at the next stopHour:stopMinute
|
* @param {TimeUntil} stopTime - optional
|
||||||
|
* @return {StopCancelFunction}
|
||||||
|
*/
|
||||||
const stop = (stopTime?: TimeUntil): StopCancelFunction | null => {
|
const stop = (stopTime?: TimeUntil): StopCancelFunction | null => {
|
||||||
if (stopTime === undefined) {
|
if (!stopTime) {
|
||||||
stopNow()
|
stopNow()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((stopTime as any) instanceof Date) {
|
|
||||||
// @ts-ignore - TS doesn't know this is allowed
|
|
||||||
const timeFromNow = stopTime - new Date()
|
|
||||||
const stopTimeout = setTimeout(stopNow, timeFromNow)
|
|
||||||
// stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true)
|
|
||||||
return (stopRunning: boolean = false) => {
|
|
||||||
clearTimeout(stopTimeout)
|
|
||||||
if (stopRunning) stopNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const stopTimeout = setTimeout(stopNow, 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) => {
|
/**
|
||||||
|
* Cancels a delayed stop
|
||||||
|
* @param {boolean} stopRunning - optional
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
return (stopRunning?: boolean) => {
|
||||||
clearTimeout(stopTimeout)
|
clearTimeout(stopTimeout)
|
||||||
if (stopRunning) stopNow()
|
if (stopRunning) stopNow()
|
||||||
}
|
}
|
||||||
|
@ -120,7 +116,7 @@ export function startTimeout(
|
||||||
* Start an interval
|
* Start an interval
|
||||||
* @param {Function} intervalFunc
|
* @param {Function} intervalFunc
|
||||||
* @param {number} intervalMS
|
* @param {number} intervalMS
|
||||||
* @param {TimeUntil} start
|
* @param {TimeUntil} start - optional
|
||||||
* @return {StopFunction}
|
* @return {StopFunction}
|
||||||
*/
|
*/
|
||||||
export function startInterval(
|
export function startInterval(
|
||||||
|
@ -146,29 +142,25 @@ export function startInterval(
|
||||||
if (interval) clearInterval(interval)
|
if (interval) clearInterval(interval)
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop() function to stop the interval and timeout
|
/**
|
||||||
// stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds
|
* Stops an interval
|
||||||
// stop(stopHour, stopMinute) will stop the interval and timeout at the next stopHour:stopMinute
|
* @param {TimeUntil} stopTime - optional
|
||||||
|
* @return {StopCancelFunction}
|
||||||
|
*/
|
||||||
const stop = (stopTime?: TimeUntil): StopCancelFunction | null => {
|
const stop = (stopTime?: TimeUntil): StopCancelFunction | null => {
|
||||||
if (stopTime === undefined) {
|
if (stopTime === undefined) {
|
||||||
stopNow()
|
stopNow()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((stopTime as any) instanceof Date) {
|
|
||||||
// @ts-ignore - TS doesn't know this is allowed
|
|
||||||
const timeFromNow = stopTime - new Date()
|
|
||||||
const stopTimeout = setTimeout(stopNow, timeFromNow)
|
|
||||||
// stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true)
|
|
||||||
return (stopRunning: boolean = false) => {
|
|
||||||
clearTimeout(stopTimeout)
|
|
||||||
if (stopRunning) stopNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const stopTimeout = setTimeout(stopNow, 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) => {
|
/**
|
||||||
|
* Cancels a delayed stop
|
||||||
|
* @param {boolean} stopRunning - optional
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
return (stopRunning?: boolean) => {
|
||||||
clearTimeout(stopTimeout)
|
clearTimeout(stopTimeout)
|
||||||
if (stopRunning) stopNow()
|
if (stopRunning) stopNow()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue