version 1.0.5

This commit is contained in:
Josh Guyette 2023-07-17 17:57:54 -05:00
parent 98e9a6ed68
commit f6a78b99fe
5 changed files with 47 additions and 29 deletions

View File

@ -17,7 +17,7 @@ yarn add schedule-later
## Import
```typescript
import Scheduler from 'schedule-later'
import { Scheduler, TimeInMS } from 'schedule-later'
```
## Key Concepts
@ -122,11 +122,11 @@ In the `StopCancelFunction`, if the `stopRunning` parameter is `true`, it stops
In this example, the goodMorning function will be called at 7:00 AM. If you want to cancel the morning greeting (for example, the user chose to sleep in), you can call the stopTimeout function.
 
3. Using startInterval with a specific interval, basically a regular setInterval
3. Using startInterval with a specific interval, basically a regular setInterval. Uses the TimeInMS enum to clearly specify the interval.
```typescript
const sayHello = () => console.log('Hello, world!')
let stopInterval = Scheduler.startInterval(sayHello, 1000)
let stopInterval = Scheduler.startInterval(sayHello, TimeInMS.SECOND * 5)
// Later, if you want to stop the interval
stopInterval()

View File

@ -1,12 +1,14 @@
{
"name": "schedule-later",
"version": "1.0.1",
"version": "1.0.5",
"types": "dist/Scheduler.d.ts",
"main": "dist/Scheduler.js",
"author": "Nightness",
"scripts": {
"build": "tsc",
"pretest": "tsc",
"test": "./node_modules/.bin/jest --verbose",
"watch": "./node_modules/.bin/jest --watch"
"prepublish": "tsc && npm run test"
},
"devDependencies": {
"@types/jest": "^29.5.3",

View File

@ -1,4 +1,4 @@
import Scheduler from './Scheduler'
import { Scheduler } from './Scheduler'
describe('Scheduler', () => {
beforeEach(() => {

View File

@ -1,17 +1,32 @@
export enum TimeInMS {
SECOND = 1000,
MINUTE = 60000,
HALF_HOUR = 1800000,
HOUR = 3600000,
HALF_DAY = 43200000,
DAY = 86400000,
WEEK = 604800000,
FOUR_SCORE = 1209600000, // 14 days
MONTH = 2592000000,
}
export interface TimeOfDay {
hour: number
minute?: number
seconds?: number
}
export type TimeUntil = {
timeOfDay?: TimeOfDay
date?: Date
ms?: number
}
type StopCancelFunction = (stopRunning: boolean) => void
type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null
export default class Scheduler {
export type StopCancelFunction = (stopRunning: boolean) => void
export type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null
export class Scheduler {
private static timeUntil(start: TimeUntil) {
if (start.ms) {
return start.ms
@ -21,17 +36,19 @@ export default class Scheduler {
let now = new Date()
// set the target time
let targetTime = !start.timeOfDay
? start.date
: new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
start.timeOfDay.hour,
start.timeOfDay.minute ?? 0,
start.timeOfDay.seconds ?? 0,
0
)
let targetTime = (
!start.timeOfDay
? start.date
: new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
start.timeOfDay.hour,
start.timeOfDay.minute ?? 0,
start.timeOfDay.seconds ?? 0,
0
)
)!
// if the target time has already passed today, set it for tomorrow
if (start.timeOfDay && now > targetTime) {
@ -58,7 +75,7 @@ export default class Scheduler {
let delay = this.timeUntil(start)
// set a timeout to start the interval at the target time
let timeout: NodeJS.Timeout = null
let timeout: NodeJS.Timeout | null = null
timeout = setTimeout(function () {
// Clear the timeout variable
timeout = null
@ -73,7 +90,7 @@ export default class Scheduler {
// stop() function to stop the interval and timeout
// stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds
// stop(stopHour, stopMinute) will stop the interval and timeout at the next stopHour:stopMinute
const stop = (stopTime?: TimeUntil): StopCancelFunction => {
const stop = (stopTime?: TimeUntil): StopCancelFunction | null => {
if (stopTime === undefined) {
stopNow()
return null
@ -114,11 +131,11 @@ export default class Scheduler {
intervalMS: number,
start?: TimeUntil
): StopFunction {
let delay = this.timeUntil(start)
let delay = start ? this.timeUntil(start) : 0
// set a timeout to start the interval at the target time
let interval: number = null
let timeout = setTimeout(function () {
let interval: number | null = null
let timeout: NodeJS.Timeout | null = setTimeout(function () {
// Clear the timeout variable
timeout = null
// start the interval
@ -135,7 +152,7 @@ export default class Scheduler {
// stop() function to stop the interval and timeout
// stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds
// stop(stopHour, stopMinute) will stop the interval and timeout at the next stopHour:stopMinute
const stop = (stopTime?: TimeUntil): StopCancelFunction => {
const stop = (stopTime?: TimeUntil): StopCancelFunction | null => {
if (stopTime === undefined) {
stopNow()
return null

View File

@ -3,12 +3,11 @@
"declaration": true,
"declarationDir": "dist",
"target": "es2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "nodenext" /* Specify what module code is generated. */,
"module": "CommonJS" /* Specify what module code is generated. */,
"rootDirs": ["./src"],
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": false /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"strict": true /* Enable all strict type-checking options. */
}
}