npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mockable-timer

v1.0.2

Published

Service that wraps Date.now(), setTimeout and setInterval for mockability

Downloads

55

Readme

Mockable Timer

A thin wrapper over Date.now(), setTimeout and setInterval, along with an elaborate mock

Designed for dependency injection, and for wrapping further to support any other interface

NOT designed for replacing the original Date.now(), setTimeout and setInterval for testing. It will probably work but it isn't an intended use case. Unit testing implies dependency injection

Usage

Client

Import the default implementation which depends on Date.now(), setTimeout, setInterval, clearTimeout and clearInterval

import { time } from 'mockable-timer'

Get the exact UNIX timestamp in seconds. It's not in milliseconds because most platforms zero out the two last digits anyway to prevent cache timing attacks so the real part is inaccurate anyway, and this way durations within human perception range are easier to read and compare.

The part after the decimal point still contains the 100s part of Date.now(), if you want to use it for benchmarking.

time.now()

Set a timeout. The time is in seconds for consistency. wait returns a function, calling this function cancels the timeout.

const cancel = time.wait(60, () => alert('Are you still there?'))
cancel()

Set an interval. The third parameter is a boolean that defaults to false.

time.wait(60, () => alert('Another minute passed'), true)

Mock

The time manager defines the following methods:

import { mockTime } from 'mockable-timer'

const [time, timeManager] = mockTime()

export type ScheduleEntry = [number, () => void]
export interface TimeManager {
    progress(s: number): Promise<void>
    progressTo(s: number): Promise<void>
    flushMtq(): Promise<void>
    runAll(limit?: number): Promise<void>
    getNext(): ScheduleEntry | undefined
    getQueue(): ScheduleEntry[]
}
  • progress: Moves time forward by the specified number of seconds
  • progressTo: Moves time to the specified point, throws if the point is in the psat
  • flushMtq: Flush all microtasks using setTimeout with a duration of 0. This is done after every enqueued timer by the mock
  • runAll: Run the timer until the queue is empty. Throws if there are still more after 10K executed timers. Note that this can get stuck on intervals
  • getNext: Return a tuple of the next task and its due timestamp. Returns undefined if the queue is empty
  • getQueue: Return an array of all enqueued tasks with their due timestamps

Todo

  • Make runAll halt if there are only repeating timers left