Created
January 11, 2026 08:41
-
-
Save Gwash3189/32dcffc86377962252be8fd54af1b690 to your computer and use it in GitHub Desktop.
Scheduler and Timer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { now, one, type SubTime } from './time' | |
| export class Scheduler { | |
| private timeouts: NodeJS.Timeout[] | |
| private lastRunTimestamp: number | |
| constructor(private ms: number) { | |
| this.timeouts = [] | |
| this.lastRunTimestamp = 0 | |
| } | |
| static every(ms: number | SubTime) { | |
| return new Scheduler(typeof ms === 'number' ? ms : ms.to('milliseconds')) | |
| } | |
| run(func: (now: Date) => void) { | |
| this.timeouts.push( | |
| setInterval(() => { | |
| const n = now() | |
| console.log( | |
| 'Scheduler triggered: ', | |
| 'last run: ', | |
| this.lastRunTimestamp, | |
| 'now: ', | |
| new Date(n), | |
| 'drift: ', | |
| n - this.lastRunTimestamp - this.ms | |
| ) | |
| this.lastRunTimestamp = Date.now() | |
| func(new Date(n)) | |
| }, this.ms) | |
| ) | |
| } | |
| stop() { | |
| this.timeouts.forEach((timeout) => clearInterval(timeout)) | |
| this.timeouts = [] | |
| } | |
| } | |
| // Scheduler.every(one.minute).run(() => console.log('asd')) | |
| // one.year.from(now()) | |
| // two.years.ago() | |
| // one.minute.plus(five.minutes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Time { | |
| constructor(private base: number) {} | |
| get milliseconds() { | |
| return new SubTime(this.base) | |
| } | |
| get millisecond() { | |
| return this.milliseconds | |
| } | |
| get seconds() { | |
| return new SubTime(this.base * 1000) | |
| } | |
| get second() { | |
| return this.seconds | |
| } | |
| get minutes() { | |
| return new SubTime(this.base * 1000 * 60) | |
| } | |
| get minute() { | |
| return this.minutes | |
| } | |
| get hours() { | |
| return new SubTime(this.base * 1000 * 60 * 60) | |
| } | |
| get hour() { | |
| return this.hours | |
| } | |
| get days() { | |
| return new SubTime(this.base * 1000 * 60 * 60 * 24) | |
| } | |
| get day() { | |
| return this.days | |
| } | |
| get years() { | |
| return new SubTime(this.base * 1000 * 60 * 60 * 24 * 365) | |
| } | |
| get year() { | |
| return this.years | |
| } | |
| } | |
| export class SubTime { | |
| constructor(private base: number) {} | |
| from(date: number | Date): Date { | |
| const time = date instanceof Date ? date.getTime() : date | |
| return new Date(time + this.base) | |
| } | |
| ago(): Date { | |
| return new Date(Date.now() - this.base) | |
| } | |
| plus(date: Date | number): Date { | |
| return this.from(date) | |
| } | |
| minus(date: Date | number): Date { | |
| return this.from(date) | |
| } | |
| to(stamp: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'years') { | |
| switch (stamp) { | |
| case 'milliseconds': | |
| return this.base | |
| case 'seconds': | |
| return this.base / 1000 | |
| case 'minutes': | |
| return this.base / (1000 * 60) | |
| case 'hours': | |
| return this.base / (1000 * 60 * 60) | |
| case 'days': | |
| return this.base / (1000 * 60 * 60 * 24) | |
| case 'years': | |
| return this.base / (1000 * 60 * 60 * 24 * 365) | |
| } | |
| } | |
| } | |
| export const now = () => Date.now() | |
| export const one = new Time(1) | |
| export const two = new Time(2) | |
| export const three = new Time(3) | |
| export const four = new Time(4) | |
| export const five = new Time(5) | |
| export const six = new Time(6) | |
| export const seven = new Time(7) | |
| export const eight = new Time(8) | |
| export const nine = new Time(9) | |
| export const ten = new Time(10) | |
| export const fifteen = new Time(15) | |
| export const twenty = new Time(20) | |
| export const twentyFive = new Time(25) | |
| export const thirty = new Time(30) | |
| export const thirtyFive = new Time(35) | |
| export const fourty = new Time(40) | |
| export const fourtyFive = new Time(45) | |
| export const fifty = new Time(50) | |
| export const fiftyFive = new Time(55) | |
| export const sixty = new Time(60) | |
| export const sixtyFive = new Time(65) | |
| export const seventy = new Time(70) | |
| export const seventyFive = new Time(75) | |
| export const eighty = new Time(80) | |
| export const eightyFive = new Time(85) | |
| export const ninety = new Time(90) | |
| export const ninetyFive = new Time(95) | |
| export const hundred = new Time(100) | |
| export const thousand = new Time(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment