version 1.0.5
This commit is contained in:
parent
98e9a6ed68
commit
f6a78b99fe
|
@ -17,7 +17,7 @@ yarn add schedule-later
|
||||||
## Import
|
## Import
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import Scheduler from 'schedule-later'
|
import { Scheduler, TimeInMS } from 'schedule-later'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Key Concepts
|
## 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.
|
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
|
```typescript
|
||||||
const sayHello = () => console.log('Hello, world!')
|
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
|
// Later, if you want to stop the interval
|
||||||
stopInterval()
|
stopInterval()
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
{
|
{
|
||||||
"name": "schedule-later",
|
"name": "schedule-later",
|
||||||
"version": "1.0.1",
|
"version": "1.0.5",
|
||||||
|
"types": "dist/Scheduler.d.ts",
|
||||||
"main": "dist/Scheduler.js",
|
"main": "dist/Scheduler.js",
|
||||||
"author": "Nightness",
|
"author": "Nightness",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
|
"pretest": "tsc",
|
||||||
"test": "./node_modules/.bin/jest --verbose",
|
"test": "./node_modules/.bin/jest --verbose",
|
||||||
"watch": "./node_modules/.bin/jest --watch"
|
"prepublish": "tsc && npm run test"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.3",
|
"@types/jest": "^29.5.3",
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import Scheduler from './Scheduler'
|
import { Scheduler } from './Scheduler'
|
||||||
|
|
||||||
describe('Scheduler', () => {
|
describe('Scheduler', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
|
@ -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 {
|
export interface TimeOfDay {
|
||||||
hour: number
|
hour: number
|
||||||
minute?: number
|
minute?: number
|
||||||
seconds?: number
|
seconds?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TimeUntil = {
|
export type TimeUntil = {
|
||||||
timeOfDay?: TimeOfDay
|
timeOfDay?: TimeOfDay
|
||||||
date?: Date
|
date?: Date
|
||||||
ms?: number
|
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) {
|
private static timeUntil(start: TimeUntil) {
|
||||||
if (start.ms) {
|
if (start.ms) {
|
||||||
return start.ms
|
return start.ms
|
||||||
|
@ -21,17 +36,19 @@ export default class Scheduler {
|
||||||
let now = new Date()
|
let now = new Date()
|
||||||
|
|
||||||
// set the target time
|
// set the target time
|
||||||
let targetTime = !start.timeOfDay
|
let targetTime = (
|
||||||
? start.date
|
!start.timeOfDay
|
||||||
: new Date(
|
? start.date
|
||||||
now.getFullYear(),
|
: new Date(
|
||||||
now.getMonth(),
|
now.getFullYear(),
|
||||||
now.getDate(),
|
now.getMonth(),
|
||||||
start.timeOfDay.hour,
|
now.getDate(),
|
||||||
start.timeOfDay.minute ?? 0,
|
start.timeOfDay.hour,
|
||||||
start.timeOfDay.seconds ?? 0,
|
start.timeOfDay.minute ?? 0,
|
||||||
0
|
start.timeOfDay.seconds ?? 0,
|
||||||
)
|
0
|
||||||
|
)
|
||||||
|
)!
|
||||||
|
|
||||||
// if the target time has already passed today, set it for tomorrow
|
// if the target time has already passed today, set it for tomorrow
|
||||||
if (start.timeOfDay && now > targetTime) {
|
if (start.timeOfDay && now > targetTime) {
|
||||||
|
@ -58,7 +75,7 @@ export default class Scheduler {
|
||||||
let delay = this.timeUntil(start)
|
let delay = this.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
|
let timeout: NodeJS.Timeout | null = null
|
||||||
timeout = setTimeout(function () {
|
timeout = setTimeout(function () {
|
||||||
// Clear the timeout variable
|
// Clear the timeout variable
|
||||||
timeout = null
|
timeout = null
|
||||||
|
@ -73,7 +90,7 @@ export default class Scheduler {
|
||||||
// stop() function to stop the interval and timeout
|
// stop() function to stop the interval and timeout
|
||||||
// stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds
|
// 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
|
// 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) {
|
if (stopTime === undefined) {
|
||||||
stopNow()
|
stopNow()
|
||||||
return null
|
return null
|
||||||
|
@ -114,11 +131,11 @@ export default class Scheduler {
|
||||||
intervalMS: number,
|
intervalMS: number,
|
||||||
start?: TimeUntil
|
start?: TimeUntil
|
||||||
): StopFunction {
|
): StopFunction {
|
||||||
let delay = this.timeUntil(start)
|
let delay = start ? this.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
|
let interval: number | null = null
|
||||||
let timeout = setTimeout(function () {
|
let timeout: NodeJS.Timeout | null = setTimeout(function () {
|
||||||
// Clear the timeout variable
|
// Clear the timeout variable
|
||||||
timeout = null
|
timeout = null
|
||||||
// start the interval
|
// start the interval
|
||||||
|
@ -135,7 +152,7 @@ export default class Scheduler {
|
||||||
// stop() function to stop the interval and timeout
|
// stop() function to stop the interval and timeout
|
||||||
// stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds
|
// 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
|
// 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) {
|
if (stopTime === undefined) {
|
||||||
stopNow()
|
stopNow()
|
||||||
return null
|
return null
|
||||||
|
|
|
@ -3,12 +3,11 @@
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"declarationDir": "dist",
|
"declarationDir": "dist",
|
||||||
"target": "es2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
"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"],
|
"rootDirs": ["./src"],
|
||||||
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
|
"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. */,
|
"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. */,
|
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
||||||
"strict": false /* Enable all strict type-checking options. */,
|
"strict": true /* Enable all strict type-checking options. */
|
||||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue