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

use-worker-timer

v1.0.15

Published

React utility for running a timer in a WebWorker, reports checkpoints when they have been reached. Includes basic playback control.

Downloads

15

Readme

Stargazers
Issues
MIT License

About The Project

Scheduling events in the browser can be challenging. Timeouts you set may be delayed by other tasks running on the main thread. Switching tabs or resizing the window can further impact accuracy, as the main thread gets throttled by the browser. This becomes especially noticeable when events are scheduled to fire in sequence, as the delay will accumulate.

In contrast, web workers run on a separate thread in the background, independent of intensive computations on the main thread, and are not throttled by the browser. However, they are notoriously challenging to use since the only way to communicate with them is through messages sent between the threads.

This package aims to address this issue by providing a convenient React hook for scheduling events in a web worker, while abstracting away some of the complexities of working with them directly.

To use the hook, you define a list of checkpoints and a callback that is called whenever a checkpoint is reached. It returns a set of functions to control the playback of these checkpoints, including playing, pausing, navigating to a specific point in the playback, and looping. Additionally, it provides the current state of the playback and an estimated progress based on the elapsed time since the last reported checkpoint.

Built With

Deno React

Getting Started

Prerequisites

  • React
  • npm or deno

Installation

Deno

import { usePlayback } from "https://deno.land/x/use_worker_timer/index.ts"

NPM

npm install use-worker-timer
import { usePlayback } from "use-worker-timer"

Usage

To use this hook, you pass in a list of checkpoints, as ms values, and a callback that is called whenever a checkpoint is reached. The hook will return a bunch of functions to control the playback, as well as the current state of the playback.

// Creates a list of 64 events, with a spacing of 60 bpm
const BPM = 60
const checkpoints = Array.from({ length: 4 * 16 }).map((_, i) => (i * (60 * 1000)) / BPM)

const callbackForTime = (ms: number) => {
  console.log(`Reached checkpoint ${reportedTime}`)
  if(i === events.length - 1){
    console.log("Reached the end!")
  }
}

... 

const {
  isReady, // Wether the worker is ready to start playing
  playState, // The state of the playback as reported by the worker
  estimatedProgress, // The estimated progress, based on the last reported checkpoint
  lagLog, // A log of the inaccuracy of the timer
  play,
  pause,
  stop,
  setLooping,
  setPlaybackProgress,
} = usePlayback({
  reportCheckpoint: callbackForTime,
  checkpoints: checkpoints,
  estimationUpdateInterval: 100, // How often to update the estimated progress
})
Arguments
  • reportCheckpoint is called whenever a checkpoint is reached.
  • checkpoints is a list of ms values, at which the worker will fire the reportCheckpoint callback.
  • estimationUpdateInterval is how often the estimated progress will be updated. Defaults to never (always same as playState.progress). If omitted, the estimated progress will be the same as in playState.
Return values
  • isReady is a boolean that is true when the worker is ready to start playing.
  • playState will update whenever a checkpoint is reached or a playback function is called. It is an object with the following properties:
    • playing is a boolean that is true when the worker is playing.
    • progress is the progress of the playback, in ms.
    • looping is a boolean that is true when the worker is looping.
  • estimatedProgress will update every estimationUpdateInterval milliseconds, and is based on the last reported checkpoint. As this is calculated on the main thread, it might be slightly off. I recommend using it for UI, but use the reported checkpoints for logic.
  • lagLog is an array of numbers, that represent the inaccuracy of the timer. It will be filled with the inaccuracy per checkpoint, in ms, whenever a checkpoint is reached.
  • play, pause, stop, setLooping and setPlaybackProgress are functions that control the playback. Self explanatory.

Take a look at the full example (source).

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

PetrosiliusPatter - [email protected]

Project Link: https://github.com/PetrosiliusPatter/use-worker-timer