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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@randajan/treelock

v0.3.1

Published

A minimal async lock with timeout support and parent-child queueing.

Readme

@randajan/treelock

NPM JavaScript Style Guide

TreeLock is a minimalist and deterministic locking mechanism for JavaScript that organizes asynchronous operations into a hierarchical, tree-like structure. Each lock node cooperates with its root and its branches to ensure that tasks are executed in the correct order — peacefully, predictably, and without deadlocks.

“I shall wait until I am calm, my root is calm, and all my branches are calm.”
— TreeLock, probably

Why TreeLock?

Because sometimes, simple promises and semaphores aren't enough. You need:

  • 🧠 Smart queueing based on hierarchy
  • 🌿 Elegant dependency propagation
  • 🔒 Synchronized task execution across nested contexts
  • ☯️ Inner peace in your asynchronous flows

Features

  • Deterministic task order based on time of registration
  • Propagated locking from root to branches
  • Automatically delays conflicting operations
  • Lightweight and dependency-free (except for a @randajan/sleep helper)
  • Perfect for nested resource management or transactional consistency

Installation

npm install @your-scope/TreeLock

Usage

import { TreeLock } from "@your-scope/TreeLock";

const root = new TreeLock();
const A = root.sub();
const B = root.sub();

await Promise.all([
    root.run(() => sleep(100)),
    A.run(() => sleep(100)),
    B.run(() => sleep(100))
]);

This guarantees that:

  1. Tasks run in order of registration
  2. No two conflicting branches will run simultaneously
  3. Root tasks block all branches, but branches can run in parallel if root is free

Options

You can pass the following options to the TreeLock constructor or the .sub() method:

  • name (string, optional): Just a label, useful for logging/debugging.
  • ttl (number, optional): Timeout in milliseconds for each task. Tasks exceeding this limit are cancelled.
  • on (function(lock, status, result), optional): A callback for each lock event: enter, start, done, timeout, error.
  • sup (TreeLock, optional): Used to attach a children to a parent (sub to sup)

Properties

Each TreeLock instance exposes the following properties:

  • name: The name of this lock.
  • sup: The parent TreeLock instance (if any).
  • subs: Array of child TreeLock instances.
  • ram: Number of currently running tasks in this lock.
  • ramSup: Number of currently running tasks in all parent locks.
  • ramSub: Number of currently running tasks in all child locks.
  • queue: A Promise that resolves once all currently enqueued tasks are done.

API

run(fn, ttl?, ...args): Promise<void>

Runs a task within the lock. Waits for its turn based on the lock tree structure.

  • fn: Function to execute.
  • ttl: Optional timeout in milliseconds.
  • ...args: Arguments to pass to fn.

Returns a promise that resolves when the task finishes or rejects on timeout/error.

wrap(fn, ttl?): (...args) => Promise<void>

Wraps a function with the lock logic. Useful for passing locked functions around.

  • fn: Function to wrap.
  • ttl: Optional timeout in milliseconds.

Returns a new function that automatically runs inside the lock.

sub(options?): TreeLock

Creates a child TreeLock bound to this one. Main benefits:

  1. Any task scheduled on this child will wait for all parent locks to be free.
  2. Any task scheduled at parent will also lock it's subtree
  • options: Same options as constructor (except sup, which is inherited).

Returns a new TreeLock instance.

License

MIT