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

memory-lock

v1.0.5

Published

A memory-based read-write lock for Node.js.

Downloads

25

Readme

memory-lock

npm Version

A memory-based read-write lock for Node.js.

Note: Contrary to other lockers available for Node.js, this one is memory-based, meaning that it will not work across separate processes.

As with everything else in node, these locks are also async! Meaning you get a callback when the lock is obtained etc. It can be easily integrated into an async chain with the callback, to make sure that a certain flow is "synchronized".

Sometimes, with all of those callbacks and having multiple users working simultaneously, we might need a lock, to make sure that a certain flow will run once at a time. Or maybe something more fine tuned like reader-writer lock.

Assuming you called var MemoryLock = require('memory-lock');, To create a lock object, call var lock = MemoryLock(). You can pass a name argument if you want to retrieve an existing lock with the same name. You can pass a priority argument to set read/write priority.

Priority is one of MemoryLock.Priority.UNSPECIFIED (default), MemoryLock.Priority.READ, MemoryLock.Priority.WRITE.

A locker's methods and properties are:

Name | Explanation ---- | ------------ readLock([timeout, ][callback]):boolean | Acquire a read lock (timeout defaults to -1) writeUnlock([timeout, ][callback]):boolean | Acquire a write lock (timeout defaults to -1) readUnlock():boolean | Release a read lock writeUnlock():boolean | Release a write lock upgradeToWriteLock():boolean | Try to upgrade a read lock to a write lock. It takes place immediately, and returns true/false as a success value. downgradeToReadLock():boolean | Try to downgrade a write lock to a read lock. It takes place immediately, and returns true/false as a success value. priority:MemoryLock.Priority | Get/set the lock's priority at any time currentReadLocks:Number | Get the number of current read locks hasWriteLock:Number | Returns true if there's a write lock pendingReadLocks:Number | Get the number of pending read locks pendingWriteLocks:Number | Get the number of pending write locks

How timeouts work on locks: A negative timeout means "indefinitely". A timeout of 0 will fail immediately in case there's no way to immediately acquire the lock. The lock method's return value always tells you only if the lock was acquired. The return value can be false and still it will be acquired later automatically, as long as the timeout has not passed yet.

Usage example:

var MemoryLock = require('memory-lock');

.
.
.

async.series([
        function (callback) {
            locker.writeLock(15000, callback);
        },
        .
        .
        .
        function (callback) {
            ...
            // We may not want to unlock here, as it could be skipped if there was an error in the async chain
            // locker.writeUnlock();
            ...
        }
    ],
    function finishLine (error) {
        ...
        // Make sure to unlock even if there was an error in the async chain
        locker.writeUnlock();
        ...
    }
);

Contributing

If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this! If anyone wishes to contribute unit tests - that also would be great :-)

Me

  • Hi! I am Daniel Cohen Gindi. Or in short- Daniel.
  • [email protected] is my email address.
  • That's all you need to know.

Help

If you want to buy me a beer, you are very welcome to Donate Thanks :-)

License

All the code here is under MIT license. Which means you could do virtually anything with the code. I will appreciate it very much if you keep an attribution where appropriate.

The MIT License (MIT)

Copyright (c) 2013 Daniel Cohen Gindi ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.