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

semafy

v2.2.0

Published

A robust cross-agent synchronization library.

Downloads

15

Readme

Semafy

Semafy provides synchronization and concurrency management across execution agents (main thread, web workers). It contains a robust set of tools modeled after C++ synchronization primitives, offering control and flexibility for managing shared resources and states.

Version Maintenance License codecov npm bundle size

Features

  • Mutexes: Exclusive and shared locking to protect data from concurrent access.
  • Semaphores: Control access to a finite number of resources.
  • ConditionVariable: Allows agents to wait for certain conditions to occur.
  • Barriers: Ensures agents reach a certain point before any can proceed.
  • Utilities: Manage async code with lock, tryLock, lockGuard, and callOnce.
  • Error Handling: Specific error classes to enhance debuggability and reliability.
  • Platform Agnostic: Works in any browser or server-side environment that supports SharedArrayBuffer.

Installation

NPM:

npm install semafy

Yarn:

yarn add semafy

JSR:

jsr add @rojas/semafy

API

Generics

  • [Sync]BasicLockable: A base interface that provides exclusive blocking for agents.

  • [Sync]Lockable: Extends BasicLockable to include attempted locking.

  • SharedLockable: Provides shared blocking semantics for agents.

  • SharedResource: Represents a shared resource that is backed by a SharedArrayBuffer.

  • SharedTimedLockable: Extends SharedLockable to include timed blocking.

  • [Sync]TimedLockable: Extends Lockable to include timed blocking.

Mutexes

  • Mutex: Provides essential mutex operations including lock, unlock, and tryLock.

  • TimedMutex: A timed variant that supports timed operations including tryLockFor and tryLockUntil.

  • RecursiveMutex: Allows multiple locks from the same agent.

  • RecursiveTimedMutex: A timed variant that supports timed operations.

  • SharedMutex: Allows multiple readers or exclusive writer access, facilitating reader-writer scenarios.

  • SharedTimedMutex: A timed variant that supports timed operations.

Mutex Management

  • lock(): Sequentially acquires the given locks. If any lock fails, the process is stopped, and any acquired locks are released in reverse order.

  • lockGuard[Sync](): Locks a mutex before calling a callback function, ensuring the mutex is unlocked afterwards.

  • MultiLock: Wraps multiple BasicLockable objects to create a multi-lock. Calls to lock, unlock, etc will acquire / release locks on all of the wrapped objects.

  • SharedLock: Wraps a SharedLockable object (e.g. SharedMutex) to create a shared lock. Calls to lock, unlock, etc will acquire / release a shared lock instead of an exclusive lock.

  • tryLock(): Tries to sequentially acquire the given locks. If any lock fails, the process is stopped, and any acquired locks are released in reverse order.

  • UniqueLock: Wraps a BasicLockable object to create a unique lock. Calls to lock, unlock, etc will acquire / release a lock on the wrapped object.

Call Once

  • callOnce(): Executes a callback function at most once, based on the state of a provided OnceFlag.

  • OnceFlag: Represents a flag that can be set exactly once, shared across different execution agents.

Condition Variable

  • ConditionVariable: Allows agents to wait for specific conditions, tightly integrated with mutexes for state management.

Semaphores

  • CountingSemaphore: Manages access to a finite number of resources, allowing multiple entities to hold the semaphore concurrently up to a maximum count.

Barriers

  • Latch: Allows agents to wait until a set of operations has been completed. Ideal for scenarios where multiple tasks must reach a common point before proceeding. For example, the completion of multiple data loading operations before data processing begins.

Errors

  • LockError: A generic error related to errors in lock acquisition, release and management.

  • MultiLockError: Occurs when attempting to acquire multiple locks simultaneously.

  • MultiUnlockError: Occurs when attempting multiple unlocks simultaneously.

  • OwnershipError: Occurs when attempting to unlock an unacquired mutex.

  • RelockError: Occurs when attempting to lock an already acquired mutex. Prevents deadlocks from occurring.

  • TimeoutError: Occurs when an operation exceeds a set time, such as when using tryLockFor or tryLockUntil.

Browsers

Browser security requirements for using shared memory must be met. Please see SharedArrayBuffer > Security Requirements for details.

Sync methods

The use of synchronous methods (e.g. lockSync) may not be allowed on the main thread. If not, their async versions (e.g. lock) are available.

Contributing

Contributions are welcome!

  • Bug Reports: Please use the GitHub issue tracker to report any bugs. Include a detailed description and any relevant code snippets or logs.

  • Feature Requests: Please submit feature requests as issues, clearly describing the feature and its potential benefits.

  • Pull Requests: Please ensure your code adheres to the existing style of the project and include any necessary tests and documentation.

For more information, check out the contributor's guide.